fix: don't let case-insensitive fallback reuse an exactly-matched key#192
Open
koriyoshi2041 wants to merge 1 commit into
Open
fix: don't let case-insensitive fallback reuse an exactly-matched key#192koriyoshi2041 wants to merge 1 commit into
koriyoshi2041 wants to merge 1 commit into
Conversation
When a struct has fields whose names differ only by case (e.g. `name` and `NAME`) and the input map contains one of them (e.g. "name"), the field with the exact match consumed the key and the case-insensitive fallback then matched the same key again, populating both fields. Skip keys that another field already removed from the unused set so each map key is consumed by at most one field.
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.
Fixes #75.
When a struct has two fields whose names differ only by case, decoding a map that contains one of them populates both fields:
In
decodeStructFromMap, theNamefield matches the key"name"exactly and removes it fromdataValKeysUnused. TheNAMEfield has no exact key, so it falls into the case-insensitive search — which iterates all keys and matches"name"again viaMatchName, even though it was already consumed. (encoding/jsongives the value only to the exact match.)The fix skips keys that are no longer in
dataValKeysUnusedduring the case-insensitive fallback, so each map key is claimed by at most one field, and the exact match wins.Added
TestDecode_caseInsensitiveDuplicateKey; it fails before the fix (NAMEis"lower") and passes after. ExistingTestDecode*tests still pass.