Problem deserializing a struct that can be serialized

Aperta
#752 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
38/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
rust
Ambito
backend

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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`
❯
Lingua principale
Rust
Stelle
1.6k
Fork
299
Merge medio
2g 4h
PR unite (30g)
9

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di tafia/quick-xml

Tutte le issue di tafia/quick-xml

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.