tafia / quick-xml

Rust high performance xml reader and writer
MIT License
1.22k stars 237 forks source link

Error deserializing a flattened struct #777

Closed djnalluri closed 4 months ago

djnalluri commented 4 months ago

There's an issue deserializing flattened structs. The below reproducer works fine for the JSON test but fails for the XML one. It panics with invalid type: map, expected a string.

Reproducer:

use serde::Deserialize;

#[derive(Deserialize)]
pub struct RecordWrapper {
    pub key: String,

    #[serde(flatten)]
    pub data: RecordData
}

#[derive(Deserialize)]
pub struct RecordData {
    foo: String,
    bar: String,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_xml() {
        let xml = r"
            <Record>
                <key>Record Key</key>
                <foo>A</foo>
                <bar>B</bar>
            </Record>";

        let record = quick_xml::de::from_str(xml).unwrap();
        verify_record(record);
    }

    #[test]
    fn parse_json() {
        let record = serde_json::from_str(r#"{
            "key": "Record Key",
            "foo": "A",
            "bar": "B"
        }"#).unwrap();
        verify_record(record);
    }

    fn verify_record(record: RecordWrapper) {
        assert_eq!(record.key, "Record Key");
        let data = record.data;
        assert_eq!(data.foo, "A");
        assert_eq!(data.bar, "B");
    }
}
Mingun commented 4 months ago

Yes, unfortunately this is unavoidable flaw of the flatten design. Duplicate of #714