Skip to content

Commit de6ca25

Browse files
committed
Fix #1964
1 parent 10f2ce3 commit de6ca25

File tree

4 files changed

+33
-8
lines changed

4 files changed

+33
-8
lines changed

release-notes/VERSION-2.x

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

99
#1565: Deserialization failure with Polymorphism using JsonTypeInfo `defaultImpl`,
1010
subtype as target
11+
#1964: Failed to specialize `Map` type during serialization where key type
12+
incompatibility overidden via "raw" types
13+
(reported by ptirador@github)
1114
#1998: Removing "type" attribute with Mixin not taken in account if
1215
using ObjectMapper.copy()
1316
(reported by SBKila@github)

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,15 @@ 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+
// 19-Apr-2018, tatu: Hack for [databind#1964] -- allow type demotion
459+
// for `java.util.Map` key type if (and only if) target type is
460+
// `java.lang.Object`
461+
if (i == 0) {
462+
if (sourceType.hasRawClass(Map.class)
463+
&& act.hasRawClass(Object.class)) {
464+
continue;
465+
}
466+
}
458467
return String.format("Type parameter #%d/%d differs; can not specialize %s with %s",
459468
(i+1), len, exp.toCanonical(), act.toCanonical());
460469
}

src/test/java/com/fasterxml/jackson/databind/mixins/MapperMixinsCopy1998Test.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ public void testB_KO() throws Exception
108108
.setConfig(myObjectMapper.getSerializationConfig().withView(MyModelView.class));
109109

110110
String result = getString(myModelInstance, myObjectMapper);
111-
System.out.println("result: "+result);
112111
assertEquals(EXPECTED, result);
113112

114113
}

src/test/java/com/fasterxml/jackson/failing/SubTypeResolution1964Test.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,31 @@
44

55
import com.fasterxml.jackson.databind.*;
66

7+
/**
8+
* Test for [databind#1964], wherein slightly incompatible type hierarchy,
9+
* where `Map` key is downcast from `String` to `Object` (via use of "raw"
10+
* types to force compiler to ignore incompatibility) causes exception
11+
* during serialization. Although ideally code would not force round peg
12+
* through square hole, it makes sense to add specific exception to allow
13+
* such downcast just for Map key types (for now at least).
14+
*/
715
@SuppressWarnings("serial")
816
public class SubTypeResolution1964Test extends BaseMapTest
917
{
1018
static class AccessModel {
11-
private Map<Object, Collection<String>> repositoryPrivileges;
12-
19+
private Map<String, Collection<String>> repositoryPrivileges;
20+
1321
public AccessModel() {
1422
repositoryPrivileges = new HashMap<>();
1523
}
16-
17-
public Map<Object, Collection<String>> getRepositoryPrivileges() {
24+
25+
// 19-Apr-2018, tatu; this would prevent issues
26+
// @JsonSerialize(typing = JsonSerialize.Typing.STATIC)
27+
public Map<String, Collection<String>> getRepositoryPrivileges() {
1828
return repositoryPrivileges;
1929
}
20-
21-
public void setRepositoryPrivileges(Map<Object, Collection<String>> repositoryPrivileges) {
30+
31+
public void setRepositoryPrivileges(Map<String, Collection<String>> repositoryPrivileges) {
2232
this.repositoryPrivileges = repositoryPrivileges;
2333
}
2434
}
@@ -27,7 +37,10 @@ static class CustomMap<T> extends LinkedHashMap<Object, T> { }
2737

2838
public void testTypeCompatibility1964() throws Exception
2939
{
30-
Map<Object, Collection<String>> repoPrivilegesMap = new CustomMap<>();
40+
// Important! Must use raw type since assignment requires effectively
41+
// casting due incompatible type parameters.
42+
@SuppressWarnings("unchecked")
43+
Map<String, Collection<String>> repoPrivilegesMap = new CustomMap();
3144
String key = "/storages/storage0/releases";
3245
Collection<String> values = new HashSet<>();
3346
values.add("ARTIFACTS_RESOLVE");
@@ -38,6 +51,7 @@ public void testTypeCompatibility1964() throws Exception
3851

3952
ObjectMapper mapper = new ObjectMapper();
4053
String jsonStr = mapper.writeValueAsString(accessModel);
54+
// ... could/should verify more, perhaps, but for now let it be.
4155
assertNotNull(jsonStr);
4256
}
4357
}

0 commit comments

Comments
 (0)