-
-
Notifications
You must be signed in to change notification settings - Fork 777
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix deserialization of empty structs and tuples in untagged enums #2805
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Moved and renamed: From test_annotations - test_expecting_message_untagged_tagged_enum => expecting_message - flatten::enum_::untagged::straitforward => contains_flatten From test_macros - test_untagged_newtype_struct => newtype_struct - test_untagged_enum => complex - test_untagged_enum_with_flattened_integer_key => contains_flatten_with_integer_key - test_enum_in_untagged_enum => newtype_enum - test_untagged_bytes => string_and_bytes - test_untagged_newtype_variant_containing_unit_struct_not_map => newtype_unit_and_empty_map
(review this commit with "ignore whitespace changes" option on)
…en==0 condition failures (2): newtype_enum::empty_struct_from_seq newtype_enum::tuple0
SeqRefDeserializer::deserialize_any has a special condition for empty sequence, which emits visit_unit. That condition assumes that type would be able to deserialized from unit, but: 1) struct variants was never able to deserialize from it (they expect only visit_map or visit_seq) 2) tuple variants even with zero fields expect visit_seq only. The suggestion to accept visit_unit instead was rejected in serde-rs#2520 Fixes (2): newtype_enum::tuple0 newtype_enum::empty_struct_from_seq
Although they are slightly different, this difference is irrelevant: - MapDeserializer has a specialization for deserialize_seq and deserialize_tuple, but only MapRefDeserializer::deserialize_any is used by the code which is almost the same - MapDeserializer checks that map was consumed after visit_map, but MapRefDeserializer does not. Actually, each derived implementation consumes map and each manual implementation also should consume it Also, MapDeserializer already used when value deserialized from ContentRefDeserializer directly and MapRefDeserializer was only used to deserialize Struct variants of enums. There are no reasons why the behavior should be different in those two cases
dtolnay
changed the title
Fix deserialziation of empty structs and tuples in untagged enums
Fix deserialization of empty structs and tuples in untagged enums
Aug 24, 2024
dtolnay
approved these changes
Aug 24, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR is logical continuation of #2788 and #2804, for untagged enums. While I working on it I found a bug that was fixed.
This PR moves all tests which tests top-level untagged enums (those which is defined using
#[serde(untagged)]
attribute) into a dedicated file and adds tests to cover missing methods ofContentRefDeserializer
that used for deserialization. Flatten tests where untagged enum is flattened into structure I consider foremost as flatten tests, so they left untouched.Each method in
ContentRefDeserializer
and linked structs was marked with a comment that shows which test(s) cover it. After this work a bug was catched:The
Untagged
type could not be deserialized becauseSeqRefDeserializer
(which is used for deserialization) has a special code to handle empty sequences which was not covered and never worked:serde/serde/src/private/de.rs
Lines 2228 to 2244 in 1a9ffdb
The new tests shows that it is impossible to create such a type that would accept
visit_unit()
in line 2234. The conditionlen == 0
assumes that type would be able to deserialized from unit, but:visit_map
orvisit_seq
)visit_seq
only. The suggestion to acceptvisit_unit
instead was rejected in Fix incorrect representation of tuples with skipped fields #2520After appling the fix the
MapRefDeserializer
andSeqRefDeserializer
no longer used so they are removed. Actually, because direct deserialization of maps and sequences from theContentRefDeserializer
usesvalue::MapDeserializer
, that is just unifies the behaviour:serde/serde/src/private/de.rs
Lines 1938 to 1946 in 1a9ffdb
serde/serde/src/private/de.rs
Lines 1967 to 1975 in 1a9ffdb
value::MapDeserializer
andvalue::SeqDeserializer
are used:serde/serde/src/private/de.rs
Lines 1685 to 1718 in 1a9ffdb