Skip to content

Commit bfeb1fa

Browse files
committed
Fix #2034
1 parent 78e7873 commit bfeb1fa

File tree

5 files changed

+66
-5
lines changed

5 files changed

+66
-5
lines changed

release-notes/CREDITS-2.x

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,3 +788,7 @@ Jakub Skierbiszewski (jskierbi@github)
788788
Carter Kozak (cakofony@github)
789789
* Reported #2016: Delegating JsonCreator disregards JsonDeserialize info
790790
(2.9.6)
791+
792+
Reinhard Prechtl (dnno@github)
793+
* Reported #2034: Serialization problem with type specialization of nested generic types
794+
(2.9.6)

release-notes/VERSION-2.x

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ Project: jackson-databind
3535
(reported by franboragina@github)
3636
#2032: Blacklist another serialization gadget (ibatis)
3737
(reported by Guixiong Wu)
38+
#2034: Serialization problem with type specialization of nested generic types
39+
(reported by Reinhard P)
3840

3941
2.9.5 (26-Mar-2018)
4042

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,12 @@ private String _resolveTypePlaceholders(JavaType sourceType, JavaType actualType
455455
JavaType exp = expectedTypes.get(i);
456456
JavaType act = actualTypes.get(i);
457457
if (!_verifyAndResolvePlaceholders(exp, act)) {
458+
// 14-May-2018, tatu: As per [databind#2034] it seems we better relax assignment
459+
// rules further -- at least likely "raw" (untyped, non-generic) base should probably
460+
// allow specialization.
461+
if (exp.hasRawClass(Object.class)) {
462+
continue;
463+
}
458464
// 19-Apr-2018, tatu: Hack for [databind#1964] -- allow type demotion
459465
// for `java.util.Map` key type if (and only if) target type is
460466
// `java.lang.Object`

src/test/java/com/fasterxml/jackson/databind/jsontype/SubTypeResolution1964Test.java

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.*;
44

5+
import com.fasterxml.jackson.annotation.JsonProperty;
56
import com.fasterxml.jackson.databind.*;
67

78
/**
@@ -15,6 +16,7 @@
1516
@SuppressWarnings("serial")
1617
public class SubTypeResolution1964Test extends BaseMapTest
1718
{
19+
// [databind#1964]
1820
static class AccessModel {
1921
private Map<String, Collection<String>> repositoryPrivileges;
2022

@@ -32,9 +34,47 @@ public void setRepositoryPrivileges(Map<String, Collection<String>> repositoryPr
3234
this.repositoryPrivileges = repositoryPrivileges;
3335
}
3436
}
35-
3637
static class CustomMap<T> extends LinkedHashMap<Object, T> { }
3738

39+
// [databind#2034]: specialization from `Object` to other types probably should
40+
// just be allowed (at least in serialization case)
41+
interface Dummy {
42+
List<String> getStrings();
43+
}
44+
45+
static class MetaModel<M, B> extends AbstractMetaValue<M, M, B> {
46+
47+
@JsonProperty
48+
protected final Map<String, AbstractMetaValue<M, ?, B>> attributes = new HashMap<>();
49+
50+
public <V> ListMetaAttribute<M, V, B> describeList(final String attributeName) {
51+
final ListMetaAttribute<M, V, B> metaAttribute = new ListMetaAttribute<>();
52+
attributes.put(attributeName, metaAttribute);
53+
return metaAttribute;
54+
}
55+
}
56+
57+
static abstract class AbstractMetaValue<M, V, B> {
58+
public int getBogus() { return 3; }
59+
}
60+
61+
static class ListMetaAttribute<M, V, B> extends MetaAttribute<M, List<V>, B> {
62+
public ListMetaAttribute() { }
63+
}
64+
65+
static class MetaAttribute<M, V, B> extends AbstractMetaValue<M, V, B> {
66+
public MetaAttribute() { }
67+
}
68+
69+
/*
70+
/**********************************************************************
71+
/* Unit tests
72+
/**********************************************************************
73+
*/
74+
75+
final ObjectMapper MAPPER = newObjectMapper();
76+
77+
// [databind#1964]
3878
public void testTypeCompatibility1964() throws Exception
3979
{
4080
// Important! Must use raw type since assignment requires effectively
@@ -49,8 +89,17 @@ public void testTypeCompatibility1964() throws Exception
4989
AccessModel accessModel = new AccessModel();
5090
accessModel.setRepositoryPrivileges(repoPrivilegesMap);
5191

52-
ObjectMapper mapper = new ObjectMapper();
53-
String jsonStr = mapper.writeValueAsString(accessModel);
92+
String jsonStr = MAPPER.writeValueAsString(accessModel);
93+
// ... could/should verify more, perhaps, but for now let it be.
94+
assertNotNull(jsonStr);
95+
}
96+
97+
// [databind#2034]
98+
public void testTypeSpecialization2034() throws Exception
99+
{
100+
MetaModel<Dummy, Dummy> metaModel = new MetaModel<>();
101+
metaModel.describeList("a1");
102+
String jsonStr = MAPPER.writeValueAsString(metaModel);
54103
// ... could/should verify more, perhaps, but for now let it be.
55104
assertNotNull(jsonStr);
56105
}

src/test/java/com/fasterxml/jackson/databind/jsontype/TestScalars.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ public ScalarList add(Object v) {
4242
return this;
4343
}
4444
}
45-
45+
4646
/*
4747
/**********************************************************
4848
/* Unit tests
4949
/**********************************************************
5050
*/
5151

52-
final ObjectMapper MAPPER = new ObjectMapper();
52+
final ObjectMapper MAPPER = newObjectMapper();
5353

5454
/**
5555
* Ensure that per-property dynamic types work, both for "native" types

0 commit comments

Comments
 (0)