Skip to content

Commit 58a6fb5

Browse files
committed
Fix #405
1 parent 3da7073 commit 58a6fb5

File tree

3 files changed

+47
-9
lines changed

3 files changed

+47
-9
lines changed

release-notes/VERSION-2.x

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Project: jackson-dataformat-xml
1919
(reported by Ghenadii B)
2020
#397: `XmlReadContext` does not keep track of array index
2121
#403: Make `JsonNode` implicitly create `ArrayNode`s for repeated XML Elements
22+
#405: Mixed content not exposed through `FromXmlParser`, lost by `JsonNode`
2223

2324
2.11.1 (not yet released)
2425

src/test/java/com/fasterxml/jackson/dataformat/xml/deser/JsonNodeBasicDeserTest.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import com.fasterxml.jackson.databind.JsonNode;
44
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import com.fasterxml.jackson.databind.ObjectWriter;
6+
import com.fasterxml.jackson.databind.json.JsonMapper;
57
import com.fasterxml.jackson.databind.node.JsonNodeType;
6-
8+
import com.fasterxml.jackson.databind.node.ObjectNode;
79
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;
810

911
public class JsonNodeBasicDeserTest extends XmlTestBase
@@ -30,4 +32,22 @@ public void testRepeated() throws Exception
3032
assertEquals("a", root.at("/value/0").asText());
3133
assertEquals("b", root.at("/value/1").asText());
3234
}
35+
36+
// [dataformat-xml#405]: support mixed content
37+
public void testMixedContent() throws Exception
38+
{
39+
JsonNode fromXml = XML_MAPPER.readTree("<root>first<a>123</a>second<b>abc</b>last</root>");
40+
final ObjectNode exp = XML_MAPPER.createObjectNode();
41+
exp.putArray("")
42+
.add("first")
43+
.add("second")
44+
.add("last");
45+
exp.put("a", "123");
46+
exp.put("b", "abc");
47+
48+
if (!fromXml.equals(exp)) {
49+
ObjectWriter w = new JsonMapper().writerWithDefaultPrettyPrinter();
50+
fail("Expected:\n"+w.writeValueAsString(exp)+"\ngot:\n"+w.writeValueAsString(fromXml));
51+
}
52+
}
3353
}

src/test/java/com/fasterxml/jackson/dataformat/xml/deser/UntypedObjectDeser205Test.java renamed to src/test/java/com/fasterxml/jackson/dataformat/xml/deser/UntypedObjectDeserTest.java

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@
88
import com.fasterxml.jackson.databind.node.ObjectNode;
99
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;
1010

11-
// for [dataformat-xml#205], handling "untyped" ({@code java.lang.Object}-targeted)
12-
// deserialization, including handling of element sequences
13-
public class UntypedObjectDeser205Test extends XmlTestBase
11+
public class UntypedObjectDeserTest extends XmlTestBase
1412
{
1513
private final ObjectMapper XML_MAPPER = newMapper();
1614

17-
private final ObjectMapper JSON_MAPPER = new JsonMapper();
18-
15+
// for [dataformat-xml#205], handling "untyped" ({@code java.lang.Object}-targeted)
16+
// deserialization, including handling of element sequences
1917
public void testRepeatingElements() throws Exception
2018
{
2119
final String XML =
@@ -39,8 +37,8 @@ public void testRepeatingElements() throws Exception
3937
" </dog>\n" +
4038
" </dogs>\n" +
4139
"</person>";
42-
final JsonNode fromXml = JSON_MAPPER.valueToTree(XML_MAPPER.readValue(XML, Object.class));
43-
final ObjectNode exp = JSON_MAPPER.createObjectNode();
40+
final JsonNode fromXml = XML_MAPPER.valueToTree(XML_MAPPER.readValue(XML, Object.class));
41+
final ObjectNode exp = XML_MAPPER.createObjectNode();
4442
exp.put("name", "John");
4543
{
4644
exp.putArray("parent")
@@ -60,7 +58,26 @@ public void testRepeatingElements() throws Exception
6058
.put("age", "14");
6159
}
6260
if (!fromXml.equals(exp)) {
63-
ObjectWriter w = JSON_MAPPER.writerWithDefaultPrettyPrinter();
61+
ObjectWriter w = new JsonMapper().writerWithDefaultPrettyPrinter();
62+
fail("Expected:\n"+w.writeValueAsString(exp)+"\ngot:\n"+w.writeValueAsString(fromXml));
63+
}
64+
}
65+
66+
// [dataformat-xml#405]: support mixed content
67+
public void testMixedContent() throws Exception
68+
{
69+
final String XML = "<root>first<a>123</a>second<b>abc</b>last</root>";
70+
final JsonNode fromXml = XML_MAPPER.valueToTree(XML_MAPPER.readValue(XML, Object.class));
71+
final ObjectNode exp = XML_MAPPER.createObjectNode();
72+
exp.putArray("")
73+
.add("first")
74+
.add("second")
75+
.add("last");
76+
exp.put("a", "123");
77+
exp.put("b", "abc");
78+
79+
if (!fromXml.equals(exp)) {
80+
ObjectWriter w = new JsonMapper().writerWithDefaultPrettyPrinter();
6481
fail("Expected:\n"+w.writeValueAsString(exp)+"\ngot:\n"+w.writeValueAsString(fromXml));
6582
}
6683
}

0 commit comments

Comments
 (0)