3
3
import java .util .ArrayList ;
4
4
import java .util .List ;
5
5
6
+ import com .fasterxml .jackson .annotation .JsonCreator ;
6
7
import com .fasterxml .jackson .annotation .JsonIdentityInfo ;
8
+ import com .fasterxml .jackson .annotation .JsonProperty ;
9
+ import com .fasterxml .jackson .annotation .JsonSubTypes ;
7
10
import com .fasterxml .jackson .annotation .JsonTypeInfo ;
8
11
import com .fasterxml .jackson .annotation .ObjectIdGenerators ;
12
+
9
13
import com .fasterxml .jackson .dataformat .xml .XmlMapper ;
10
14
import com .fasterxml .jackson .dataformat .xml .XmlTestBase ;
11
15
@@ -47,30 +51,35 @@ public Wrapper(){}
47
51
public Wrapper (List <TypeWithClassPropertyAndObjectId > data ) { this .data = data ; }
48
52
}
49
53
50
- /*
51
- /**********************************************************
52
- /* Set up
53
- /**********************************************************
54
- */
54
+ // [dataformat-xml#451]
55
+ @ JsonTypeInfo (use = JsonTypeInfo .Id .DEDUCTION )
56
+ @ JsonSubTypes (@ JsonSubTypes .Type (Child451 .class ))
57
+ public interface Value451 {}
55
58
56
- protected XmlMapper _xmlMapper ;
59
+ public static class Child451 implements Value451 {
60
+ private final String property1 ;
57
61
58
- // let's actually reuse XmlMapper to make things bit faster
59
- @ Override
60
- public void setUp () throws Exception {
61
- super .setUp ();
62
- _xmlMapper = new XmlMapper ();
63
- }
62
+ @ JsonCreator (mode = JsonCreator .Mode .PROPERTIES )
63
+ public Child451 (@ JsonProperty ("property1" ) String property1 ) {
64
+ this .property1 = property1 ;
65
+ }
66
+
67
+ public String getProperty1 () {
68
+ return property1 ;
69
+ }
70
+ }
64
71
65
72
/*
66
- /**********************************************************
67
- /* Unit tests
68
- /**********************************************************
73
+ /**********************************************************************
74
+ /* Test methods
75
+ /**********************************************************************
69
76
*/
70
77
78
+ private final XmlMapper MAPPER = newMapper ();
79
+
71
80
public void testAsClassProperty () throws Exception
72
81
{
73
- String xml = _xmlMapper .writeValueAsString (new SubTypeWithClassProperty ("Foobar" ));
82
+ String xml = MAPPER .writeValueAsString (new SubTypeWithClassProperty ("Foobar" ));
74
83
75
84
// Type info should be written as an attribute, so:
76
85
final String exp =
@@ -80,36 +89,46 @@ public void testAsClassProperty() throws Exception
80
89
;
81
90
assertEquals (exp , xml );
82
91
83
- Object result = _xmlMapper .readValue (xml , BaseTypeWithClassProperty .class );
92
+ Object result = MAPPER .readValue (xml , BaseTypeWithClassProperty .class );
84
93
assertNotNull (result );
85
94
assertEquals (SubTypeWithClassProperty .class , result .getClass ());
86
95
assertEquals ("Foobar" , ((SubTypeWithClassProperty ) result ).name );
87
96
}
88
97
89
98
public void testAsClassObject () throws Exception
90
99
{
91
- String xml = _xmlMapper .writeValueAsString (new SubTypeWithClassObject ("Foobar" ));
92
- Object result = _xmlMapper .readValue (xml , BaseTypeWithClassObject .class );
100
+ String xml = MAPPER .writeValueAsString (new SubTypeWithClassObject ("Foobar" ));
101
+ Object result = MAPPER .readValue (xml , BaseTypeWithClassObject .class );
93
102
assertNotNull (result );
94
103
assertEquals (SubTypeWithClassObject .class , result .getClass ());
95
104
assertEquals ("Foobar" , ((SubTypeWithClassObject ) result ).name );
96
105
}
97
106
98
- /**
99
- * Test for [dataformat-xml#81]
100
- */
107
+ // Test for [dataformat-xml#81]
101
108
public void testAsPropertyWithObjectId () throws Exception
102
109
{
103
110
List <TypeWithClassPropertyAndObjectId > data = new ArrayList <PolymorphicTypesTest .TypeWithClassPropertyAndObjectId >();
104
111
TypeWithClassPropertyAndObjectId object = new TypeWithClassPropertyAndObjectId ("Foobar" );
105
112
data .add (object );
106
113
// This will be written as an id reference instead of object; as such, no type info will be written.
107
114
data .add (object );
108
- String xml = _xmlMapper .writeValueAsString (new Wrapper (data ));
109
- Wrapper result = _xmlMapper .readValue (xml , Wrapper .class );
115
+ String xml = MAPPER .writeValueAsString (new Wrapper (data ));
116
+ Wrapper result = MAPPER .readValue (xml , Wrapper .class );
110
117
assertNotNull (result );
111
118
assertSame (result .data .get (0 ), result .data .get (1 ));
112
119
assertEquals ("Foobar" , result .data .get (0 ).id );
113
120
}
121
+
122
+ // Test for [dataformat-xml#451]
123
+ public void testDeduction () throws Exception
124
+ {
125
+ String xml = MAPPER .writeValueAsString (new Child451 ("value1" ));
126
+ assertTrue (xml .contains ("<property1>value1</property1>" ));
127
+
128
+ // and try reading back for funsies
129
+ Value451 result = MAPPER .readValue (xml , Value451 .class );
130
+ assertNotNull (result );
131
+ assertEquals (Child451 .class , result .getClass ());
132
+ }
114
133
}
115
134
0 commit comments