Skip to content

Commit fd3926b

Browse files
committed
Add test for (now) fixed #301
1 parent ab310eb commit fd3926b

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

release-notes/VERSION-2.x

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Project: jackson-dataformat-xml
1111
(reported by Eric S)
1212
#294: XML parser error with nested same element names
1313
(reported by Alexei V)
14+
#301: Problem deserializing POJO with unwrapped `List`, ignorable attribute value
1415
#393: `MismatchedInputException` for nested repeating element name in `List`
1516
(reported by kaizenHorse@github)
1617
#399: Can not deserialize unwrapped list when `@JacksonXmlProperty` localName matches

src/test/java/com/fasterxml/jackson/dataformat/xml/lists/ListWithAttributes.java

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.*;
44

5+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
56
import com.fasterxml.jackson.annotation.JsonProperty;
67
import com.fasterxml.jackson.databind.*;
78
import com.fasterxml.jackson.dataformat.xml.*;
@@ -40,7 +41,7 @@ public static class Value {
4041
public int id;
4142
}
4243

43-
// [Issue#108]: unwrapped lists, more than one entry, id attributes
44+
// [dataformat-xml#108]: unwrapped lists, more than one entry, id attributes
4445

4546
static class Foo {
4647
@JacksonXmlElementWrapper(useWrapping = false)
@@ -56,21 +57,45 @@ static class Bar {
5657
public int id;
5758
}
5859

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+
5983
/*
6084
/**********************************************************
6185
/* Test methods
6286
/**********************************************************
6387
*/
6488

89+
private final ObjectMapper MAPPER = newMapper();
90+
6591
// [Issue#43]
6692
public void testIssue43() throws Exception
6793
{
6894
String xmlData = "<roomName><names>"
6995
+"<name language=\"en\">SPECIAL</name>"
7096
+"</names></roomName>";
7197

72-
XmlMapper xmlMapper = new XmlMapper();
73-
RoomName roomName = xmlMapper.readValue(xmlData, RoomName.class);
98+
RoomName roomName = MAPPER.readValue(xmlData, RoomName.class);
7499
assertEquals(1, roomName.names.size());
75100
assertEquals("SPECIAL", roomName.names.get(0).text);
76101
}
@@ -82,16 +107,15 @@ public void testListWithAttributes() throws Exception
82107
+ " <value id=\"1\"/>"
83108
+ " <fail/>"
84109
+ "</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);
88113
assertNotNull(root.values);
89114
assertEquals(1, root.values.size());
90115
}
91116

92117
// [Issue#108]: unwrapped lists, more than one entry, id attributes
93118
public void testIdsFromAttributes() throws Exception {
94-
XmlMapper xmlMapper = new XmlMapper();
95119
Foo foo = new Foo();
96120
Bar bar1 = new Bar();
97121
bar1.id = 1;
@@ -101,8 +125,23 @@ public void testIdsFromAttributes() throws Exception {
101125
bar2.value = "SECOND";
102126
bar2.id = 2;
103127
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);
106130
assertEquals(foo.secondBar.get(0).id, fooRead.secondBar.get(0).id);
107131
}
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+
}
108146
}
147+

0 commit comments

Comments
 (0)