Skip to content

Commit 4643caa

Browse files
committed
Merge branch '2.5'
2 parents dd955cf + 66bfe66 commit 4643caa

File tree

6 files changed

+96
-19
lines changed

6 files changed

+96
-19
lines changed

release-notes/VERSION

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,17 @@ Project: jackson-databind
1818

1919
2.5.2 (not yet released)
2020

21+
#609: Problem resolving locally declared generic type
22+
(repoted by Hal H)
2123
#691: NullSerializer for MapProperty failing when using polymorphic handling
2224
(reported by Antibrumm@github)
2325
#703: Multiple calls to ObjectMapper#canSerialize(Object.class) returns different values
2426
(reported by flexfrank@github)
2527
#705: JsonAnyGetter doesn't work with JsonSerialize (except with keyUsing)
2628
(reported by natnan@github)
29+
#728: TypeFactory#_fromVariable returns unknownType() even though it has enough information
30+
to provide a more specific type
31+
(reported by jkochaniak@github)
2732
- Improvement to handling of custom `ValueInstantiator` for delegating mode; no more NPE
2833
if `getDelegateCreator()` returns null
2934
- Refactor `TypedKey` into separate util class

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,13 @@ public int getBindingCount() {
114114
}
115115
return _bindings.size();
116116
}
117+
118+
@Deprecated // since 2.6, remove from 2.7
119+
public JavaType findType(String name) {
120+
return findType(name, true);
121+
}
117122

118-
public JavaType findType(String name)
123+
public JavaType findType(String name, boolean mustFind)
119124
{
120125
if (_bindings == null) {
121126
_resolve();
@@ -158,6 +163,10 @@ public JavaType findType(String name)
158163
*/
159164
}
160165
}
166+
167+
if (!mustFind) {
168+
return null;
169+
}
161170

162171
String className;
163172
if (_contextClass != null) {

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

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ protected JavaType _fromClass(Class<?> clz, TypeBindings context)
773773
context = new TypeBindings(this, cls);
774774
}
775775
*/
776-
776+
777777
// First: do we have an array type?
778778
if (clz.isArray()) {
779779
result = ArrayType.construct(_constructType(clz.getComponentType(), null), null, null);
@@ -876,15 +876,15 @@ protected JavaType _fromParamType(ParameterizedType type, TypeBindings context)
876876

877877
// Ok: Map or Collection?
878878
if (Map.class.isAssignableFrom(rawType)) {
879-
JavaType subtype = constructSimpleType(rawType, pt);
879+
JavaType subtype = constructSimpleType(rawType, rawType, pt);
880880
JavaType[] mapParams = findTypeParameters(subtype, Map.class);
881881
if (mapParams.length != 2) {
882882
throw new IllegalArgumentException("Could not find 2 type parameters for Map class "+rawType.getName()+" (found "+mapParams.length+")");
883883
}
884884
return MapType.construct(rawType, mapParams[0], mapParams[1]);
885885
}
886886
if (Collection.class.isAssignableFrom(rawType)) {
887-
JavaType subtype = constructSimpleType(rawType, pt);
887+
JavaType subtype = constructSimpleType(rawType, rawType, pt);
888888
JavaType[] collectionParams = findTypeParameters(subtype, Collection.class);
889889
if (collectionParams.length != 1) {
890890
throw new IllegalArgumentException("Could not find 1 type parameter for Collection class "+rawType.getName()+" (found "+collectionParams.length+")");
@@ -906,19 +906,21 @@ protected JavaType _fromArrayType(GenericArrayType type, TypeBindings context)
906906

907907
protected JavaType _fromVariable(TypeVariable<?> type, TypeBindings context)
908908
{
909-
/* 26-Sep-2009, tatus: It should be possible to try "partial"
910-
* resolution; meaning that it is ok not to find bindings.
911-
* For now this is indicated by passing null context.
912-
*/
909+
final String name = type.getName();
910+
// 19-Mar-2015: Without context, all we can check are bounds.
913911
if (context == null) {
914-
return _unknownType();
915-
}
916-
917-
// Ok: here's where context might come in handy!
918-
String name = type.getName();
919-
JavaType actualType = context.findType(name);
920-
if (actualType != null) {
921-
return actualType;
912+
// And to prevent infinite loops, now need this:
913+
context = new TypeBindings(this, (Class<?>) null);
914+
} else {
915+
// Ok: here's where context might come in handy!
916+
/* 19-Mar-2015, tatu: As per [databind#609], may need to allow
917+
* unresolved type variables to handle some cases where bounds
918+
* are enough. Let's hope it does not hide real fail cases.
919+
*/
920+
JavaType actualType = context.findType(name, false);
921+
if (actualType != null) {
922+
return actualType;
923+
}
922924
}
923925

924926
/* 29-Jan-2010, tatu: We used to throw exception here, if type was
@@ -941,7 +943,7 @@ protected JavaType _fromVariable(TypeVariable<?> type, TypeBindings context)
941943
* (T extends Comparable<T>). Need to add "placeholder"
942944
* for resolution to catch those.
943945
*/
944-
context._addPlaceholder(name);
946+
context._addPlaceholder(name);
945947
return _constructType(bounds[0], context);
946948
}
947949

src/test/java/com/fasterxml/jackson/databind/ser/TestMapSerialization.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,32 @@ static class MapWithTypedValues extends LinkedHashMap<String,String> { }
144144
@JsonTypeInfo(use = Id.CLASS)
145145
public static class Mixin691 { }
146146

147+
// For [databind#47]
148+
149+
public static class Wat
150+
{
151+
private final String wat;
152+
153+
@JsonCreator
154+
Wat(String wat) {
155+
this.wat = wat;
156+
}
157+
158+
@JsonValue
159+
public String getWat() {
160+
return wat;
161+
}
162+
163+
@Override
164+
public String toString() {
165+
return "(String)[Wat: " + wat + "]";
166+
}
167+
}
168+
169+
// For [databind#47]
170+
@SuppressWarnings("serial")
171+
static class WatMap extends HashMap<Wat,Boolean> { }
172+
147173
/*
148174
/**********************************************************
149175
/* Test methods
@@ -318,5 +344,14 @@ public void testNullJsonInTypedMap691() throws Exception {
318344
String json = mapper.writeValueAsString(map);
319345
assertEquals("{\"@class\":\"java.util.HashMap\",\"NULL\":null}", json);
320346
}
321-
}
322347

348+
public void testMapJsonValueKey47() throws Exception
349+
{
350+
WatMap input = new WatMap();
351+
input.put(new Wat("3"), true);
352+
353+
ObjectMapper mapper = new ObjectMapper();
354+
String json = mapper.writeValueAsString(input);
355+
assertEquals(aposToQuotes("{'3':true}"), json);
356+
}
357+
}

src/test/java/com/fasterxml/jackson/databind/type/TestJavaType.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.fasterxml.jackson.databind.type;
22

3+
import java.lang.reflect.Method;
34
import java.util.*;
45

56
import com.fasterxml.jackson.databind.BaseMapTest;
@@ -23,6 +24,31 @@ static enum MyEnum2 {
2324
private MyEnum2(int value) { }
2425
}
2526

27+
// [databind#728]
28+
static class Issue728 {
29+
public <C extends CharSequence> C method(C input) { return null; }
30+
}
31+
public void testLocalType728() throws Exception
32+
{
33+
TypeFactory tf = TypeFactory.defaultInstance();
34+
Method m = Issue728.class.getMethod("method", CharSequence.class);
35+
assertNotNull(m);
36+
37+
// Start with return type
38+
// first type-erased
39+
JavaType t = tf.constructType(m.getReturnType());
40+
assertEquals(CharSequence.class, t.getRawClass());
41+
// then generic
42+
t = tf.constructType(m.getGenericReturnType());
43+
assertEquals(CharSequence.class, t.getRawClass());
44+
45+
// then parameter type
46+
t = tf.constructType(m.getParameterTypes()[0]);
47+
assertEquals(CharSequence.class, t.getRawClass());
48+
t = tf.constructType(m.getGenericParameterTypes()[0]);
49+
assertEquals(CharSequence.class, t.getRawClass());
50+
}
51+
2652
/*
2753
/**********************************************************
2854
/* Test methods

src/test/java/com/fasterxml/jackson/failing/TestLocalType609.java renamed to src/test/java/com/fasterxml/jackson/databind/type/TestLocalType609.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.fasterxml.jackson.failing;
1+
package com.fasterxml.jackson.databind.type;
22

33
import com.fasterxml.jackson.databind.*;
44

0 commit comments

Comments
 (0)