Skip to content

Untagged enums with empty variants (de)serialize in unintuitive ways #1560

@Sushisource

Description

@Sushisource

Hello - I've got some structs like:

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct SubscriptionUpdate {
    pub result: SubscriptionResult,
    pub subscription: u64,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(untagged)]
pub enum SubscriptionResult {
    #[serde(rename = "ready")]
    Ready,
    #[serde(rename = "invalid")]
    Invalid,
    Whatever {
        something: String,
    },
}

I would expect

let t = SubscriptionUpdate {
    subscription: 0,
    result: SubscriptionResult::Invalid,
};

To serialize as
{ "result": "invalid", "subscription": 0}
but it's
{ "result": "null", "subscription": 0}

That strikes me as really surprising, and I haven't yet figured how to get it to behave the way I'd like. Any advice? Does this qualify as a bug?

Love serde, btw, great work!

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