Skip to content

Null String field serialization through ToXmlGenerator causes NullPointerException #413

@jimnz111

Description

@jimnz111

Using jackson-data-format-xml Version 2.10.2

I am using a custom serializer for an pojo to allow conversion to json or xml. It will pass null values for String and Number fields as opposed to "" and 0 into the serializer.

For json everything works as intended and the json would look like this example

{
   field1: null,
   field2: "value"
}

The output must also be available in XML, and that uses ToXmlGenerator for serializing the data. Where a String field is null, it will fail in woodstox BaseStreamWriter with a NullPointerException, as it assumes the String passed is never a null.

A snippet of the stacktrace shows the path it went through

java.lang.NullPointerException: null
	at com.ctc.wstx.sw.BaseStreamWriter.writeCharacters(BaseStreamWriter.java:458)
	at com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator.writeString(ToXmlGenerator.java:622)
	at com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator.writeStringField(ToXmlGenerator.java:460)
	at com.fis.core.json.JsonGeneratorPojoFieldVisitor.lambda$2(JsonGeneratorPojoFieldVisitor.java:170)

In ToXmlGenerator the writeString(String text) method does not check for a null String. Looking at the other methods eg writeNumber(BigInteger value) there is this code to check for a null

if (value == null) {
    writeNull();
    return;
}

I couldn't find any reason that the writeString(String) shouldn't also check for a null value. I checked in version 3.0 and there is no changes for it. There may well be a valid explanation of why it shouldn't check for a null. For me I found a workaround for this in that I check in my serializer that if the string is null to instead call writeNullField(String fieldName). Some developers may not have this capability, and if it can instead call the writeNull() it would remove the potential for NPE.

Can the check for null String be added to the method in ToXmlGenerator.writeString(String)?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions