Problem deserializing a struct that can be serialized

Open
#752 2 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
38/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
rust
Domain
backend

Research direction

Start with the issue’s minimal Rust example and its failing deserialization tests, especially the Source enum and the source field’s serde(rename = "Source") attribute. Check quick-xml’s deserialization behavior for repeated XML elements and enum values; done means the shown XML deserializes to the expected Sources values while the serialization test still passes.

Written by the indexing model from the issue text.

Description

arrays bug help wanted serde

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`
❯
Dominant language
Rust
Stars
1.6k
Forks
299
Avg merge
2d 4h
Merged PRs (30d)
9

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from tafia/quick-xml

All issues in tafia/quick-xml

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.