Skip to content

Commit 67ba365

Browse files
committed
Fix #3108
1 parent e0fd026 commit 67ba365

File tree

7 files changed

+77
-6
lines changed

7 files changed

+77
-6
lines changed

release-notes/VERSION-2.x

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ Project: jackson-databind
66

77
2.12.3 (not yet released)
88

9+
#3108: `TypeFactory` cannot convert `Collection` sub-type without type parameters
10+
to canonical form and back
11+
(reported by lbilger@github)
912
- Fix for [modules-java8#207]: prevent fail on secondary Java 8 date/time types
1013

1114
2.12.2 (03-Mar-2021)

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,9 @@ public StringBuilder getGenericSignature(StringBuilder sb) {
212212
protected String buildCanonicalName() {
213213
StringBuilder sb = new StringBuilder();
214214
sb.append(_class.getName());
215-
if (_elementType != null) {
215+
// 10-Apr-2021, tatu: [databind#3108] Ensure we have at least nominally
216+
// compatible type declaration (weak guarantee but better than nothing)
217+
if ((_elementType != null) && _hasNTypeParameters(1)) {
216218
sb.append('<');
217219
sb.append(_elementType.toCanonical());
218220
sb.append('>');

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,9 @@ public JavaType refine(Class<?> rawType, TypeBindings bindings,
189189
protected String buildCanonicalName() {
190190
StringBuilder sb = new StringBuilder();
191191
sb.append(_class.getName());
192-
if (_keyType != null) {
192+
// 10-Apr-2021, tatu: [databind#3108] Ensure we have at least nominally
193+
// compatible type declaration (weak guarantee but better than nothing)
194+
if ((_keyType != null) && _hasNTypeParameters(2)) {
193195
sb.append('<');
194196
sb.append(_keyType.toCanonical());
195197
sb.append(',');

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,11 @@ protected String buildCanonicalName()
164164
{
165165
StringBuilder sb = new StringBuilder();
166166
sb.append(_class.getName());
167-
sb.append('<');
168-
sb.append(_referencedType.toCanonical());
169-
sb.append('>');
167+
if ((_referencedType != null) && _hasNTypeParameters(1)) {
168+
sb.append('<');
169+
sb.append(_referencedType.toCanonical());
170+
sb.append('>');
171+
}
170172
return sb.toString();
171173
}
172174

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,10 @@ protected String buildCanonicalName()
219219
sb.append(_class.getName());
220220

221221
final int count = _bindings.size();
222-
if (count > 0) {
222+
223+
// 10-Apr-2021, tatu: [databind#3108] Ensure we have at least nominally
224+
// compatible type declaration (weak guarantee but better than nothing)
225+
if ((count > 0) && _hasNTypeParameters(count)) {
223226
sb.append('<');
224227
for (int i = 0; i < count; ++i) {
225228
JavaType t = containedType(i);

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

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

33
import java.io.IOException;
4+
import java.lang.reflect.TypeVariable;
45
import java.util.*;
56

67
import com.fasterxml.jackson.core.JsonGenerator;
@@ -254,4 +255,9 @@ protected static JavaType _bogusSuperClass(Class<?> cls) {
254255
}
255256
return TypeFactory.unknownType();
256257
}
258+
259+
protected boolean _hasNTypeParameters(int count) {
260+
TypeVariable<?>[] params = _class.getTypeParameters();
261+
return (params.length == count);
262+
}
257263
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.fasterxml.jackson.databind.type;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
6+
import com.fasterxml.jackson.databind.BaseMapTest;
7+
import com.fasterxml.jackson.databind.JavaType;
8+
9+
// [databind#3108]: canonical type description for non-generic subtypes
10+
@SuppressWarnings("serial")
11+
public class TestTypeFactory3108
12+
extends BaseMapTest
13+
{
14+
static class StringList3108 extends ArrayList<String> {}
15+
16+
static class StringStringMap3108 extends HashMap<String, String> {}
17+
18+
static class ParamType3108<T> {}
19+
20+
static class ConcreteType3108 extends ParamType3108<Integer> {}
21+
22+
// [databind#3108] with custom Collection
23+
public void testCanonicalWithCustomCollection()
24+
{
25+
final TypeFactory tf = TypeFactory.defaultInstance();
26+
JavaType stringListType = tf.constructType(StringList3108.class);
27+
String canonical = stringListType.toCanonical();
28+
JavaType type = tf.constructFromCanonical(canonical);
29+
assertEquals(StringList3108.class, type.getRawClass());
30+
assertTrue(type.isCollectionLikeType());
31+
}
32+
33+
// [databind#3108] with custom Map
34+
public void testCanonicalWithCustomMap()
35+
{
36+
final TypeFactory tf = TypeFactory.defaultInstance();
37+
JavaType stringListType = tf.constructType(StringStringMap3108.class);
38+
String canonical = stringListType.toCanonical();
39+
JavaType type = tf.constructFromCanonical(canonical);
40+
assertEquals(StringStringMap3108.class, type.getRawClass());
41+
assertTrue(type.isMapLikeType());
42+
}
43+
44+
// [databind#3108] with custom generic type
45+
public void testCanonicalWithCustomGenericType()
46+
{
47+
final TypeFactory tf = TypeFactory.defaultInstance();
48+
JavaType stringListType = tf.constructType(ConcreteType3108.class);
49+
String canonical = stringListType.toCanonical();
50+
JavaType type = tf.constructFromCanonical(canonical);
51+
assertEquals(ConcreteType3108.class, type.getRawClass());
52+
}
53+
}

0 commit comments

Comments
 (0)