Skip to content

Commit 925c7c1

Browse files
committed
Fixed #2796 (or part of it at least)
1 parent a8182ea commit 925c7c1

File tree

4 files changed

+61
-30
lines changed

4 files changed

+61
-30
lines changed

release-notes/CREDITS-2.x

+4
Original file line numberDiff line numberDiff line change
@@ -1140,3 +1140,7 @@ Joshua Shannon (retrodaredevil@github)
11401140
* Reported, contributed fix for #2785: Polymorphic subtypes not registering on copied
11411141
ObjectMapper (2.11.1)
11421142
(2.11.2)
1143+
1144+
Daniel Hrabovcak (TheSpiritXIII@github)
1145+
* Reported #2796: `TypeFactory.constructType()` does not take `TypeBindings` correctly
1146+
(2.11.2)

release-notes/VERSION-2.x

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Project: jackson-databind
1010
`createGenerator()`
1111
#2785: Polymorphic subtypes not registering on copied ObjectMapper (2.11.1)
1212
(reported, fix contributed by Joshua S)
13+
#2796: `TypeFactory.constructType()` does not take `TypeBindings` correctly
14+
(reported by Daniel H)
1315

1416
2.11.1 (25-Jun-2020)
1517

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

+41-28
Original file line numberDiff line numberDiff line change
@@ -699,9 +699,15 @@ public JavaType constructType(Type type) {
699699
}
700700

