Skip to content

Commit

Permalink
Fix #213
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Nov 15, 2016
1 parent 4c6a8b9 commit 2c5f6f4
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 71 deletions.
2 changes: 2 additions & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Project: jackson-dataformat-xml

2.8.5 (14-Nov-2016)

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

2.8.4 (14-Oct-2016)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,15 @@ public void serializeValue(JsonGenerator gen, Object value, JavaType rootType,

protected void _serializeXmlNull(JsonGenerator jgen) throws IOException
{
if (jgen instanceof ToXmlGenerator)
_initWithRootName((ToXmlGenerator) jgen, ROOT_NAME_FOR_NULL);
// 14-Nov-2016, tatu: As per [dataformat-xml#213], we may have explicitly
// configured root name...
QName rootName = _rootNameFromConfig();
if (rootName == null) {
rootName = ROOT_NAME_FOR_NULL;
}
if (jgen instanceof ToXmlGenerator) {
_initWithRootName((ToXmlGenerator) jgen, rootName);
}
super.serializeValue(jgen, null);
}

Expand All @@ -225,7 +232,7 @@ protected void _initWithRootName(ToXmlGenerator xgen, QName rootName) throws IOE
}
xgen.initGenerator();
String ns = rootName.getNamespaceURI();
/* [Issue#26] If we just try writing root element with namespace,
/* [dataformat-xml#26] If we just try writing root element with namespace,
* we will get an explicit prefix. But we'd rather use the default
* namespace, so let's try to force that.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.fasterxml.jackson.dataformat.xml.misc;

import java.io.IOException;

import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.dataformat.xml.*;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;

public class RootNameTest extends XmlTestBase
{
static class RootBeanBase
{
public String value;

protected RootBeanBase() { this("123"); }
public RootBeanBase(String v) {
value = v;
}
}

@JacksonXmlRootElement(localName="root")
static class RootBean extends RootBeanBase
{
protected RootBean() { super(); }
}

@JacksonXmlRootElement(localName="nsRoot", namespace="http://foo")
static class NsRootBean
{
public String value = "abc";
}

/*
/**********************************************************
/* Unit tests
/**********************************************************
*/

protected XmlMapper _xmlMapper = new XmlMapper();

// Unit test to verify that root name is properly set
public void testRootNameAnnotation() throws IOException
{
String xml = _xmlMapper.writeValueAsString(new StringBean());

// Hmmh. Looks like JDK Stax may adds bogus ns declaration. As such,
// let's just check that name starts ok...
if (!xml.startsWith("<StringBean")) {
fail("Expected root name of 'StringBean'; but XML document is ["+xml+"]");
}

// and then see that basic non-namespace root is ok
xml = _xmlMapper.writeValueAsString(new RootBean());
assertEquals("<root><value>123</value></root>", xml);

// and namespace one too
xml = _xmlMapper.writeValueAsString(new NsRootBean());
if (xml.indexOf("nsRoot") < 0) { // verify localName
fail("Expected root name of 'nsRoot'; but XML document is ["+xml+"]");
}
// and NS declaration
if (xml.indexOf("http://foo") < 0) {
fail("Expected NS declaration for 'http://foo', not found, XML document is ["+xml+"]");
}
}

public void testDynamicRootName() throws IOException
{
String xml;

ObjectWriter w = _xmlMapper.writer().withRootName("rudy");

xml = w.writeValueAsString(new StringBean("foo"));
assertEquals("<rudy><text>foo</text></rudy>", xml);

xml = w.writeValueAsString(new StringBean(null));
assertEquals("<rudy><text/></rudy>", xml);

// and even with null will respect configured root name
xml = w.writeValueAsString(null);
assertEquals("<rudy/>", xml);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,10 @@
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlCData;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;

@SuppressWarnings("serial")
public class TestSerialization extends XmlTestBase
{
/*
/**********************************************************
/* Helper types
/**********************************************************
*/

static class StringBean2
{
public String text = "foobar";
Expand Down Expand Up @@ -56,25 +49,13 @@ static class MapBean
public MapBean() { }
public MapBean(Map<String,Integer> v) { map = v; }
}

static class NsElemBean
{
@JacksonXmlProperty(namespace="http://foo")
public String text = "blah";
}

@JacksonXmlRootElement(localName="root")
static class RootBean
{
public String value = "123";
}

@JacksonXmlRootElement(localName="nsRoot", namespace="http://foo")
static class NsRootBean
{
public String value = "abc";
}

static class CDataStringBean
{
@JacksonXmlCData
Expand All @@ -99,75 +80,36 @@ public void serialize(String value, JsonGenerator jgen,
}

static class CustomMap extends LinkedHashMap<String, Integer> { }

/*
/**********************************************************
/* Set up
/**********************************************************
*/

protected XmlMapper _xmlMapper;

// let's actually reuse XmlMapper to make things bit faster
@Override
public void setUp() throws Exception {
super.setUp();
_xmlMapper = new XmlMapper();
}

/*
/**********************************************************
/* Unit tests
/**********************************************************
*/

// Unit test to verify that root name is properly set
public void testRootName() throws IOException
{
String xml = _xmlMapper.writeValueAsString(new StringBean());

// Hmmh. Looks like JDK Stax may adds bogus ns declaration. As such,
// let's just check that name starts ok...
if (!xml.startsWith("<StringBean")) {
fail("Expected root name of 'StringBean'; but XML document is ["+xml+"]");
}

// and then see that basic non-namespace root is ok
xml = _xmlMapper.writeValueAsString(new RootBean());
assertEquals("<root><value>123</value></root>", xml);
protected XmlMapper _xmlMapper = new XmlMapper();

// and namespace one too
xml = _xmlMapper.writeValueAsString(new NsRootBean());
if (xml.indexOf("nsRoot") < 0) { // verify localName
fail("Expected root name of 'nsRoot'; but XML document is ["+xml+"]");
}
// and NS declaration
if (xml.indexOf("http://foo") < 0) {
fail("Expected NS declaration for 'http://foo', not found, XML document is ["+xml+"]");
}
}

public void testSimpleAttribute() throws IOException
{
String xml = _xmlMapper.writeValueAsString(new AttributeBean());
xml = removeSjsxpNamespace(xml);
assertEquals("<AttributeBean attr=\"something\"/>", xml);
}

public void testSimpleAttrAndElem() throws IOException
{
String xml = _xmlMapper.writeValueAsString(new AttrAndElem());
xml = removeSjsxpNamespace(xml);
assertEquals("<AttrAndElem id=\"42\"><elem>whatever</elem></AttrAndElem>", xml);
}

public void testSimpleNsElem() throws IOException
{
String xml = _xmlMapper.writeValueAsString(new NsElemBean());
xml = removeSjsxpNamespace(xml);
// here we assume woodstox automatic prefixes, not very robust but:
assertEquals("<NsElemBean><wstxns1:text xmlns:wstxns1=\"http://foo\">blah</wstxns1:text></NsElemBean>", xml);
}

public void testSimpleAttrAndElem() throws IOException
{
String xml = _xmlMapper.writeValueAsString(new AttrAndElem());
xml = removeSjsxpNamespace(xml);
assertEquals("<AttrAndElem id=\"42\"><elem>whatever</elem></AttrAndElem>", xml);
}

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

// for [Issue#41]
// for [dataformat-xml#41]
public void testCustomSerializer() throws Exception
{
JacksonXmlModule module = new JacksonXmlModule();
Expand Down

0 comments on commit 2c5f6f4

Please sign in to comment.