Skip to content

Deserializing enums fails with latest serde #581

@jwodder

Description

@jwodder

The following code:

use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(tag = "type")]
enum Message {
    Request {
        id: u32,
        resource: String,
        operation: String,
    },
    Response {
        id: u32,
        value: String,
    },
}

fn main() {
    let value = Message::Response {
        id: 60069,
        value: "Foobar".into(),
    };
    let serialized = ron::to_string(&value).unwrap();
    println!("Serialized:");
    println!("{serialized}");
    println!();
    let deserialized: Message = ron::from_str(&serialized).unwrap();
    println!("Deserialized:");
    println!("{deserialized:#?}");
}

with the following dependencies:

[dependencies]
ron = "0.11.0"
serde = { version = "1.0.221", features = ["derive"] }

fails with:

Serialized:
(type:"Response",id:60069,value:"Foobar")


thread 'main' panicked at src/main.rs:35:60:
called `Result::unwrap()` on an `Err` value: SpannedError { code: ExpectedString, span: Span { start: Position { line: 1, col: 21 }, end: Position { line: 1, col: 21 } } }
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

It also fails when ron 0.10.1 is used, though there the output is:

Serialized:
(type:"Response",id:60069,value:"Foobar")


thread 'main' panicked at src/main.rs:35:60:
called `Result::unwrap()` on an `Err` value: SpannedError { code: ExpectedString, position: Position { line: 1, col: 21 } }
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

It also fails with serde 1.0.220 but not with 1.0.219.

Other serde formats are not failing with the latest serde, so this appears to be a bug in ron.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions