Skip to content

Serialization of enums adds extra tags #346

Closed
@Storyyeller

Description

@Storyyeller

Currently, we have some types using quick_xml and serde to parse XML, which works well. However, we want to also serialize them back to XML, and the XML that quick_xml produces differs from the original (expected) format, and I can't figure out any way to fix it.

Our type definitions look like this:

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Contactinfo {
    pub bugurl: String,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Include {
    pub name: String,
    #[serde(default)]
    pub groups: String,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum ManifestChild {
    Include(Include),
    Contactinfo(Contactinfo),

    #[serde(other)]
    Unknown,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "kebab-case")]
pub struct Manifest {
    #[serde(default, rename = "$value")]
    pub children: Vec<ManifestChild>,
}

And we can parse XML with it just fine:

let text = r#"
<?xml version="1.0" encoding="UTF-8"?>
<manifest>
  <include name="bar"/>
  <contactinfo bugurl="foo"/>
  <include name="hello" groups="world"/>
</manifest>"#;

    let result = quick_xml::de::from_str(text);
    let m: Manifest = result.unwrap();

    let s = quick_xml::se::to_string(&m).unwrap();
    println!("{}", s);

However, the output XML is in the wrong format, wrapping all the enum contents in an extra tag. How can we get rid of this and make it serialize in the same way as the input xml? Also, why is Manifest capitalized in the output when it isn't in the input? It seems like serialization ignores our #[serde(rename_all = "kebab-case")] annotations!

Here is what the output looks like with quick_xml 0.23alpha3:

<Manifest><include><Include name="bar"/></include><contactinfo><Contactinfo bugurl="foo"/></contactinfo><include><Include name="hello" groups="world"/></include></Manifest>

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugserdeIssues related to mapping from Rust types to XML

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions