RReverser / serde-xml-rs

xml-rs based deserializer for Serde (compatible with 1.0+)
https://crates.io/crates/serde-xml-rs
MIT License
269 stars 90 forks source link

Root element always deserializes #139

Open rapodaca opened 4 years ago

rapodaca commented 4 years ago

Using a slight modification of the code from the README and version 0.4.0:

#[macro_use]
extern crate serde;
extern crate serde_xml_rs;

use serde_xml_rs::from_reader;

#[derive(Debug, Deserialize)]
struct Item {
    pub name: String,
    pub source: String
}

#[derive(Debug, Deserialize)]
struct Project {
    pub name: String,

    #[serde(rename = "Item", default)]
    pub items: Vec<Item>
}

fn main() {
    let s = r##"
        <P name="my_project">
            <Item name="hello" source="world.rs" />
        </P>
    "##;
    let project: Project = from_reader(s.as_bytes()).unwrap();
    println!("{:#?}", project);
}

Output is:

Project {
    name: "my_project",
    items: [
        Item {
            name: "hello",
            source: "world.rs",
        },
    ],
}

I expect to see an error. I can change the P tag to anything and it still works.

If this is expected behavior, how can I enforce the tag Project at the root?

punkstarman commented 4 years ago

@rapodaca, thank you for submitting this.

I don't think that this is expected behavior. Rather, it is a little quirk that I knew about but didn't mind.

I'll take a stab at fixing this.