Skip to content

Commit fcdc96d

Browse files
committed
Fixed #314 and #390 (same root cause)
1 parent 38ffd8a commit fcdc96d

File tree

7 files changed

+127
-141
lines changed

7 files changed

+127
-141
lines changed

release-notes/CREDITS-2.x

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,23 @@ Joseph Petersen (jetersen@github)
9292
* Reported #273: Input mismatch with case-insensitive properties
9393
(2.12.0)
9494

95-
Ghenadii Batalski (ghenadiibatalski@github)
95+
Eduard Wirch (ewirch@github)
9696

97-
* Reported #377: `ToXmlGenerator` ignores `Base64Variant` while serializing `byte[]`
97+
* Reported #314: Jackson gets confused by parent list element
9898
(2.12.0)
9999

100100
Jochen Schalanda (joschi@github)
101101

102102
* Reported #318: XMLMapper fails to deserialize null (POJO reference) from blank tag
103103
(2.12.0)
104+
105+
Ghenadii Batalski (ghenadiibatalski@github)
106+
107+
* Reported #377: `ToXmlGenerator` ignores `Base64Variant` while serializing `byte[]`
108+
(2.12.0)
109+
110+
David Schmidt (d-schmidt@github)
111+
112+
* Reported #390: Unexpected attribute at string fields causes extra objects to be
113+
created in parent list
114+
(2.12.0)

release-notes/VERSION-2.x

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,17 @@ Project: jackson-dataformat-xml
1717
(requested by Dave J)
1818
#273: Input mismatch with case-insensitive properties
1919
(reported by Joseph P)
20+
#314: Jackson gets confused by parent list element
21+
(reported by Eduard W)
2022
#318: XMLMapper fails to deserialize null (POJO reference) from blank tag
2123
(reported by Jochen S)
2224
#319: Empty root tag into `List` deserialization bug
2325
(reported by Seatec13@github)
2426
#377: `ToXmlGenerator` ignores `Base64Variant` while serializing `byte[]`
2527
(reported by Ghenadii B)
28+
#390: Unexpected attribute at string fields causes extra objects to be
29+
created in parent list
30+
(reported by David S
2631
#397: `XmlReadContext` does not keep track of array index
2732
#403: Make `JsonNode` implicitly create `ArrayNode`s for repeated XML Elements
2833
#405: Mixed content not exposed through `FromXmlParser`, lost by `JsonNode`

src/main/java/com/fasterxml/jackson/dataformat/xml/deser/FromXmlParser.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ public boolean isExpectedStartArrayToken()
490490
_xmlTokens.skipAttributes();
491491
return true;
492492
}
493-
//System.out.println(" isExpectedArrayStart?: t="+t);
493+
//System.out.println(" FromXmlParser.isExpectedArrayStart?: t="+t);
494494
return (t == JsonToken.START_ARRAY);
495495
}
496496

