Skip to content

Commit 1bef91c

Browse files
committed
Fixed #1456
1 parent 422c4cc commit 1bef91c

File tree

4 files changed

+37
-9
lines changed

4 files changed

+37
-9
lines changed

release-notes/CREDITS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,9 @@ Dmitry Spikhalskiy (Spikhalskiy@github)
247247
* Reported #731, suggested the way to fix it: XmlAdapter result marshaling error in
248248
case of ValueType=Object
249249
(2.5.3)
250+
* Reported #1456: `TypeFactory` type resolution broken in 2.7 for generic types
251+
when using `constructType` with context
252+
(2.7.9 / 2.8.6)
250253

251254
John Meyer (jpmeyer@github)
252255
* Reported, contributed fix for #745: EnumDeserializer.deserializerForCreator() fails

release-notes/VERSION

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ Project: jackson-databind
1313
#1432: Off by 1 bug in PropertyValueBuffer
1414
(reported by Kevin D)
1515
#1439: NPE when using with filter id, serializing `java.util.Map` types
16+
#1456: `TypeFactory` type resolution broken in 2.7 for generic types
17+
when using `constructType` with context
18+
(reported by Dmitry S)
1619

1720
2.7.8 (26-Sep-2016)
1821

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -621,18 +621,36 @@ public JavaType constructType(TypeReference<?> typeRef)
621621
*/
622622
@Deprecated
623623
public JavaType constructType(Type type, Class<?> contextClass) {
624-
TypeBindings bindings = (contextClass == null)
625-
? TypeBindings.emptyBindings() : constructType(contextClass).getBindings();
626-
return _fromAny(null, type, bindings);
624+
JavaType contextType = (contextClass == null) ? null : constructType(contextClass);
625+
return constructType(type, contextType);
627626
}
628627

629628
/**
630629
* @deprecated Since 2.7 (accidentally removed in 2.7.0; added back in 2.7.1)
631630
*/
632631
@Deprecated
633632
public JavaType constructType(Type type, JavaType contextType) {
634-
TypeBindings bindings = (contextType == null)
635-
? TypeBindings.emptyBindings() : contextType.getBindings();
633+
TypeBindings bindings;
634+
if (contextType == null) {
635+
bindings = TypeBindings.emptyBindings();
636+
} else {
637+
bindings = contextType.getBindings();
638+
// 16-Nov-2016, tatu: Unfortunately as per [databind#1456] this can't
639+
// be made to work for some cases used to work (even if accidentally);
640+
// however, we can try a simple heuristic to increase chances of
641+
// compatibility from 2.6 code
642+
if (type.getClass() != Class.class) {
643+
// Ok: so, ideally we would test super-interfaces if necessary;
644+
// but let's assume most if not all cases are for classes.
645+
while (bindings.isEmpty()) {
646+
contextType = contextType.getSuperClass();
647+
if (contextType == null) {
648+
break;
649+
}
650+
bindings = contextType.getBindings();
651+
}
652+
}
653+
}
636654
return _fromAny(null, type, bindings);
637655
}
638656

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

Lines changed: 8 additions & 4 deletions
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 java.lang.reflect.Method;
44
import java.lang.reflect.Type;
@@ -8,7 +8,9 @@
88
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
99
import com.fasterxml.jackson.databind.introspect.AnnotatedParameter;
1010

11-
public class GenericParameterTypeFactory1456Test extends BaseMapTest
11+
// Tests for [databind#1456]: resolution using methods deprecated
12+
// in 2.7, but used to work in 2.6
13+
public class DeprecatedConstructType1456Test extends BaseMapTest
1214
{
1315
public static class BaseController<Entity extends BaseEntity> {
1416
public void process(Entity entity) {}
@@ -21,8 +23,9 @@ public static class BaseEntity {}
2123
public static class ImplEntity extends BaseEntity {}
2224

2325
private final ObjectMapper MAPPER = new ObjectMapper();
24-
25-
public void testGenericParameterDirect() throws Exception
26+
27+
@SuppressWarnings("deprecation")
28+
public void testGenericResolutionUsingDeprecated() throws Exception
2629
{
2730
Method proceed = BaseController.class.getMethod("process", BaseEntity.class);
2831
Type entityType = proceed.getGenericParameterTypes()[0];
@@ -31,6 +34,7 @@ public void testGenericParameterDirect() throws Exception
3134
assertEquals(ImplEntity.class, resolvedType.getRawClass());
3235
}
3336

37+
// and this is how new code should resolve types if at all possible
3438
public void testGenericParameterViaClass() throws Exception
3539
{
3640
BeanDescription desc = MAPPER.getDeserializationConfig().introspect(

0 commit comments

Comments
 (0)