Skip to content

Commit 2c5f6f4

Browse files
committed
Fix #213
1 parent 4c6a8b9 commit 2c5f6f4

File tree

4 files changed

+105
-71
lines changed

4 files changed

+105
-71
lines changed

release-notes/VERSION

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Project: jackson-dataformat-xml
88

99
2.8.5 (14-Nov-2016)
1010

11+
#213: `XmlSerializerProvider` does not use `withRootName` config for null
12+
(reported by gitlabbtr@github)
1113
- Update woodstox dependency to 5.0.3
1214

1315
2.8.4 (14-Oct-2016)

src/main/java/com/fasterxml/jackson/dataformat/xml/ser/XmlSerializerProvider.java

+10-3
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,15 @@ public void serializeValue(JsonGenerator gen, Object value, JavaType rootType,
199199

200200
protected void _serializeXmlNull(JsonGenerator jgen) throws IOException
201201
{
202-
if (jgen instanceof ToXmlGenerator)
203-
_initWithRootName((ToXmlGenerator) jgen, ROOT_NAME_FOR_NULL);
202+
// 14-Nov-2016, tatu: As per [dataformat-xml#213], we may have explicitly
203+
// configured root name...
204+
QName rootName = _rootNameFromConfig();
205+
if (rootName == null) {
206+
rootName = ROOT_NAME_FOR_NULL;
207+
}
208+
if (jgen instanceof ToXmlGenerator) {
209+
_initWithRootName((ToXmlGenerator) jgen, rootName);
210+
}
204211
super.serializeValue(jgen, null);
205212
}
206213

@@ -225,7 +232,7 @@ protected void _initWithRootName(ToXmlGenerator xgen, QName rootName) throws IOE
225232
}
226233
xgen.initGenerator();
227234
String ns = rootName.getNamespaceURI();
228-
/* [Issue#26] If we just try writing root element with namespace,
235+
/* [dataformat-xml#26] If we just try writing root element with namespace,
229236
* we will get an explicit prefix. But we'd rather use the default
230237
* namespace, so let's try to force that.
231238
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.fasterxml.jackson.dataformat.xml.misc;
2+
3+
import java.io.IOException;
4+
5+
import com.fasterxml.jackson.databind.ObjectWriter;
6+
import com.fasterxml.jackson.dataformat.xml.*;
7+
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
8+
9+
public class RootNameTest extends XmlTestBase
10+
{
11+
static class RootBeanBase
12+
{
13+
public String value;
14+
15+
protected RootBeanBase() { this("123"); }
16+
public RootBeanBase(String v) {
17+
value = v;
18+
}
19+
}
20+
21+
@JacksonXmlRootElement(localName="root")
22+
static class RootBean extends RootBeanBase
23+
{
24+
protected RootBean() { super(); }
25+
}
26+
27+
@JacksonXmlRootElement(localName="nsRoot", namespace="http://foo")
28+
static class NsRootBean
29+
{
30+
public String value = "abc";
31+
}
32+
33+
/*
34+
/**********************************************************
35+
/* Unit tests
36+
/**********************************************************
37+
*/
38+
39+
protected XmlMapper _xmlMapper = new XmlMapper();
40+
41+
// Unit test to verify that root name is properly set
42+
public void testRootNameAnnotation() throws IOException
43+
{
44+
String xml = _xmlMapper.writeValueAsString(new StringBean());
45+
46+
// Hmmh. Looks like JDK Stax may adds bogus ns declaration. As such,
47+
// let's just check that name starts ok...
48+
if (!xml.startsWith("<StringBean")) {
49+
fail("Expected root name of 'StringBean'; but XML document is ["+xml+"]");
50+
}
51+
52+
// and then see that basic non-namespace root is ok
53+
xml = _xmlMapper.writeValueAsString(new RootBean());
54+
assertEquals("<root><value>123</value></root>", xml);
55+
56+
// and namespace one too
57+
xml = _xmlMapper.writeValueAsString(new NsRootBean());
58+
if (xml.indexOf("nsRoot") < 0) { // verify localName
59+
fail("Expected root name of 'nsRoot'; but XML document is ["+xml+"]");
60+
}
61+
// and NS declaration
62+
if (xml.indexOf("http://foo") < 0) {
63+
fail("Expected NS declaration for 'http://foo', not found, XML document is ["+xml+"]");
64+
}
65+
}
66+
67+
public void testDynamicRootName() throws IOException
68+
{
69+
String xml;
70+
71+
ObjectWriter w = _xmlMapper.writer().withRootName("rudy");
72+
73+
xml = w.writeValueAsString(new StringBean("foo"));
74+
assertEquals("<rudy><text>foo</text></rudy>", xml);
75+
76+
xml = w.writeValueAsString(new StringBean(null));
77+
assertEquals("<rudy><text/></rudy>", xml);
78+
79+
// and even with null will respect configured root name
80+
xml = w.writeValueAsString(null);
81+
assertEquals("<rudy/>", xml);
82+
}
83+
}

src/test/java/com/fasterxml/jackson/dataformat/xml/ser/TestSerialization.java

+10-68
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,10 @@
1111
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;
1212
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlCData;
1313
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
14-
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
1514

1615
@SuppressWarnings("serial")
1716
public class TestSerialization extends XmlTestBase
1817
{
19-
/*
20-
/**********************************************************
21-
/* Helper types
22-
/**********************************************************
23-
*/
24-
2518
static class StringBean2
2619
{
2720
public String text = "foobar";
@@ -56,25 +49,13 @@ static class MapBean
5649
public MapBean() { }
5750
public MapBean(Map<String,Integer> v) { map = v; }
5851
}
59-
52+
6053
static class NsElemBean
6154
{
6255
@JacksonXmlProperty(namespace="http://foo")
6356
public String text = "blah";
6457
}
6558

66-
@JacksonXmlRootElement(localName="root")
67-
static class RootBean
68-
{
69-
public String value = "123";
70-
}
71-
72-
@JacksonXmlRootElement(localName="nsRoot", namespace="http://foo")
73-
static class NsRootBean
74-
{
75-
public String value = "abc";
76-
}
77-
7859
static class CDataStringBean
7960
{
8061
@JacksonXmlCData
@@ -99,75 +80,36 @@ public void serialize(String value, JsonGenerator jgen,
9980
}
10081

10182
static class CustomMap extends LinkedHashMap<String, Integer> { }
102-
103-
/*
104-
/**********************************************************
105-
/* Set up
106-
/**********************************************************
107-
*/
108-
109-
protected XmlMapper _xmlMapper;
11083

111-
// let's actually reuse XmlMapper to make things bit faster
112-
@Override
113-
public void setUp() throws Exception {
114-
super.setUp();
115-
_xmlMapper = new XmlMapper();
116-
}
117-
11884
/*
11985
/**********************************************************
12086
/* Unit tests
12187
/**********************************************************
12288
*/
12389

124-
// Unit test to verify that root name is properly set
125-
public void testRootName() throws IOException
126-
{
127-
String xml = _xmlMapper.writeValueAsString(new StringBean());
128-
129-
// Hmmh. Looks like JDK Stax may adds bogus ns declaration. As such,
130-
// let's just check that name starts ok...
131-
if (!xml.startsWith("<StringBean")) {
132-
fail("Expected root name of 'StringBean'; but XML document is ["+xml+"]");
133-
}
134-
135-
// and then see that basic non-namespace root is ok
136-
xml = _xmlMapper.writeValueAsString(new RootBean());
137-
assertEquals("<root><value>123</value></root>", xml);
90+
protected XmlMapper _xmlMapper = new XmlMapper();
13891

139-
// and namespace one too
140-
xml = _xmlMapper.writeValueAsString(new NsRootBean());
141-
if (xml.indexOf("nsRoot") < 0) { // verify localName
142-
fail("Expected root name of 'nsRoot'; but XML document is ["+xml+"]");
143-
}
144-
// and NS declaration
145-
if (xml.indexOf("http://foo") < 0) {
146-
fail("Expected NS declaration for 'http://foo', not found, XML document is ["+xml+"]");
147-
}
148-
}
149-
15092
public void testSimpleAttribute() throws IOException
15193
{
15294
String xml = _xmlMapper.writeValueAsString(new AttributeBean());
15395
xml = removeSjsxpNamespace(xml);
15496
assertEquals("<AttributeBean attr=\"something\"/>", xml);
15597
}
15698

157-
public void testSimpleAttrAndElem() throws IOException
158-
{
159-
String xml = _xmlMapper.writeValueAsString(new AttrAndElem());
160-
xml = removeSjsxpNamespace(xml);
161-
assertEquals("<AttrAndElem id=\"42\"><elem>whatever</elem></AttrAndElem>", xml);
162-
}
163-
16499
public void testSimpleNsElem() throws IOException
165100
{
166101
String xml = _xmlMapper.writeValueAsString(new NsElemBean());
167102
xml = removeSjsxpNamespace(xml);
168103
// here we assume woodstox automatic prefixes, not very robust but:
169104
assertEquals("<NsElemBean><wstxns1:text xmlns:wstxns1=\"http://foo\">blah</wstxns1:text></NsElemBean>", xml);
170105
}
106+
107+
public void testSimpleAttrAndElem() throws IOException
108+
{
109+
String xml = _xmlMapper.writeValueAsString(new AttrAndElem());
110+
xml = removeSjsxpNamespace(xml);
111+
assertEquals("<AttrAndElem id=\"42\"><elem>whatever</elem></AttrAndElem>", xml);
112+
}
171113

172114
@SuppressWarnings("boxing")
173115
public void testMap() throws IOException
@@ -225,7 +167,7 @@ public void testCDataStringArray() throws IOException
225167
assertEquals("<CDataStringArrayBean><value><value><![CDATA[<some<data\"]]></value><value><![CDATA[abc]]></value></value></CDataStringArrayBean>", xml);
226168
}
227169

228-
// for [Issue#41]
170+
// for [dataformat-xml#41]
229171
public void testCustomSerializer() throws Exception
230172
{
231173
JacksonXmlModule module = new JacksonXmlModule();

0 commit comments

Comments
 (0)