Reject struct field names that collide with #[serde(tag)] - #3095
Open
SebTardif wants to merge 2 commits into
Open
Reject struct field names that collide with #[serde(tag)]#3095SebTardif wants to merge 2 commits into
SebTardif wants to merge 2 commits into
Conversation
Named structs with #[serde(tag = "...")] inject a type-name field under that key on serialize. Enums already rejected a field (or alias) whose serialize name equals the tag. Structs skipped that check, so a field renamed to the same key produced duplicate map keys and failed to round-trip. Mirror the enum conflict check for named structs, skip flattened fields (they never appear under their own name), and add UI tests matching the existing enum coverage. Signed-off-by: Sebastien Tardif <sebtardif@ncf.ca>
Documentation failed downloading cargo-docs-rs (curl 503/56), not doc content. External contributors cannot gh run rerun --failed. Signed-off-by: Sebastien Tardif <sebtardif@ncf.ca>
This file contains hidden or 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
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.
Summary
Reject named-struct field (and alias) names that collide with
#[serde(tag = "...")], matching the existing check for enum struct variants.Problem
Enums with an internal tag already error at derive time when a field serialize name or alias equals the tag:
Named structs with the same attribute skipped that check. Serialize injects the type name under the tag key, then also emits the colliding field, which produces duplicate keys and breaks round-trips.
This early return for structs has been present since the internal-tag field conflict check was introduced (2018).
Change
check_internal_tag_field_name_conflictto named structs (Style::Struct)tag = "data"with#[serde(flatten)] data)internal-tag-struct.rsandinternal-tag-struct-alias.rsnext to the existing enum casesValidation
Flatten + tag with a matching field name still compiles (regression covered by the flatten skip).