fix(traits): resolve JSON trait key collisions deterministically - #67
Merged
Conversation
v0.17.0 started normalizing **traits JSON keys, which lets two keys
differing only in case land on one trait. The merge then wrote both
through a map, so Go's randomized iteration order decided the survivor:
parsing {"Tap":true,"tap":false} 200 times returned false 172 times and
true 28 times.
There is no member order to break the tie by. JSON arguments are
normalized through a map in parseJSONArg before the traits merge sees
them, so the object arrives with its keys already sorted. Drop a
contested trait instead of picking a winner, which stays correct however
that upstream normalization behaves.
Also fix the AGENTS.md example, which redeclared an existing err with :=.
|
Warning Review limit reachedNext included review available in 40 minutes. View limit detailsLimit details: You’ve used the included review currently available. You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Review configuration: ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (3)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
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.
Addresses both CodeRabbit findings on #66.
The bug
v0.17.0 started normalizing
**traitsJSON keys, which lets two keys differing only in case land on one trait. The merge wrote both through a map, so Go's randomized iteration order decided which value survived. Parsing**traits:{"Tap":true,"tap":false}200 times returnedfalse172 times andtrue28 times — same input, different result.This is a regression from #66. Before it, the JSON path stored keys verbatim, so those stayed two distinct keys and the outcome was stable.
Why not preserve JSON member order
CodeRabbit suggested preserving member order and applying last-wins, to match the inline
#keysyntax. That isn't available:parseJSONArg(arguments.go:71-80) unmarshals every JSON argument intoanyand re-marshals it, which sorts object keys alphabetically. Member order is gone before the traits merge ever runs —{"Tap":true,"tap":false,"TAP":"last"}arrives as{"TAP":"last","Tap":true,"tap":false}.So a contested trait is dropped rather than resolved. That is order-independent, which means it stays correct however that upstream normalization behaves, and it matches how the parser already treats contradictory input elsewhere. Uncontested keys in the same object are unaffected.
Also
AGENTS.mdredeclared an existingerrwith:=in the new example; it now uses a scopedif err := ...; err != nil.Tests
TestParseTraitsJSONKeyCollisionIsDeterministic— 200 parses per input, asserting a stable result. Fails on v0.17.0.TestParseTraitsJSONKeyCollisionDropsTrait— the contested trait is dropped, uncontested keys survive.TestParseTraitsJSONDecoding— null, empty object, value types, and non-object JSON keep their existing behavior.task lint-fix0 issues,task test(race) passing,FuzzParseScriptclean for 30s.