Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deserialization fails when choice and element are in complex element #342

Closed
idaniel86 opened this issue Nov 18, 2021 · 0 comments · Fixed by #387
Closed

Deserialization fails when choice and element are in complex element #342

idaniel86 opened this issue Nov 18, 2021 · 0 comments · Fixed by #387
Labels
bug serde Issues related to mapping from Rust types to XML

Comments

@idaniel86
Copy link

idaniel86 commented Nov 18, 2021

Serialization works, but deserialization not:

#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "PascalCase")]
struct Root {
    element: Element,
    #[serde(rename = "$value")]
    choice: Choice,
}

#[derive(Debug, serde::Serialize, serde::Deserialize)]
struct Element(String);

#[derive(Debug, serde::Serialize, serde::Deserialize)]
enum Choice {
    ChoiceOne(String),
    ChoiceTwo(String),
}

fn main() {
    let root = Root {
        element: Element("element".to_string()),
        choice: Choice::ChoiceTwo("choice_two".to_string()),
    };

    let s = quick_xml::se::to_string(&root).unwrap();
    println!("{:?}", s);
    let r: Root = quick_xml::de::from_str(&s).unwrap();
    println!("{:?}", r);
}

Serialization:

<Root><Element>element</Element><ChoiceTwo>choice_two</ChoiceTwo></Root>

Deserialization error:

'main' panicked at 'called `Result::unwrap()` on an `Err` value: Custom("unknown variant `Element`, expected `ChoiceOne` or `ChoiceTwo`")'
@Mingun Mingun added bug serde Issues related to mapping from Rust types to XML labels May 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug serde Issues related to mapping from Rust types to XML
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants