Skip to content

Commit a9bee65

Browse files
committed
Merge branch '2.12' into 2.13
2 parents 4faaf00 + e7300de commit a9bee65

File tree

4 files changed

+165
-6
lines changed

4 files changed

+165
-6
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package com.fasterxml.jackson.dataformat.xml.failing;
2+
3+
import java.util.*;
4+
5+
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
6+
7+
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
8+
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;
9+
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
10+
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
11+
12+
// Actual reproduction of [dataformat-xml#469]: quite specific set up
13+
// needed, alas
14+
public class ListDeser469FailingTest extends XmlTestBase
15+
{
16+
static class OuterBean {
17+
public MiddleBean middle;
18+
}
19+
20+
@JsonPropertyOrder({"inner1", "inner2"})
21+
static class MiddleBean
22+
{
23+
public InnerBean1 inner1;
24+
25+
@JacksonXmlElementWrapper(useWrapping = false)
26+
public List<InnerBean2> inner2;
27+
}
28+
29+
static class InnerBean1
30+
{
31+
public String str;
32+
33+
// Fail if (and only if!) wrapping disabled, by default or via
34+
// annotations
35+
// (that is; if this is commented out, passes)
36+
@JacksonXmlElementWrapper(useWrapping = false)
37+
public List<InnerBean1Item> item;
38+
}
39+
40+
static class InnerBean1Item
41+
{
42+
@JacksonXmlProperty(isAttribute = true)
43+
public String id;
44+
}
45+
46+
static class InnerBean2
47+
{
48+
@JacksonXmlProperty(isAttribute = true)
49+
public String str2;
50+
51+
protected InnerBean2() { }
52+
public InnerBean2(String s) { str2 = s; }
53+
}
54+
55+
/*
56+
/**********************************************************************
57+
/* Test methods
58+
/**********************************************************************
59+
*/
60+
61+
public void testIssue469WithDefaults() throws Exception
62+
{
63+
// Here we just use default settings (which defaults to using wrappers)
64+
final XmlMapper mapper = newMapper();
65+
66+
// First: create POJO value to test round-trip:
67+
{
68+
OuterBean source = new OuterBean();
69+
source.middle = new MiddleBean();
70+
List<InnerBean2> items = new ArrayList<>();
71+
items.add(new InnerBean2("foo"));
72+
source.middle.inner2 = items;
73+
74+
String xml = mapper.writerWithDefaultPrettyPrinter()
75+
.writeValueAsString(source);
76+
77+
OuterBean result = mapper.readValue(xml, OuterBean.class);
78+
79+
MiddleBean mid = result.middle;
80+
assertNotNull(mid);
81+
assertNotNull(mid.inner2);
82+
assertEquals(1, mid.inner2.size());
83+
assertEquals("foo", mid.inner2.get(0).str2);
84+
}
85+
86+
// And then verify from XML String
87+
String xmlInput = "<OuterBean>\n" +
88+
" <middle>\n" +
89+
" <inner1/>\n" +
90+
" <inner2 str2='aaaa'/>\n" +
91+
" </middle>\n" +
92+
"</OuterBean>\n";
93+
94+
OuterBean outer = mapper.readValue(xmlInput, OuterBean.class);
95+
96+
MiddleBean mid = outer.middle;
97+
assertNotNull(mid);
98+
99+
assertNotNull(mid.inner2);
100+
assertEquals(1, mid.inner2.size());
101+
assertEquals("aaaa", mid.inner2.get(0).str2);
102+
}
103+
}

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

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.*;
44

55
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
6+
import com.fasterxml.jackson.dataformat.xml.JacksonXmlAnnotationIntrospector;
67
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
78
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;
89
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
@@ -51,10 +52,30 @@ protected InnerBean2() { }
5152
public InnerBean2(String s) { str2 = s; }
5253
}
5354

54-
private final XmlMapper MAPPER = newMapper();
55+
// also wrt [dataformat-xml#469]
56+
static class OuterNoWrappers {
57+
public List<InnerNoWrappers> inner;
58+
}
59+
60+
static class InnerNoWrappers {
61+
@JacksonXmlProperty(isAttribute = true)
62+
public String str;
63+
64+
protected InnerNoWrappers() { }
65+
public InnerNoWrappers(String s) { str = s; }
66+
}
67+
68+
/*
69+
/**********************************************************************
70+
/* Test methods
71+
/**********************************************************************
72+
*/
5573

56-
public void testIssue469() throws Exception
74+
public void testIssue469WithDefaults() throws Exception
5775
{
76+
// Here we just use default settings (which defaults to using wrappers)
77+
final XmlMapper mapper = newMapper();
78+
5879
// First: create POJO value to test round-trip:
5980
{
6081
OuterBean source = new OuterBean();
@@ -63,10 +84,10 @@ public void testIssue469() throws Exception
6384
items.add(new InnerBean2("foo"));
6485
source.middle.inner2 = items;
6586

66-
String xml = MAPPER.writerWithDefaultPrettyPrinter()
87+
String xml = mapper.writerWithDefaultPrettyPrinter()
6788
.writeValueAsString(source);
6889

69-
OuterBean result = MAPPER.readValue(xml, OuterBean.class);
90+
OuterBean result = mapper.readValue(xml, OuterBean.class);
7091

7192
MiddleBean mid = result.middle;
7293
assertNotNull(mid);
@@ -83,7 +104,7 @@ public void testIssue469() throws Exception
83104
" </Middle>\n" +
84105
"</OuterBean>\n";
85106

86-
OuterBean outer = MAPPER.readValue(xmlInput, OuterBean.class);
107+
OuterBean outer = mapper.readValue(xmlInput, OuterBean.class);
87108

88109
MiddleBean mid = outer.middle;
89110
assertNotNull(mid);
@@ -92,4 +113,27 @@ public void testIssue469() throws Exception
92113
assertEquals(1, mid.inner2.size());
93114
assertEquals("aaaa", mid.inner2.get(0).str2);
94115
}
116+
117+
// But alternatively can try setting default to "no wrappers":
118+
public void testIssue469WithNoWrapper() throws Exception
119+
{
120+
final XmlMapper mapper = XmlMapper.builder()
121+
.annotationIntrospector(new JacksonXmlAnnotationIntrospector(false))
122+
.build();
123+
124+
// First: check round-trip
125+
{
126+
OuterNoWrappers source = new OuterNoWrappers();
127+
source.inner = new ArrayList<>();
128+
source.inner.add(new InnerNoWrappers("value"));
129+
130+
String xml = mapper.writerWithDefaultPrettyPrinter()
131+
.writeValueAsString(source);
132+
//System.err.println("XML:\n"+xml);
133+
OuterNoWrappers result = mapper.readValue(xml, OuterNoWrappers.class);
134+
assertNotNull(result.inner);
135+
assertEquals(1, result.inner.size());
136+
assertEquals("value",result.inner.get(0).str);
137+
}
138+
}
95139
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.List;
77

88
import com.fasterxml.jackson.annotation.JsonProperty;
9+
import com.fasterxml.jackson.dataformat.xml.JacksonXmlAnnotationIntrospector;
910
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
1011
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;
1112
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
@@ -81,6 +82,17 @@ public void testSimpleWrappedList() throws IOException
8182
String xml = MAPPER.writeValueAsString(new ListBean(1, 2, 3));
8283
xml = removeSjsxpNamespace(xml);
8384
assertEquals("<ListBean><values><values>1</values><values>2</values><values>3</values></values></ListBean>", xml);
85+
86+
// for [dataformat-xml#469] try forcing wrapping:
87+
XmlMapper unwrapMapper = XmlMapper.builder()
88+
.annotationIntrospector(new JacksonXmlAnnotationIntrospector(false))
89+
.build();
90+
xml = unwrapMapper.writeValueAsString(new ListBean(1, 2, 3));
91+
xml = removeSjsxpNamespace(xml);
92+
assertEquals("<ListBean>"
93+
+"<values>1</values><values>2</values><values>3</values>"
94+
+"</ListBean>",
95+
xml);
8496
}
8597

8698
public void testStringList() throws IOException

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public Optional() { }
5656

5757
/*
5858
/**********************************************************************
59-
/* Unit tests
59+
/* Test methods
6060
/**********************************************************************
6161
*/
6262

0 commit comments

Comments
 (0)