Unexpected Behaviour with Unit Variants in Serde

I'm trying to use this crate with serde and serde_yaml on an enum that has some unit variants and some struct variants. I expect to be able to deserialize the unit variants as unit variants, but serde complains that it is expecting a newtype variant instead. You can consider the following example.

With the following:

#[enum_dispatch]
trait SomeTrait {
  // ...
}

#[enum_dispatch(SomeTrait)]
#[derive(Deserialize)]
enum MyEnum {
  Foo,
  Bar,
}

#[derive(Deserialize)]
struct Foo {
  test: u32,
}

#[derive(Deserialize)]
struct Bar;

#[derive(Deserialize)]
struct MyContainer {
  list: Vec<MyEnum>,
}

I would expect to be able to deseralize Bar as a unit variant. However, serde is expecting a newtype variant with null instead. For example, with serde_yaml, the following does not work:

list:
  - Foo: {test: 12}
  - Bar

While the following does work:

list:
  - Foo: {test: 12}
  - Bar: null

I'd like to get the behaviour from the first example. Is this possible?

Edited Feb 28, 2021 by William Findlay
Assignee Loading
Time tracking Loading