4
4
5
5
import com .fasterxml .jackson .databind .*;
6
6
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
+ */
7
15
@ SuppressWarnings ("serial" )
8
16
public class SubTypeResolution1964Test extends BaseMapTest
9
17
{
10
18
static class AccessModel {
11
- private Map <Object , Collection <String >> repositoryPrivileges ;
12
-
19
+ private Map <String , Collection <String >> repositoryPrivileges ;
20
+
13
21
public AccessModel () {
14
22
repositoryPrivileges = new HashMap <>();
15
23
}
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 () {
18
28
return repositoryPrivileges ;
19
29
}
20
-
21
- public void setRepositoryPrivileges (Map <Object , Collection <String >> repositoryPrivileges ) {
30
+
31
+ public void setRepositoryPrivileges (Map <String , Collection <String >> repositoryPrivileges ) {
22
32
this .repositoryPrivileges = repositoryPrivileges ;
23
33
}
24
34
}
@@ -27,7 +37,10 @@ static class CustomMap<T> extends LinkedHashMap<Object, T> { }
27
37
28
38
public void testTypeCompatibility1964 () throws Exception
29
39
{
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 ();
31
44
String key = "/storages/storage0/releases" ;
32
45
Collection <String > values = new HashSet <>();
33
46
values .add ("ARTIFACTS_RESOLVE" );
@@ -38,6 +51,7 @@ public void testTypeCompatibility1964() throws Exception
38
51
39
52
ObjectMapper mapper = new ObjectMapper ();
40
53
String jsonStr = mapper .writeValueAsString (accessModel );
54
+ // ... could/should verify more, perhaps, but for now let it be.
41
55
assertNotNull (jsonStr );
42
56
}
43
57
}
0 commit comments