-
Notifications
You must be signed in to change notification settings - Fork 4.4k
fix: MapTypeAdapterFactory rejects duplicate keys on write (mirror of #3006 read-side) #3036
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
Closed
andrewstellman
wants to merge
2
commits into
google:main
from
andrewstellman:qpb/bug-002-map-duplicate-keys-write
+74
−2
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
58 changes: 58 additions & 0 deletions
58
gson/src/test/java/com/google/gson/regression/MapTypeAdapterDuplicateKeyTest.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| // Generated by Quality Playbook v1.5.8 — https://github.com/andrewstellman/quality-playbook | ||
| // Author: Andrew Stellman · Date: 2026-06-04 · Project: gson | ||
| package com.google.gson.regression; | ||
|
|
||
| import static com.google.common.truth.Truth.assertThat; | ||
|
|
||
| import com.google.gson.Gson; | ||
| import com.google.gson.JsonSyntaxException; | ||
| import com.google.gson.reflect.TypeToken; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.Map; | ||
| import org.junit.Test; | ||
|
|
||
| /** Regression test: map serialization must not silently emit duplicate JSON member names. */ | ||
| public class MapTypeAdapterDuplicateKeyTest { | ||
| @Test | ||
| public void mapWriteDoesNotEmitDuplicateNames() { | ||
| Gson gson = new Gson(); | ||
| Object k1 = | ||
| new Object() { | ||
| @Override | ||
| public String toString() { | ||
| return "k"; | ||
| } | ||
| }; | ||
| Object k2 = | ||
| new Object() { | ||
| @Override | ||
| public String toString() { | ||
| return "k"; | ||
| } | ||
| }; | ||
| Map<Object, String> map = new LinkedHashMap<>(); | ||
| map.put(k1, "a"); | ||
| map.put(k2, "b"); | ||
| // Confirm two distinct anonymous-instance keys (defensive against future key-type refactors). | ||
| assertThat(map).hasSize(2); | ||
|
|
||
| String json; | ||
| try { | ||
| json = gson.toJson(map, new TypeToken<Map<Object, String>>() {}.getType()); | ||
| } catch (JsonSyntaxException expected) { | ||
| // Acceptable fix: throw JsonSyntaxException naming the duplicate key. Narrow match — | ||
| // message must reference "duplicate key:" followed by the literal colliding name "k", | ||
| // not merely contain the word "key" from any unrelated JSON syntax error. | ||
| assertThat(expected).hasMessageThat().containsMatch("duplicate key:\\s*k(?:\\W|$)"); | ||
| return; | ||
| } | ||
|
|
||
| // Otherwise: gson emitted JSON without throwing. Pin the object-form serialization (not the | ||
| // complex-key array-form) so the indexOf "k": substring check is exercising the bug's locus. | ||
| assertThat(json).startsWith("{"); | ||
| int first = json.indexOf("\"k\":"); | ||
| assertThat(first).isAtLeast(0); | ||
| int second = json.indexOf("\"k\":", first + 1); | ||
| assertThat(second).isEqualTo(-1); | ||
|
Comment on lines
+40
to
+56
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If throwing an exception is the desired and expected behavior now, then the test should probably reflect that and use |
||
| } | ||
| } | ||
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.
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.
Might be good to also cover the complex map key scenario which you changed as well:
["k"]),toString()is identical (e.g."k") but the duplicate keys are not rejectedWhat do you think?
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.
You are 100% right – I hyperfocused on a corner case. Thank you! Closing this PR.