Skip to content

Commit

Permalink
Fixed #413
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jun 23, 2020
1 parent d895334 commit cbabade
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 22 deletions.
5 changes: 5 additions & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,8 @@ Martin Vysny (mvysny@github)

* Reported #395: Namespace repairing generates xmlns definitions for xml: prefix (which is implicit)
(2.10.5)

James Bushell (jimnz111@github)

* Suggested #413: Null String field serialization through ToXmlGenerator causes NullPointerException
(2.10.5)
4 changes: 3 additions & 1 deletion release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ Project: jackson-dataformat-xml

#395: Namespace repairing generates xmlns definitions for xml: prefix (which is implicit)
(reported by Martin V)
- Upgrade Woodstox dependency to 6.2.0 (minor fix to namespace binding)
#413: Null String field serialization through ToXmlGenerator causes NullPointerException
(suggested by James B)
- Upgrade Woodstox dependency to 6.2.1

2.10.4 (03-May-2020)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -590,10 +590,14 @@ public void writeFieldName(SerializableString name) throws IOException
{
writeFieldName(name.getValue());
}

@Override
public void writeString(String text) throws IOException
{
if (text == null) { // [dataformat-xml#413]
writeNull();
return;
}
_verifyValueWrite("write String value");
if (_nextName == null) {
handleMissingName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@

public class XmlGeneratorTest extends XmlTestBase
{
private final XmlFactory XML_F = new XmlFactory();

public void testSimpleElement() throws Exception
{
XmlFactory f = new XmlFactory();
StringWriter out = new StringWriter();
ToXmlGenerator gen = f.createGenerator(out);
ToXmlGenerator gen = XML_F.createGenerator(out);
// root name is special, need to be fed first:
gen.setNextName(new QName("root"));
gen.writeStartObject();
Expand All @@ -30,11 +31,37 @@ public void testSimpleElement() throws Exception
assertEquals("<root><elem>value</elem></root>", xml);
}

public void testNullValuedElement() throws Exception
{
// First explicitly written
StringWriter out = new StringWriter();
ToXmlGenerator gen = XML_F.createGenerator(out);
gen.setNextName(new QName("root"));
gen.writeStartObject();
gen.writeFieldName("elem");
gen.writeNull();
gen.writeEndObject();
gen.close();
String xml = removeSjsxpNamespace(out.toString());
assertEquals("<root><elem/></root>", xml);

// and then indirectly (see [dataformat-xml#413])
out = new StringWriter();
gen = XML_F.createGenerator(out);
gen.setNextName(new QName("root"));
gen.writeStartObject();
gen.writeFieldName("elem");
gen.writeString((String) null);
gen.writeEndObject();
gen.close();
xml = removeSjsxpNamespace(out.toString());
assertEquals("<root><elem/></root>", xml);
}

public void testSimpleAttribute() throws Exception
{
XmlFactory f = new XmlFactory();
StringWriter out = new StringWriter();
ToXmlGenerator gen = f.createGenerator(out);
ToXmlGenerator gen = XML_F.createGenerator(out);
// root name is special, need to be fed first:
gen.setNextName(new QName("root"));
gen.writeStartObject();
Expand All @@ -52,9 +79,8 @@ public void testSimpleAttribute() throws Exception

public void testSecondLevelAttribute() throws Exception
{
XmlFactory f = new XmlFactory();
StringWriter out = new StringWriter();
ToXmlGenerator gen = f.createGenerator(out);
ToXmlGenerator gen = XML_F.createGenerator(out);
gen.setNextName(new QName("root"));
gen.writeStartObject();
gen.writeFieldName("elem");
Expand All @@ -74,9 +100,8 @@ public void testSecondLevelAttribute() throws Exception

public void testAttrAndElem() throws Exception
{
XmlFactory f = new XmlFactory();
StringWriter out = new StringWriter();
ToXmlGenerator gen = f.createGenerator(out);
ToXmlGenerator gen = XML_F.createGenerator(out);
gen.setNextName(new QName("root"));
gen.writeStartObject();
// and also need to force attribute
Expand Down Expand Up @@ -109,9 +134,8 @@ public void testWriteToFile() throws Exception

public void testRawSimpleValue() throws Exception
{
XmlFactory f = new XmlFactory();
StringWriter out = new StringWriter();
ToXmlGenerator gen = f.createGenerator(out);
ToXmlGenerator gen = XML_F.createGenerator(out);
// root name is special, need to be fed first:
gen.setNextName(new QName("root"));
gen.writeStartObject();
Expand All @@ -127,9 +151,8 @@ public void testRawSimpleValue() throws Exception

public void testRawOffsetValue() throws Exception
{
XmlFactory f = new XmlFactory();
StringWriter out = new StringWriter();
ToXmlGenerator gen = f.createGenerator(out);
ToXmlGenerator gen = XML_F.createGenerator(out);
// root name is special, need to be fed first:
gen.setNextName(new QName("root"));
gen.writeStartObject();
Expand All @@ -145,9 +168,8 @@ public void testRawOffsetValue() throws Exception

public void testRawCharArrayValue() throws Exception
{
XmlFactory f = new XmlFactory();
StringWriter out = new StringWriter();
ToXmlGenerator gen = f.createGenerator(out);
ToXmlGenerator gen = XML_F.createGenerator(out);
// root name is special, need to be fed first:
gen.setNextName(new QName("root"));
gen.writeStartObject();
Expand All @@ -163,9 +185,8 @@ public void testRawCharArrayValue() throws Exception

public void testRawSimpleAttribute() throws Exception
{
XmlFactory f = new XmlFactory();
StringWriter out = new StringWriter();
ToXmlGenerator gen = f.createGenerator(out);
ToXmlGenerator gen = XML_F.createGenerator(out);
// root name is special, need to be fed first:
gen.setNextName(new QName("root"));
gen.writeStartObject();
Expand All @@ -183,9 +204,8 @@ public void testRawSimpleAttribute() throws Exception

public void testRawOffsetAttribute() throws Exception
{
XmlFactory f = new XmlFactory();
StringWriter out = new StringWriter();
ToXmlGenerator gen = f.createGenerator(out);
ToXmlGenerator gen = XML_F.createGenerator(out);
// root name is special, need to be fed first:
gen.setNextName(new QName("root"));
gen.writeStartObject();
Expand All @@ -203,9 +223,8 @@ public void testRawOffsetAttribute() throws Exception

public void testRawCharArratAttribute() throws Exception
{
XmlFactory f = new XmlFactory();
StringWriter out = new StringWriter();
ToXmlGenerator gen = f.createGenerator(out);
ToXmlGenerator gen = XML_F.createGenerator(out);
// root name is special, need to be fed first:
gen.setNextName(new QName("root"));
gen.writeStartObject();
Expand Down

0 comments on commit cbabade

Please sign in to comment.