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

Problem deserializing a struct that can be serialized #752

Open
KorribanMaster opened this issue Jun 3, 2024 · 2 comments
Open

Problem deserializing a struct that can be serialized #752

KorribanMaster opened this issue Jun 3, 2024 · 2 comments
Labels
arrays Issues related to mapping XML content onto arrays using serde bug help wanted serde Issues related to mapping from Rust types to XML

Comments

@KorribanMaster
Copy link

KorribanMaster commented Jun 3, 2024

I have a Problem de serializing the following xml

<Sources>
    <Source>A</Source>
    <Source>B</Source>
</Sources>

This is my current attempt

/*
[dependencies]
serde = { version = "1.0", features = ["derive"] }
quick-xml = {version="0.31", features = ["serialize"]}
*/
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct Sources {
    #[serde(rename = "Source")]
    source: Vec<Source>,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
enum Source {
    A,
    B,
}

#[cfg(test)]
mod tests {

    use quick_xml::de::from_str;

    use super::*;
    #[test]
    fn test_serialize_sources() {
        let sources = Sources {
            source: vec![Source::A, Source::B],
        };
        let xml = r#"<Sources><Source>A</Source><Source>B</Source></Sources>"#;
        let serialized = to_string(&sources).expect("Failed to serialize sources"); // Use the to_string function from quick_xml
        assert_eq!(serialized, xml);
    }
    #[test]
    fn test_deserialize_sources(){
        let xml = r#"<Sources><Source>A</Source><Source>B</Source></Sources>"#;
        let sources = Sources{ source: vec![Source::A, Source::B]};
        let deserialize = from_str::<Sources>(&xml).expect("Failed to deserialize sources");
        assert_eq!(deserialize, sources);
    }
    #[test]
    fn test_deserialize_source(){
        let xml = r#"<Sources><Source>B</Source></Sources>"#;
        let sources = Sources{ source: vec![Source::B]};
        let deserialize = from_str::<Sources>(&xml).expect("Failed to deserialize sources");
        assert_eq!(deserialize, sources);
    }
        
}

which fails with the following error

running 3 tests
test tests::test_deserialize_sources ... FAILED
test tests::test_serialize_sources ... ok
test tests::test_deserialize_source ... FAILED

failures:

---- tests::test_deserialize_sources stdout ----
thread 'tests::test_deserialize_sources' panicked at src/main.rs:45:53:
Failed to deserialize sources: Custom("unknown variant `Source`, expected `A` or `B`")
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

---- tests::test_deserialize_source stdout ----
thread 'tests::test_deserialize_source' panicked at src/main.rs:54:53:
Failed to deserialize sources: Custom("unknown variant `Source`, expected `A` or `B`")


failures:
    tests::test_deserialize_source
    tests::test_deserialize_sources

test result: FAILED. 1 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

error: test failed, to rerun pass `--bin example_issue_quick_xml`
❯
@KorribanMaster KorribanMaster changed the title Problem deserializing a list Problem deserializing a struct that can be serialized Jun 3, 2024
@KorribanMaster
Copy link
Author

To check it out yourself i uploaded a simple example project to reproduce the error
https://github.com/KorribanMaster/example_quick_xml

@Mingun Mingun added bug serde Issues related to mapping from Rust types to XML arrays Issues related to mapping XML content onto arrays using serde labels Jun 3, 2024
@Mingun
Copy link
Collaborator

Mingun commented Jun 3, 2024

Yes, there is unconsistency between serializer and deserializer. #717 could be related.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrays Issues related to mapping XML content onto arrays using serde bug help wanted serde Issues related to mapping from Rust types to XML
Projects
None yet
Development

No branches or pull requests

2 participants