Skip to content

Deserializing tagged enum derails the parser #586

Description

@Kriskras99

Example:

use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
#[serde(tag = "@tag")]
pub enum TaggedEnum {
    VariantTag(WrappedVariant),
}

#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct WrappedVariant {
    pub variant: Variant,
}

#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Variant {
    #[serde(rename = "@float")]
    pub float: f64,
}


fn main() {
    let document = r#"
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <variant float="0.0">
    </variant>
    "#;
    let _: Variant = quick_xml::de::from_str(document).unwrap();

    let document = r#"
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <tagged_enum tag="VariantTag">
        <variant float="0.0">
        </variant>
    </tagged_enum>
    "#;
    let _: TaggedEnum = quick_xml::de::from_str(document).unwrap();
}

Expected behaviour

Both documents are deserialized correctly without any errors.

Actual behaviour

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Custom("invalid type: string \"0.0\", expected f64")', src/main.rs:38:59

The parser seems to derail completely, any type that is not a String gives an error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugduplicateserdeIssues 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