Skip to content

#[serde(flatten)] doesn't work with #[serde(default)] for externally tagged enums #1626

@slckl

Description

@slckl

Given this:

#[derive(Deserialize, Debug)]
enum SomeEnum {
    A(u32),
    B(u32),
}

impl Default for SomeEnum {
    fn default() -> Self {
        SomeEnum::B(1)
    }
}

#[derive(Deserialize, Debug)]
struct SomeStruct {
    filler: u32,
    #[serde(default)]
    #[serde(flatten)]
    some_enum: SomeEnum,
}

trying to deserialize this yaml:

filler: 1

into SomeStruct panics with a message:

no variant of enum SomeEnum found in flattened data

I hoped it would pick up the default value specified for SomeEnum instead...

Playground example.

Is this a bug or is this not supported?
Docs don't cover this, I tried searching issues, but couldn't find anything quite like this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions