Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aliases for enum variants are not working with flattened enums #1847

Closed
Bergmann89 opened this issue Jul 2, 2020 · 6 comments
Closed

Aliases for enum variants are not working with flattened enums #1847

Bergmann89 opened this issue Jul 2, 2020 · 6 comments

Comments

@Bergmann89
Copy link

I have an enum with some aliases for the variants. The enum is placed as field inside a struct. If I add the flatten tag to the field, the deserialization fails with the error no variant of enum MyEnum found in flattened data. If I don't use the flatten tag, everything works as expected.

Not working example (with flatten tag):

#[derive(Deserialize, Debug, PartialEq)]
pub enum MyEnum {
    #[serde(alias = "ValueStringAlias")]
    #[serde(rename = "ValueString")]
    String(String),

    #[serde(alias = "ValueBooleanAlias")]
    #[serde(rename = "ValueBoolean")]
    Boolean(bool),
}

#[derive(Deserialize, Debug, PartialEq)]
#[serde(rename_all = "PascalCase")]
struct Test {
    number: u32,

    #[serde(flatten)]
    my_enum: MyEnum,
};

const TEST_XML: &'static str = r##"
    <Test>
        <Number>123</Number>
        <ValueBooleanAlias>true</ValueBooleanAlias>
    </Test>
"##;

Working example (without flatten tag):

#[derive(Deserialize, Debug, PartialEq)]
pub enum MyEnum {
    #[serde(alias = "ValueStringAlias")]
    #[serde(rename = "ValueString")]
    String(String),

    #[serde(alias = "ValueBooleanAlias")]
    #[serde(rename = "ValueBoolean")]
    Boolean(bool),
}

#[derive(Deserialize, Debug, PartialEq)]
#[serde(rename_all = "PascalCase")]
struct Test {
    number: u32,
    my_enum: MyEnum,
};

const TEST_XML: &'static str = r##"
    <Test>
        <Number>123</Number>
        <MyEnum>
            <ValueBooleanAlias>true</ValueBooleanAlias>
        </MyEnum>
    </Test>
"##;

I've used quick-xml as deserializer here, but it semse to be a bug of serde.

@dtolnay
Copy link
Member

dtolnay commented Jan 23, 2022

Closing as a duplicate of #1504.

@dtolnay dtolnay closed this as completed Jan 23, 2022
@Anders429
Copy link

While the duplicate issue, #1504, is closed, the issue in the original comment here is still a problem. Using the same code from the original comment, I have put together an example using serde_json (so I could have it run on the playground) to show the problem: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=088644732feb07e0e7d1a97f423838ad

It seems that the solution to #1504 fixed the problem for field aliases, but not variant aliases.

Can we reopen this issue? Or is there another issue tracking enum variant aliases?

@Mingun
Copy link
Contributor

Mingun commented Jul 29, 2024

@jaymell
Copy link

jaymell commented Aug 14, 2024

@Anders429 You have a nice repro for the outstanding problem with enum variants. I would open a separate issue for better tracking.

@kskalski
Copy link

Seems like the outstanding problem with enum+alias+flattened is also reported here #2188

@Anders429
Copy link

Anders429 commented Dec 6, 2024

#2566 being merged does completely resolve this issue. This can be verified by rerunning the playground I linked above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

6 participants