701701
public JavaType constructType(Type type, TypeBindings bindings) {
702+
// 15-Jun-2020, tatu: To resolve (parts of) [databind#2796], need to
703+
// call _fromClass() directly if we get `Class` argument
704+
if (type instanceof Class<?>) {
705+
JavaType resultType = _fromClass(null, (Class<?>) type, bindings);
706+
return _applyModifiers(type, resultType);
707+
}
702708
return _fromAny(null, type, bindings);
703709
}
704-
710+
705711
public JavaType constructType(TypeReference<?> typeRef)
706712
{
707713
// 19-Oct-2015, tatu: Simpler variant like so should work
@@ -1274,51 +1280,58 @@ protected JavaType _findWellKnownSimple(Class<?> clz) {
12741280
* as Java typing returned from <code>getGenericXxx</code> methods
12751281
* (usually for a return or argument type).
12761282
*/
1277-
protected JavaType _fromAny(ClassStack context, Type type, TypeBindings bindings)
1283+
protected JavaType _fromAny(ClassStack context, Type srcType, TypeBindings bindings)
12781284
{
12791285
JavaType resultType;
12801286

12811287
// simple class?
1282-
if (type instanceof Class<?>) {
1288+
if (srcType instanceof Class<?>) {
12831289
// Important: remove possible bindings since this is type-erased thingy
1284-
resultType = _fromClass(context, (Class<?>) type, EMPTY_BINDINGS);
1290+
resultType = _fromClass(context, (Class<?>) srcType, EMPTY_BINDINGS);
12851291
}
12861292
// But if not, need to start resolving.
1287-
else if (type instanceof ParameterizedType) {
1288-
resultType = _fromParamType(context, (ParameterizedType) type, bindings);
1293+
else if (srcType instanceof ParameterizedType) {
1294+
resultType = _fromParamType(context, (ParameterizedType) srcType, bindings);
12891295
}
1290-
else if (type instanceof JavaType) { // [databind#116]
1296+
else if (srcType instanceof JavaType) { // [databind#116]
12911297
// no need to modify further if we already had JavaType
1292-
return (JavaType) type;
1298+
return (JavaType) srcType;
12931299
}
1294-
else if (type instanceof GenericArrayType) {
1295-
resultType = _fromArrayType(context, (GenericArrayType) type, bindings);
1300+
else if (srcType instanceof GenericArrayType) {
1301+
resultType = _fromArrayType(context, (GenericArrayType) srcType, bindings);
12961302
}
1297-
else if (type instanceof TypeVariable<?>) {
1298-
resultType = _fromVariable(context, (TypeVariable<?>) type, bindings);
1303+
else if (srcType instanceof TypeVariable<?>) {
1304+
resultType = _fromVariable(context, (TypeVariable<?>) srcType, bindings);
12991305
}
1300-
else if (type instanceof WildcardType) {
1301-
resultType = _fromWildcard(context, (WildcardType) type, bindings);
1306+
else if (srcType instanceof WildcardType) {
1307+
resultType = _fromWildcard(context, (WildcardType) srcType, bindings);
13021308
} else {
13031309
// sanity check
1304-
throw new IllegalArgumentException("Unrecognized Type: "+((type == null) ? "[null]" : type.toString()));
1310+
throw new IllegalArgumentException("Unrecognized Type: "+((srcType == null) ? "[null]" : srcType.toString()));
13051311
}
13061312
// 21-Feb-2016, nateB/tatu: as per [databind#1129] (applied for 2.7.2),
13071313
// we do need to let all kinds of types to be refined, esp. for Scala module.
1308-
if (_modifiers != null) {
1309-
TypeBindings b = resultType.getBindings();
1310-
if (b == null) {
1311-
b = EMPTY_BINDINGS;
1312-
}
1313-
for (TypeModifier mod : _modifiers) {
1314-
JavaType t = mod.modifyType(resultType, type, b, this);
1315-
if (t == null) {
1316-
throw new IllegalStateException(String.format(
1317-
"TypeModifier %s (of type %s) return null for type %s",
1318-
mod, mod.getClass().getName(), resultType));
1319-
}
1320-
resultType = t;
1314+
return _applyModifiers(srcType, resultType);
1315+
}
1316+
1317+
protected JavaType _applyModifiers(Type srcType, JavaType resolvedType)
1318+
{
1319+
if (_modifiers == null) {
1320+
return resolvedType;
1321+
}
1322+
JavaType resultType = resolvedType;
1323+
TypeBindings b = resultType.getBindings();
1324+
if (b == null) {
1325+
b = EMPTY_BINDINGS;
1326+
}
1327+
for (TypeModifier mod : _modifiers) {
1328+
JavaType t = mod.modifyType(resultType, srcType, b, this);
1329+
if (t == null) {
1330+
throw new IllegalStateException(String.format(
1331+
"TypeModifier %s (of type %s) return null for type %s",
1332+
mod, mod.getClass().getName(), resultType));
13211333
}
1334+
resultType = t;
13221335
}
13231336
return resultType;
13241337
}

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

+14-2
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ public void testCanonicalWithSpaces()
278278
public void testCollections()
279279
{
280280
// Ok, first: let's test what happens when we pass 'raw' Collection:
281-
TypeFactory tf = TypeFactory.defaultInstance();
281+
final TypeFactory tf = TypeFactory.defaultInstance();
282282
JavaType t = tf.constructType(ArrayList.class);
283283
assertEquals(CollectionType.class, t.getClass());
284284
assertSame(ArrayList.class, t.getRawClass());
@@ -299,7 +299,19 @@ public void testCollections()
299299
assertEquals(CollectionType.class, t.getClass());
300300
assertSame(String.class, ((CollectionType) t).getContentType().getRawClass());
301301
}
302-
302+
303+
// [databind#2796]
304+
public void testCollectionsWithBindings()
305+
{
306+
final TypeFactory tf = TypeFactory.defaultInstance();
307+
TypeBindings tb = TypeBindings.create(Set.class, new JavaType[] {
308+
tf.constructType(String.class) });
309+
JavaType t = tf.constructType(ArrayList.class, tb);
310+
assertEquals(CollectionType.class, t.getClass());
311+
assertSame(ArrayList.class, t.getRawClass());
312+
assertSame(String.class, ((CollectionType) t).getContentType().getRawClass());
313+
}
314+
303315
// since 2.7
304316
public void testCollectionTypesRefined()
305317
{

0 commit comments

Comments
 (0)