2
2
3
3
import java .util .*;
4
4
5
+ import com .fasterxml .jackson .annotation .JsonIgnoreProperties ;
5
6
import com .fasterxml .jackson .annotation .JsonProperty ;
6
7
import com .fasterxml .jackson .databind .*;
7
8
import com .fasterxml .jackson .dataformat .xml .*;
@@ -40,7 +41,7 @@ public static class Value {
40
41
public int id ;
41
42
}
42
43
43
- // [Issue #108]: unwrapped lists, more than one entry, id attributes
44
+ // [dataformat-xml #108]: unwrapped lists, more than one entry, id attributes
44
45
45
46
static class Foo {
46
47
@ JacksonXmlElementWrapper (useWrapping = false )
@@ -56,21 +57,45 @@ static class Bar {
56
57
public int id ;
57
58
}
58
59
60
+ // [dataformat-xml#301]: mismatched attribute to skip
61
+ static class Parent301 {
62
+ @ JacksonXmlProperty (localName = "MY_ATTR" , isAttribute = true )
63
+ public String myAttribute ;
64
+
65
+ @ JacksonXmlElementWrapper (useWrapping = false )
66
+ @ JacksonXmlProperty (localName = "MY_PROPERTY" )
67
+ public List <ChildA301 > childrenA = new ArrayList <>();
68
+
69
+ @ JacksonXmlElementWrapper (useWrapping = false )
70
+ @ JacksonXmlProperty (localName = "CHILDB" )
71
+ public List <ChildB301 > childrenB = new ArrayList <>();
72
+
73
+ }
74
+
75
+ static class ChildA301 { }
76
+
77
+ @ JsonIgnoreProperties (ignoreUnknown = true )
78
+ static class ChildB301 {
79
+ @ JacksonXmlProperty (localName = "MY_PROPERTY" )
80
+ public Double value ;
81
+ }
82
+
59
83
/*
60
84
/**********************************************************
61
85
/* Test methods
62
86
/**********************************************************
63
87
*/
64
88
89
+ private final ObjectMapper MAPPER = newMapper ();
90
+
65
91
// [Issue#43]
66
92
public void testIssue43 () throws Exception
67
93
{
68
94
String xmlData = "<roomName><names>"
69
95
+"<name language=\" en\" >SPECIAL</name>"
70
96
+"</names></roomName>" ;
71
97
72
- XmlMapper xmlMapper = new XmlMapper ();
73
- RoomName roomName = xmlMapper .readValue (xmlData , RoomName .class );
98
+ RoomName roomName = MAPPER .readValue (xmlData , RoomName .class );
74
99
assertEquals (1 , roomName .names .size ());
75
100
assertEquals ("SPECIAL" , roomName .names .get (0 ).text );
76
101
}
@@ -82,16 +107,15 @@ public void testListWithAttributes() throws Exception
82
107
+ " <value id=\" 1\" />"
83
108
+ " <fail/>"
84
109
+ "</Root>" ;
85
- ObjectMapper mapper = new XmlMapper ( )
86
- .disable (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES );
87
- Root root = mapper .readValue (source , Root .class );
110
+ ObjectReader r = MAPPER . readerFor ( Root . class )
111
+ .without (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES );
112
+ Root root = r .readValue (source , Root .class );
88
113
assertNotNull (root .values );
89
114
assertEquals (1 , root .values .size ());
90
115
}
91
116
92
117
// [Issue#108]: unwrapped lists, more than one entry, id attributes
93
118
public void testIdsFromAttributes () throws Exception {
94
- XmlMapper xmlMapper = new XmlMapper ();
95
119
Foo foo = new Foo ();
96
120
Bar bar1 = new Bar ();
97
121
bar1 .id = 1 ;
@@ -101,8 +125,23 @@ public void testIdsFromAttributes() throws Exception {
101
125
bar2 .value = "SECOND" ;
102
126
bar2 .id = 2 ;
103
127
foo .secondBar .add (bar2 );
104
- String string = xmlMapper .writeValueAsString (foo );
105
- Foo fooRead = xmlMapper .readValue (string , Foo .class );
128
+ String string = MAPPER .writeValueAsString (foo );
129
+ Foo fooRead = MAPPER .readValue (string , Foo .class );
106
130
assertEquals (foo .secondBar .get (0 ).id , fooRead .secondBar .get (0 ).id );
107
131
}
132
+
133
+ public void testIssue301WithAttr () throws Exception {
134
+ final String XML =
135
+ "<PARENT>" +
136
+ " <CHILDB MY_ATTR='TEST_VALUE'>" +
137
+ " <MY_PROPERTY>12.25</MY_PROPERTY>" +
138
+ " </CHILDB>" +
139
+ "</PARENT>" ;
140
+ Parent301 result = MAPPER .readValue (XML , Parent301 .class );
141
+ assertNotNull (result );
142
+ assertNotNull (result .childrenB );
143
+ assertEquals (1 , result .childrenB .size ());
144
+ assertEquals (Double .valueOf (12.25 ), result .childrenB .get (0 ).value );
145
+ }
108
146
}
147
+
0 commit comments