Skip to content

Commit dae5440

Browse files
committed
Add failing test for #1215
1 parent 69095e5 commit dae5440

File tree

3 files changed

+110
-2
lines changed

3 files changed

+110
-2
lines changed

src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3817,7 +3817,7 @@ protected Object _readMapAndClose(JsonParser jp, JavaType valueType)
38173817
} catch (IOException ioe) { }
38183818
}
38193819
}
3820-
3820+
38213821
/**
38223822
* Method called to ensure that given parser is ready for reading
38233823
* content for data binding.

src/main/java/com/fasterxml/jackson/databind/type/MapType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public MapType withKeyType(JavaType keyType) {
119119
return new MapType(_class, _bindings, _superClass, _superInterfaces,
120120
keyType, _valueType, _valueHandler, _typeHandler, _asStatic);
121121
}
122-
122+
123123
@Override
124124
public JavaType refine(Class<?> rawType, TypeBindings bindings,
125125
JavaType superClass, JavaType[] superInterfaces) {
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package com.fasterxml.jackson.failing;
2+
3+
import java.util.LinkedHashMap;
4+
import java.util.Map;
5+
6+
import com.fasterxml.jackson.annotation.JsonCreator;
7+
import com.fasterxml.jackson.annotation.JsonProperty;
8+
import com.fasterxml.jackson.databind.BaseMapTest;
9+
import com.fasterxml.jackson.databind.ObjectMapper;
10+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
11+
12+
public class TypeRefinementForMap1215Test extends BaseMapTest
13+
{
14+
interface HasUniqueId<K> {
15+
K getId();
16+
String getIdFieldName();
17+
}
18+
interface AnotherMap<K, V extends HasUniqueId<K>> extends Map<K, V> {
19+
public void add(V v);
20+
}
21+
22+
static class Item implements HasUniqueId<String>
23+
{
24+
public String id;
25+
public String property;
26+
27+
@Override
28+
public String getId() { return id; }
29+
30+
@Override
31+
public String getIdFieldName() {
32+
return "id";
33+
}
34+
}
35+
36+
static class Data
37+
{
38+
public String id;
39+
40+
@JsonProperty
41+
private Map<String, Item> items;
42+
43+
public Data() { }
44+
45+
public Map<String, Item> getItems() {
46+
return items;
47+
}
48+
49+
@JsonDeserialize(as = MyHashMap.class)
50+
public void setItems(Map<String, Item> items) {
51+
this.items = items;
52+
}
53+
}
54+
55+
@SuppressWarnings("serial")
56+
static class MyHashMap<K, V extends HasUniqueId<K>>
57+
extends LinkedHashMap<K, V>
58+
implements AnotherMap<K, V>
59+
{
60+
@JsonCreator
61+
public static <K, V extends HasUniqueId<K>> MyHashMap<K, V> fromArray(V[] values) {
62+
MyHashMap<K, V> map = new MyHashMap<K, V>();
63+
for (int i = 0; i < values.length; i++) {
64+
V v = values[i];
65+
if (v.getId() == null) {
66+
throw new RuntimeException("Failed to get id");
67+
}
68+
if (map.containsKey(v.getId())) {
69+
throw new RuntimeException("Conflict on id");
70+
}
71+
map.put(v.getId(), v);
72+
}
73+
return map;
74+
}
75+
76+
public MyHashMap() { }
77+
78+
@Override
79+
public void add(V v) {
80+
if (containsKey(v.getId())) {
81+
throw new RuntimeException("Conflict on add of id");
82+
}
83+
put(v.getId(), v);
84+
}
85+
}
86+
87+
/*
88+
/*******************************************************
89+
/* Test methods
90+
/*******************************************************
91+
*/
92+
93+
public void testMapRefinement() throws Exception
94+
{
95+
String ID1 = "3a6383d4-8123-4c43-8b8d-7cedf3e59404";
96+
String ID2 = "81c3d978-90c4-4b00-8da1-1c39ffcab02c";
97+
String json = aposToQuotes(
98+
"{'id':'"+ID1+"','items':[{'id':'"+ID2+"','property':'value'}]}");
99+
100+
ObjectMapper m = new ObjectMapper();
101+
Data data = m.readValue(json, Data.class);
102+
103+
assertEquals(ID1, data.id);
104+
assertNotNull(data.items);
105+
assertEquals(1, data.items.size());
106+
assertEquals(ID2, data.items.get(0).id);
107+
}
108+
}

0 commit comments

Comments
 (0)