@@ -887,6 +887,7 @@ public String getValueAsString(String defValue) throws IOException
887887
_nextToken = null;
888888
// One more thing: must explicitly skip the END_OBJECT that would follow
889889
_skipEndElement();
890+
//System.out.println(" FromXmlParser.getValueAsString() on START_OBJECT, str == '"+str+"'");
890891
return (_currText = str);
891892
}
892893
} catch (XMLStreamException e) {

src/main/java/com/fasterxml/jackson/dataformat/xml/deser/XmlTokenStream.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -358,9 +358,11 @@ protected String convertToString() throws XMLStreamException
358358
if (text == null) {
359359
text = "";
360360
}
361-
if (_currentWrapper != null) {
362-
_currentWrapper = _currentWrapper.getParent();
363-
}
361+
// 06-Jun-2020, tatu: As per [dataformat-xml#390], doing this is wrong,
362+
// should not (at least always?) assume we need it
363+
// if (_currentWrapper != null) {
364+
// _currentWrapper = _currentWrapper.getParent();
365+
// }
364366
// just for diagnostics, reset to element name (from first attribute name)
365367
_localName = _xmlReader.getLocalName();
366368
_namespaceURI = _xmlReader.getNamespaceURI();

src/test/java/com/fasterxml/jackson/dataformat/xml/failing/ListDeser314Test.java

Lines changed: 0 additions & 83 deletions
This file was deleted.

src/test/java/com/fasterxml/jackson/dataformat/xml/failing/ListDeser390Test.java

Lines changed: 0 additions & 51 deletions
This file was deleted.

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

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
66
import com.fasterxml.jackson.annotation.JsonProperty;
7+
78
import com.fasterxml.jackson.databind.*;
9+
810
import com.fasterxml.jackson.dataformat.xml.*;
911
import com.fasterxml.jackson.dataformat.xml.annotation.*;
1012

11-
public class ListWithAttributes extends XmlTestBase
13+
public class ListWithAttributesDeserTest extends XmlTestBase
1214
{
1315
// [dataformat-xml#43]
1416
static class Name {
@@ -79,6 +81,43 @@ static class ChildB301 {
7981
public Double value;
8082
}
8183

84+
// [dataformat-xml#314]
85+
static class Customer314 {
86+
@JacksonXmlElementWrapper(localName = "Customer", useWrapping = false)
87+
@JacksonXmlProperty(localName = "Address")
88+
public List<Address314> address;
89+
}
90+
91+
static class Address314 {
92+
public String stateProv;
93+
public CountryName314 countryName;
94+
}
95+
96+
static class CountryName314 {
97+
public String code;
98+
@JacksonXmlText
99+
public String name;
100+
}
101+
102+
// [dataformat-xml#390]
103+
@JacksonXmlRootElement(localName = "many")
104+
static class Many390 {
105+
@JacksonXmlProperty(localName = "one")
106+
@JacksonXmlElementWrapper(useWrapping = false)
107+
List<One390> ones;
108+
}
109+
110+
static class One390 {
111+
@JacksonXmlProperty
112+
String value;
113+
@JacksonXmlProperty
114+
String another;
115+
116+
public void setAnother(String s) {
117+
another = s;
118+
}
119+
}
120+
82121
/*
83122
/**********************************************************
84123
/* Test methods
@@ -87,6 +126,10 @@ static class ChildB301 {
87126

88127
private final ObjectMapper MAPPER = newMapper();
89128

129+
private final ObjectMapper UPPER_CASE_MAPPER = mapperBuilder()
130+
.propertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE)
131+
.build();
132+
90133
// [dataformat-xml#43]
91134
public void testIssue43() throws Exception
92135
{
@@ -142,5 +185,63 @@ public void testIssue301WithAttr() throws Exception {
142185
assertEquals(1, result.childrenB.size());
143186
assertEquals(Double.valueOf(12.25), result.childrenB.get(0).value);
144187
}
188+
189+
// [dataformat-xml#314]
190+
public void testDeser314Order1() throws Exception
191+
{
192+
String content = ""
193+
+ "<Customer>\n"
194+
+ " <Address>\n"
195+
+ " <StateProv StateCode='DE-NI'>Niedersachsen</StateProv>\n"
196+
+ " <CountryName Code='DE'>Deutschland</CountryName>\n"
197+
+ " </Address>\n"
198+
+ "</Customer>"
199+
;
200+
Customer314 result = UPPER_CASE_MAPPER.readValue(content, Customer314.class);
201+
assertNotNull(result);
202+
}
203+
204+
public void testDeser314Order2() throws Exception
205+
{
206+
String content = ""
207+
+ "<Customer>\n"
208+
+ " <Address>\n"
209+
+ " <CountryName Code='DE'>Deutschland</CountryName>\n"
210+
+ " <StateProv StateCode='DE-NI'>Niedersachsen</StateProv>\n"
211+
+ " </Address>\n"
212+
+ "</Customer>"
213+
;
214+
Customer314 result = UPPER_CASE_MAPPER.readValue(content, Customer314.class);
215+
assertNotNull(result);
216+
}
217+
218+
public void testDeser314Address() throws Exception
219+
{
220+
String content = ""
221+
+ " <Address>\n"
222+
+ " <CountryName Code=\"DE\">Deutschland</CountryName>\n"
223+
+ " <StateProv StateCode=\"DE-NI\">Niedersachsen</StateProv>\n"
224+
+ " </Address>\n"
225+
;
226+
Address314 result = UPPER_CASE_MAPPER.readValue(content, Address314.class);
227+
assertNotNull(result);
228+
}
229+
230+
// [dataformat-xml#390]
231+
public void testDeser390() throws Exception
232+
{
233+
String XML = "<many>\n"
234+
+ " <one>\n"
235+
+ " <value bar=\"baz\">foo</value>\n"
236+
+ " <another>stuff</another>\n"
237+
+ " </one>\n"
238+
+ "</many>";
239+
Many390 many = MAPPER.readValue(XML, Many390.class);
240+
assertNotNull(many.ones);
241+
//System.err.println("XML:\n"+MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(many));
242+
assertEquals(1, many.ones.size());
243+
assertEquals("foo", many.ones.get(0).value);
244+
assertEquals("stuff", many.ones.get(0).another);
245+
}
145246
}
146247

0 commit comments

Comments
 (0)