From dae5440084d6cea9334ee9fe9b043b65ebc7263d Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Sat, 30 Apr 2016 14:57:03 -0700 Subject: [PATCH] Add failing test for #1215 --- .../jackson/databind/ObjectMapper.java | 2 +- .../jackson/databind/type/MapType.java | 2 +- .../failing/TypeRefinementForMap1215Test.java | 108 ++++++++++++++++++ 3 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 src/test/java/com/fasterxml/jackson/failing/TypeRefinementForMap1215Test.java diff --git a/src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java b/src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java index 5535266f41..a9f7879d46 100644 --- a/src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java +++ b/src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java @@ -3817,7 +3817,7 @@ protected Object _readMapAndClose(JsonParser jp, JavaType valueType) } catch (IOException ioe) { } } } - + /** * Method called to ensure that given parser is ready for reading * content for data binding. diff --git a/src/main/java/com/fasterxml/jackson/databind/type/MapType.java b/src/main/java/com/fasterxml/jackson/databind/type/MapType.java index e71abb6837..efaf238df2 100644 --- a/src/main/java/com/fasterxml/jackson/databind/type/MapType.java +++ b/src/main/java/com/fasterxml/jackson/databind/type/MapType.java @@ -119,7 +119,7 @@ public MapType withKeyType(JavaType keyType) { return new MapType(_class, _bindings, _superClass, _superInterfaces, keyType, _valueType, _valueHandler, _typeHandler, _asStatic); } - + @Override public JavaType refine(Class rawType, TypeBindings bindings, JavaType superClass, JavaType[] superInterfaces) { diff --git a/src/test/java/com/fasterxml/jackson/failing/TypeRefinementForMap1215Test.java b/src/test/java/com/fasterxml/jackson/failing/TypeRefinementForMap1215Test.java new file mode 100644 index 0000000000..93ec2e01ba --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/failing/TypeRefinementForMap1215Test.java @@ -0,0 +1,108 @@ +package com.fasterxml.jackson.failing; + +import java.util.LinkedHashMap; +import java.util.Map; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.BaseMapTest; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; + +public class TypeRefinementForMap1215Test extends BaseMapTest +{ + interface HasUniqueId { + K getId(); + String getIdFieldName(); + } + interface AnotherMap> extends Map { + public void add(V v); + } + + static class Item implements HasUniqueId + { + public String id; + public String property; + + @Override + public String getId() { return id; } + + @Override + public String getIdFieldName() { + return "id"; + } + } + + static class Data + { + public String id; + + @JsonProperty + private Map items; + + public Data() { } + + public Map getItems() { + return items; + } + + @JsonDeserialize(as = MyHashMap.class) + public void setItems(Map items) { + this.items = items; + } + } + + @SuppressWarnings("serial") + static class MyHashMap> + extends LinkedHashMap + implements AnotherMap + { + @JsonCreator + public static > MyHashMap fromArray(V[] values) { + MyHashMap map = new MyHashMap(); + for (int i = 0; i < values.length; i++) { + V v = values[i]; + if (v.getId() == null) { + throw new RuntimeException("Failed to get id"); + } + if (map.containsKey(v.getId())) { + throw new RuntimeException("Conflict on id"); + } + map.put(v.getId(), v); + } + return map; + } + + public MyHashMap() { } + + @Override + public void add(V v) { + if (containsKey(v.getId())) { + throw new RuntimeException("Conflict on add of id"); + } + put(v.getId(), v); + } + } + + /* + /******************************************************* + /* Test methods + /******************************************************* + */ + + public void testMapRefinement() throws Exception + { + String ID1 = "3a6383d4-8123-4c43-8b8d-7cedf3e59404"; + String ID2 = "81c3d978-90c4-4b00-8da1-1c39ffcab02c"; + String json = aposToQuotes( +"{'id':'"+ID1+"','items':[{'id':'"+ID2+"','property':'value'}]}"); + + ObjectMapper m = new ObjectMapper(); + Data data = m.readValue(json, Data.class); + + assertEquals(ID1, data.id); + assertNotNull(data.items); + assertEquals(1, data.items.size()); + assertEquals(ID2, data.items.get(0).id); + } +}