Commit f4d371d
authored
Fix duplicate key detection when first value is null (#3006)
* Fix duplicate key detection when first value is null
MapTypeAdapterFactory detected duplicate map keys by checking the
return value of Map.put(): `if (replaced != null)`. This missed
duplicates where the first value was null, because Map.put() returns
null (the previous value) which passed the != null check.
For example, {"a":null,"a":1} was silently accepted as {a=1} instead
of throwing JsonSyntaxException("duplicate key: a").
Fix: check Map.containsKey() before put() instead of relying on the
return value of put(). This correctly detects duplicates regardless
of whether the first value is null.
Fixes both the object form and the array-of-entries form
(enableComplexMapKeySerialization).
* Apply spotless formatting1 parent 27d9ba1 commit f4d371d
3 files changed
Lines changed: 33 additions & 4 deletions
File tree
- gson/src
- main/java/com/google/gson/internal/bind
- test/java/com/google/gson/functional
Lines changed: 4 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
188 | 188 | | |
189 | 189 | | |
190 | 190 | | |
191 | | - | |
192 | | - | |
| 191 | + | |
193 | 192 | | |
194 | 193 | | |
| 194 | + | |
195 | 195 | | |
196 | 196 | | |
197 | 197 | | |
| |||
201 | 201 | | |
202 | 202 | | |
203 | 203 | | |
204 | | - | |
205 | | - | |
| 204 | + | |
206 | 205 | | |
207 | 206 | | |
| 207 | + | |
208 | 208 | | |
209 | 209 | | |
210 | 210 | | |
| |||
Lines changed: 10 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
80 | 80 | | |
81 | 81 | | |
82 | 82 | | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
83 | 93 | | |
84 | 94 | | |
85 | 95 | | |
| |||
Lines changed: 19 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
693 | 693 | | |
694 | 694 | | |
695 | 695 | | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
696 | 715 | | |
697 | 716 | | |
698 | 717 | | |
| |||
0 commit comments