Skip to content

serde_bser: serialize struct enum variants in object form - #1358

Open
rootkiller6788 wants to merge 1 commit into
facebook:mainfrom
rootkiller6788:fix-serde-bser-struct-variant
Open

serde_bser: serialize struct enum variants in object form#1358
rootkiller6788 wants to merge 1 commit into
facebook:mainfrom
rootkiller6788:fix-serde-bser-struct-variant

Conversation

@rootkiller6788

Copy link
Copy Markdown

Summary

serde_bser::Serializer::serialize_struct_variant was implemented by delegating to serialize_tuple_variant, which emits the variant payload as a BSER array:

{"TestStruct": ["abc", null, "def", "💩"]}

But the deserializer (VariantAccess::struct_variantdeserialize_any) decodes the payload as a map — the object form {"TestStruct": {"abc": null, "def": "💩"}} that de::test::test_struct_variant already exercises. Struct-variant enums therefore could not round-trip: serializefrom_slice failed with invalid type: string "abc", expected unit.

Fix

Serialize struct variants in object form, writing <BSER_OBJECT> 1 <variant name> <BSER_OBJECT nfields> (key value)*, instead of reusing the tuple-variant path. The Compound type is shared by both SerializeStructVariant and SerializeStruct, so the field-name-keyed output is produced by the same SerializeStruct machinery used for ordinary structs.

Test

  • Updated BASIC_SERIALIZED expectation in ser/test.rs to the object form (only the container tag byte changes; PDU length is unchanged).
  • Added test_struct_variant_round_trip, which serializes TestStruct { abc: (), def: '💩' } and deserializes it back — this failed before the fix and passes after.

cargo test in watchman/rust/serde_bser: 16 passed.

serialize_struct_variant delegated to serialize_tuple_variant, emitting a
struct variant as {"Variant": [field_name, value, ...]} instead of the
{"Variant": {"field": value, ...}} form that the deserializer expects
when decoding a struct variant. Enums with struct variants therefore could
not round-trip through serde_bser (deserializing the emitted bytes failed
with a type mismatch).

Emit the variant as an object keyed by field name, matching the format
already exercised by de::test::test_struct_variant, and add a round-trip
regression test.
@meta-cla meta-cla Bot added the CLA Signed label Aug 21, 2026
@meta-codesync

meta-codesync Bot commented Aug 21, 2026

Copy link
Copy Markdown

This pull request has been imported. If you are a Meta employee, you can view this in D116917626. (Because this pull request was imported automatically, there will not be any future comments.)

@rootkiller6788
rootkiller6788 marked this pull request as ready for review August 22, 2026 14:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant