Skip to content

Nested untagged and externally tagged enums fail to deserialize #217

@caelunshun

Description

@caelunshun

The following example deserializes an untagged enum with two variants using ron, serde_json, and serde_yaml. The latter two libraries output the expected result, but RON fails with a "data did not match any variant of untagged enum Either" error.

use serde::Deserialize;

#[derive(Debug, Deserialize)]
struct Root {
    value: Either,
}

#[derive(Debug, Deserialize)]
#[serde(untagged)]
enum Either {
    One(One),
    Two(Two),
}

#[derive(Debug, Deserialize)]
enum One {
    OneA,
    OneB,
    OneC,
}

#[derive(Debug, Deserialize)]
enum Two {
    TwoA,
    TwoB,
    TwoC,
}

const RON_SRC: &str = "OneA";
const JSON_SRC: &str = "{ \"value\": \"OneA\" }";
const YAML_SRC: &str = "value: OneA";

fn main() {
    println!("{:?}", ron::de::from_str::<Either>(RON_SRC)); // Err(Message("data did not match any variant of untagged enum Either"))
    println!("{:?}", ron::de::from_str::<One>(RON_SRC)); // Ok(OneA)
    println!("{:?}", serde_json::from_str::<Root>(JSON_SRC)); // Ok(Root { value: One(OneA) })
    println!("{:?}", serde_yaml::from_str::<Root>(YAML_SRC)); // Ok(Root { value: One(OneA) })
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions