Skip to content

Conversation

max-heller
Copy link

When serde_derive generates Serialize/Deserialize implementations for an enum with #[serde(skipped)] variants, it leaves a gap in the assigned variant_indexes for the variants (i.e., includes skipped variants in the count) in the Serialize impl, but excludes skipped variants in the count in the Deserialize impl. This means that non-self-describing data formats that rely on variant indices instead of variant names cannot correctly serialize+deserialize enums with skipped variants.

This changes the generated Serialize impl to match the deserialize impl by excluding skipped variants from the count. This is also consistent with how #[serde(skip)] affects field indices in structs.

Fixes #2614

This is not covered by existing tests because the test suite only covers variant names, not variant indices (serde-deprecated/test#21). As such, I'm not sure the best way to add test coverage for this particular case.

When serde_derive generates Serialize/Deserialize implementations for an
enum with #[serde(skipped)] variants, it leaves a gap in the assigned
`variant_index`es for the variants (i.e., includes skipped variants
in the count) in the Serialize impl, but excludes skipped variants in the
count in the Deserialize impl. This means that non-self-describing data
formats that rely on variant indices instead of variant names cannot
correctly serialize+deserialize enums with skipped variants.

This changes the generated Serialize impl to match the deserialize impl
by excluding skipped variants from the count. This is also consistent
with how #[serde(skip)] affects field indices in structs.

Fixes serde-rs#2614
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Binary serialize/deserialize inconsistency of enums with skipped fields

1 participant