Skip to content

Commit

Permalink
Fixed #1456
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Nov 17, 2016
1 parent 422c4cc commit 1bef91c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
3 changes: 3 additions & 0 deletions release-notes/CREDITS
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ Dmitry Spikhalskiy (Spikhalskiy@github)
* Reported #731, suggested the way to fix it: XmlAdapter result marshaling error in
case of ValueType=Object
(2.5.3)
* Reported #1456: `TypeFactory` type resolution broken in 2.7 for generic types
when using `constructType` with context
(2.7.9 / 2.8.6)

John Meyer (jpmeyer@github)
* Reported, contributed fix for #745: EnumDeserializer.deserializerForCreator() fails
Expand Down
3 changes: 3 additions & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ Project: jackson-databind
#1432: Off by 1 bug in PropertyValueBuffer
(reported by Kevin D)
#1439: NPE when using with filter id, serializing `java.util.Map` types
#1456: `TypeFactory` type resolution broken in 2.7 for generic types
when using `constructType` with context
(reported by Dmitry S)

2.7.8 (26-Sep-2016)

Expand Down
28 changes: 23 additions & 5 deletions src/main/java/com/fasterxml/jackson/databind/type/TypeFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -621,18 +621,36 @@ public JavaType constructType(TypeReference<?> typeRef)
*/
@Deprecated
public JavaType constructType(Type type, Class<?> contextClass) {
TypeBindings bindings = (contextClass == null)
? TypeBindings.emptyBindings() : constructType(contextClass).getBindings();
return _fromAny(null, type, bindings);
JavaType contextType = (contextClass == null) ? null : constructType(contextClass);
return constructType(type, contextType);
}

/**
* @deprecated Since 2.7 (accidentally removed in 2.7.0; added back in 2.7.1)
*/
@Deprecated
public JavaType constructType(Type type, JavaType contextType) {
TypeBindings bindings = (contextType == null)
? TypeBindings.emptyBindings() : contextType.getBindings();
TypeBindings bindings;
if (contextType == null) {
bindings = TypeBindings.emptyBindings();
} else {
bindings = contextType.getBindings();
// 16-Nov-2016, tatu: Unfortunately as per [databind#1456] this can't
// be made to work for some cases used to work (even if accidentally);
// however, we can try a simple heuristic to increase chances of
// compatibility from 2.6 code
if (type.getClass() != Class.class) {
// Ok: so, ideally we would test super-interfaces if necessary;
// but let's assume most if not all cases are for classes.
while (bindings.isEmpty()) {
contextType = contextType.getSuperClass();
if (contextType == null) {
break;
}
bindings = contextType.getBindings();
}
}
}
return _fromAny(null, type, bindings);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.fasterxml.jackson.failing;
package com.fasterxml.jackson.databind.type;

import java.lang.reflect.Method;
import java.lang.reflect.Type;
Expand All @@ -8,7 +8,9 @@
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
import com.fasterxml.jackson.databind.introspect.AnnotatedParameter;

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

private final ObjectMapper MAPPER = new ObjectMapper();

public void testGenericParameterDirect() throws Exception

@SuppressWarnings("deprecation")
public void testGenericResolutionUsingDeprecated() throws Exception
{
Method proceed = BaseController.class.getMethod("process", BaseEntity.class);
Type entityType = proceed.getGenericParameterTypes()[0];
Expand All @@ -31,6 +34,7 @@ public void testGenericParameterDirect() throws Exception
assertEquals(ImplEntity.class, resolvedType.getRawClass());
}

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

0 comments on commit 1bef91c

Please sign in to comment.