1
1
package com .fasterxml .jackson .databind .jsontype .ext ;
2
2
3
- import com .fasterxml .jackson .annotation .JsonCreator ;
4
- import com .fasterxml .jackson .annotation .JsonSubTypes ;
5
- import com .fasterxml .jackson .annotation .JsonTypeInfo ;
6
-
3
+ import com .fasterxml .jackson .annotation .*;
4
+ import com .fasterxml .jackson .core .type .TypeReference ;
7
5
import com .fasterxml .jackson .databind .*;
8
6
9
- // [databind#1198]
10
7
public class ExternalTypeIdWithCreatorTest extends BaseMapTest
11
8
{
9
+ // [databind#1198]
10
+
12
11
public enum Attacks { KICK , PUNCH }
13
12
14
13
static class Character {
@@ -47,8 +46,46 @@ public Punch(String side) {
47
46
}
48
47
}
49
48
50
- final ObjectMapper MAPPER = new ObjectMapper ();
49
+ // [databind#999]
50
+
51
+ public static interface Payload999 { }
52
+
53
+ @ JsonTypeName ("foo" )
54
+ public static class FooPayload999 implements Payload999 { }
55
+
56
+ @ JsonTypeName ("bar" )
57
+ public static class BarPayload999 implements Payload999 { }
58
+
59
+ public static class Message <P extends Payload999 >
60
+ {
61
+ final String type ;
62
+
63
+ @ JsonTypeInfo (use = JsonTypeInfo .Id .NAME ,
64
+ visible = true ,
65
+ include = JsonTypeInfo .As .EXTERNAL_PROPERTY , property = "type" )
66
+ @ JsonSubTypes ({
67
+ @ JsonSubTypes .Type (FooPayload999 .class ),
68
+ @ JsonSubTypes .Type (BarPayload999 .class ) })
69
+ private final P payload ;
70
+
71
+ @ JsonCreator
72
+ public Message (@ JsonProperty ("type" ) String type ,
73
+ @ JsonProperty ("payload" ) P payload )
74
+ {
75
+ this .type = type ;
76
+ this .payload = payload ;
77
+ }
78
+ }
79
+
80
+ /*
81
+ /**********************************************************************
82
+ /* Test methods
83
+ /**********************************************************************
84
+ */
85
+
86
+ private final ObjectMapper MAPPER = new ObjectMapper ();
51
87
88
+ // [databind#1198]
52
89
public void testFails () throws Exception {
53
90
String json = "{ \" name\" : \" foo\" , \" attack\" :\" right\" } }" ;
54
91
@@ -59,6 +96,7 @@ public void testFails() throws Exception {
59
96
assertEquals ("foo" , character .name );
60
97
}
61
98
99
+ // [databind#1198]
62
100
public void testWorks () throws Exception {
63
101
String json = "{ \" name\" : \" foo\" , \" preferredAttack\" : \" KICK\" , \" attack\" :\" right\" } }" ;
64
102
@@ -68,4 +106,21 @@ public void testWorks() throws Exception {
68
106
assertNotNull (character .attack );
69
107
assertEquals ("foo" , character .name );
70
108
}
109
+
110
+ // [databind#999]
111
+ public void testExternalTypeId () throws Exception
112
+ {
113
+ TypeReference <Message <FooPayload999 >> type = new TypeReference <Message <FooPayload999 >>() { };
114
+
115
+ Message <?> msg = MAPPER .readValue (aposToQuotes ("{ 'type':'foo', 'payload': {} }" ), type );
116
+ assertNotNull (msg );
117
+ assertNotNull (msg .payload );
118
+ assertEquals ("foo" , msg .type );
119
+
120
+ // and then with different order
121
+ msg = MAPPER .readValue (aposToQuotes ("{'payload': {}, 'type':'foo' }" ), type );
122
+ assertNotNull (msg );
123
+ assertNotNull (msg .payload );
124
+ assertEquals ("foo" , msg .type );
125
+ }
71
126
}
0 commit comments