RReverser/serde-xml-rs

serde(other) - not working with nested other tags

Open

#237 geöffnet am 28. Juni 2025

Auf GitHub ansehen
 (1 Kommentar) (0 Reaktionen) (0 zugewiesene Personen)Rust (100 Forks)github user discovery
help wanted

Repository-Metriken

Stars
 (333 Stars)
PR-Merge-Metriken
 (PR-Metriken ausstehend)

Beschreibung

Trying to deserialize an xml with child nodes , possibly out of order child nodes and some unknown / other child nodes that can appear too.

For eg:

<root>
            <a>
                <some>value</some>
            </a>
            <nested>
                <c></c>
            </nested>
            <a>
                <some>value2</some>
            </a>
            <b>
                <another>value</another>
            </b>
</root>     

Now - before reading this xml string - I know I can expect a and b child nodes - but nested was mostly an unknown at the time of reading . I don't want the parser to fail - but just handle it under #[serde(other)] so I known an unknown / other element appeared in the tree.

But the moment the unknown element #[serde(other)] is a nested element - the parser seems to fail and not parse any further.

Example code with test case , written below. With 4 child nodes / enum ( I expect 3 of them to be identified correctly as either a or b - but one of them to be Others ).


    #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
    pub struct ElementA {
        #[serde(rename = "some")]
        some: String
    }

    #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
    pub struct ElementB {
        #[serde(rename = "another")]
        another: String

    }

    #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
    pub enum MyEnum {
        #[serde(rename = "a")]
        ElementA(Box<ElementA>),
        #[serde(rename = "b")]
        ElementB(Box<ElementB>),
        #[serde(other)]
        Others,
    }

    #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
    pub struct MyStruct {
        #[serde(default, rename = "#content")]
        pub items: Vec<MyEnum>,
    }

    /// Test case involving a serde parsing error with out of place
    /// child nodes
    #[test]
    fn test_parse_serde_xml_rs_other() {
        let contents = r###"
            <root>
            <a>
                <some>value</some>
            </a>
            <nested>
                <c></c>
            </nested>
            <a>
                <some>value2</some>
            </a>
            <b>
                <another>value</another>
            </b>
        </root>        
        "###;
        let result: Result<MyStruct, serde_xml_rs::Error> = from_str(contents);
        assert!(result.is_ok());
        let my_struct = result.unwrap();
        assert_eq!(my_struct.items.len(), 4);    // the test fails with value to be 1 . Unable to parse beyond `nested` attribute
    }

But the parser test case fails entirely.

What gives ? How does recursive parsing work for #[serde(other)] ? Is that the right track - or anything else we are missing

Contributor Guide