Add Decoder.AllowDuplicateKeys - #17
Open
reuvenharrison wants to merge 1 commit into
Open
Conversation
A repeated mapping key is an error here, which is YAML's rule, while encoding/json takes the last occurrence (RFC 8259 says object names SHOULD be unique, so the handling is the implementation's to choose). That difference is invisible until one program decodes both formats into the same types, at which point a document loads or fails depending only on which format it happens to be written in. This exposes the existing internal switch so such a caller can pick one answer for both. The default is unchanged: repeated keys still error. Enabling the option makes the last occurrence win, matching encoding/json. Tests assert the surviving value rather than just the absence of an error, since "does not fail" would equally be satisfied by dropping the key or keeping the first, and neither matches encoding/json. They also cover the struct and generic-map decode paths separately (different code in the decoder), nesting, and that the flag does not leak between decoders.
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.
Why
A repeated mapping key is an error here, which is YAML's rule.
encoding/jsontakes the last occurrence for a repeated JSON object name — RFC 8259 says names SHOULD be unique, so the handling is left to the implementation.That difference is invisible until one program decodes both YAML and JSON into the same types. Then a document loads or fails depending only on which format it happens to be written in. Measured on the same content:
encoding/jsonmapping key "title" already defined at line 3The switch already exists internally (
decoder.uniqueKeys, hard-codedtrueinnewDecoder); it was simply never plumbed to the publicDecoder.What
Decoder.AllowDuplicateKeys(bool), wired the same way asKnownFields,OriginandDisableTimestamps.Default is unchanged — repeated keys still error. Enabled, the last occurrence wins, matching
encoding/json.Tests
Six cases. They assert the surviving value rather than only the absence of an error, since "does not fail" would equally be satisfied by dropping the key or keeping the first, and neither matches
encoding/json:mappingandmappingStructare different paths in the decoderVerified the suite actually exercises them by breaking an assertion and confirming the failure is caught (
obtained "b"), not merely that the run stays green.