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

Fat enum not deserialized correctly #172

Open
jwodder opened this issue Jan 28, 2024 · 1 comment
Open

Fat enum not deserialized correctly #172

jwodder opened this issue Jan 28, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@jwodder
Copy link

jwodder commented Jan 28, 2024

Running cargo test on the following code:

use yaserde_derive::YaDeserialize;

#[derive(Clone, Debug, Eq, PartialEq, YaDeserialize)]
#[yaserde(rename = "propfind", namespace = "DAV:")]
pub enum Propfind {
    PropName { propname: PropName },
    AllProp { allprop: AllProp },
}

// Just to shut yarserde up
impl Default for Propfind {
    fn default() -> Propfind {
        Propfind::AllProp { allprop: AllProp }
    }
}

impl Propfind {
    pub fn from_xml(s: &str) -> Result<Propfind, String> {
        yaserde::de::from_str(s)
    }
}

#[derive(Clone, Debug, Default, Eq, PartialEq, YaDeserialize)]
pub struct AllProp;

#[derive(Clone, Debug, Default, Eq, PartialEq, YaDeserialize)]
pub struct PropName;

#[cfg(test)]
mod tests {
    use super::*;
    use indoc::indoc;

    #[test]
    fn allprop() {
        let s = indoc! {r#"
            <?xml version="1.0" encoding="utf-8" ?>
            <propfind xmlns="DAV:">
                <allprop/>
            </propfind>
        "#};
        let pf = Propfind::from_xml(s).unwrap();
        assert_eq!(pf, Propfind::AllProp { allprop: AllProp });
    }

    #[test]
    fn propname() {
        let s = indoc! {r#"
            <?xml version="1.0" encoding="utf-8" ?>
            <propfind xmlns="DAV:">
                <propname/>
            </propfind>
        "#};
        let pf = Propfind::from_xml(s).unwrap();
        assert_eq!(pf, Propfind::PropName { propname: PropName });
    }
}

with the following dependencies:

[dependencies]
yaserde = "0.9.1"
yaserde_derive = "0.9.1"

[dev-dependencies]
indoc = "2.0.4"

results in the propname test failing because the XML is deserialized as Propfind::AllProp { allprop: AllProp } instead of the expected Propfind::PropName variant.

How can I get the given XML snippets to deserialize the way I want?

@MarcAntoine-Arnaud MarcAntoine-Arnaud added the bug Something isn't working label Feb 6, 2024
@antis81
Copy link

antis81 commented Jun 13, 2024

Looks like this feature request also relates to what I was trying to achieve (example in #185). The approach is slightly different here, but the goal is practically identical. Often it is needed to keep the element order throughout the de-/serialization process.

Does #189 implement this actually?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants