dtolnay / serde-yaml

Strongly typed YAML library for Rust
Apache License 2.0
965 stars 164 forks source link

[Bug] Failed to deserialize a enum inside a tagged enum. #378

Open peng1999 opened 1 year ago

peng1999 commented 1 year ago
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize)]
#[serde(tag = "version")]
enum Outer {
    V1(OuterContent),
}

#[derive(Serialize, Deserialize)]
struct OuterContent {
    a: Inner,
}

#[derive(Serialize, Deserialize)]
enum Inner {
    M {},
}

fn main() {
    let obj = Outer::V1(OuterContent { a: Inner::M {} });

    let b = serde_yaml::to_string(&obj).unwrap();
    println!("{}", b);

    let obj = serde_yaml::from_str::<Outer>(&b).unwrap();
}

The above code failed with output:

version: V1
a: !M {}

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error("a: untagged and internally tagged enums do not support enum input", line: 2, column: 4)', src/main.rs:25:49

This bug is introduced in version 0.9. Maybe it has something to do with !tag syntax. Related: #344