serde-rs / serde

Serialization framework for Rust
https://serde.rs/
Apache License 2.0
9.08k stars 767 forks source link

Alias ignored when flattening #2188

Open TheEnbyperor opened 2 years ago

TheEnbyperor commented 2 years ago

Given an enum with a name and an alias, such as below.

#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum PSCNatureOfControls {
    #[serde(
        rename = "{http://xmlgw.companieshouse.gov.uk}NatureOfControls",
        alias = "{http://www.govtalk.gov.uk/CM/envelope}NatureOfControls"
    )]
    NatureOfControls(NatureOfControls),
    #[serde(
        rename = "{http://xmlgw.companieshouse.gov.uk}LLPNatureOfControls",
        alias = "{http://www.govtalk.gov.uk/CM/envelope}LLPNatureOfControls"
    )]
    LLPNatureOfControls(LLPNatureOfControls),
}

Then put in a struct as such, and flattened.

#[derive(Debug, Deserialize)]
pub struct CompanyDataPSCNotification {
    #[serde(flatten)]
    pub nature_of_control: PSCNatureOfControls,
    ...
}

And when an enum value with the alias name is presented to the deserializer, an error such as "no variant of enum PSCNatureOfControls found in flattened data" is given. When the enum variant is changed from alias to rename, it works as expected.

jplatte commented 2 years ago

Duplicate of #1504.

Mingun commented 1 year ago

This was fixed (partially) by #2387, the in-place part is fixed in #2443

Colerar commented 1 year ago

This issue has not yet been fully resolved. struct + flatten + alias works as expected, but enum + flatten + alias does not. Just a reminder.