Skip to content

Problem deserializing a struct that can be serialized #752

Open

Description

@KorribanMaster

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`
❯

Activity

  1. changed the title [-]Problem deserializing a list[/-] [+]Problem deserializing a struct that can be serialized[/+] on Jun 3, 2024
  2. KorribanMaster commented on Jun 3, 2024

    @KorribanMaster
    Author

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

  3. added
    serdeIssues related to mapping from Rust types to XML
    arraysIssues related to mapping XML content onto arrays using serde
    on Jun 3, 2024
  4. Mingun commented on Jun 3, 2024

    @Mingun
    Collaborator

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    arraysIssues related to mapping XML content onto arrays using serdebughelp wantedserdeIssues related to mapping from Rust types to XML

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions