Skip to content

Deserializing struct inside untagged enum variant behaves inconsistently #577

@mierak

Description

@mierak

Hi! Sorry for the issue name, I cannot find a better description. Anyway, there is some weirdness with untagged enum variants. Probably the best description is the contrived example below.

The deserialization works if there is a space(or any other character that does not break it in other ways really, like comments, non default fields etc). The fact that Second( ) deserializes correctly but Second() does not seems extremely weird to me. This fails with Err(SpannedError { code: Message("data did not match any variant of untagged enum Foo"), position: Position { line: 1, col: 9 } })

this is on version 0.10.1

#![allow(dead_code)]
use serde::Deserialize;

#[derive(Deserialize, Eq, PartialEq, Debug)]
#[serde(untagged)]
enum Foo {
    A(String),
    B(Bar),
}

#[derive(Deserialize, Eq, PartialEq, Debug)]
enum Bar {
    First,
    Second(Baz),
}

#[derive(Deserialize, Eq, PartialEq, Debug)]
struct Baz {
    #[serde(default)]
    first: String,
    #[serde(default)]
    second: String,
}

fn main() {
    let a: Result<Foo, _> = ron::de::from_str("\"test\"");
    let b: Result<Foo, _> = ron::de::from_str("Second(first: \"value\")");
    let c: Result<Foo, _> = ron::de::from_str("Second( )");
    let d: Result<Foo, _> = ron::de::from_str("Second()");

    assert_eq!(a, Ok(Foo::A("test".to_owned()))); // Ok
    assert_eq!(b, Ok(Foo::B(Bar::Second(Baz { first: "value".to_owned(), second: "".to_owned() })))); // Ok
    assert_eq!(c, Ok(Foo::B(Bar::Second(Baz { first: "".to_owned(), second: "".to_owned() })))); // Ok
    assert_eq!(d, Ok(Foo::B(Bar::Second(Baz { first: "".to_owned(), second: "".to_owned() })))); // This errors
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions