Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SequenceWriter returns NPE when trying XML serialization #493

Closed
Moribund7 opened this issue Sep 1, 2021 · 2 comments
Closed

SequenceWriter returns NPE when trying XML serialization #493

Moribund7 opened this issue Sep 1, 2021 · 2 comments
Milestone

Comments

@Moribund7
Copy link

Describe the bug
SequenceWriter returns NPE when trying XML serialization.

Version information
2.11.2

To Reproduce
If you have a way to reproduce this with:

ByteArrayOutputStream OUTPUT_STREAM = new ByteArrayOutputStream();

  ObjectMapper xmlMapper = new XmlMapper();

  SequenceWriter seqWriter = xmlMapper.writer().writeValues(OUTPUT_STREAM);


  Map<String, String> reportObject = new HashMap<>();
  reportObject.put("a", "b");
  seqWriter.write(reportObject);

Expected behavior

Additional context
Stack trace:

java.lang.NullPointerException
	at com.fasterxml.jackson.dataformat.xml.util.XmlRootNameLookup.findRootName(XmlRootNameLookup.java:41)
	at com.fasterxml.jackson.dataformat.xml.ser.XmlSerializerProvider.serializeValue(XmlSerializerProvider.java:148)
	at com.fasterxml.jackson.databind.SequenceWriter.write(SequenceWriter.java:153)

@cowtowncoder
Copy link
Member

Since 2.11.2 is neither the latest minor version, nor the latest patch of 2.11 (2.11.4 is that) it would make sense to try this with a more recent version? Ideally 2.12.5 that was just released.
I don't think that will make much difference here but as a general best practice it makes sense.

Note, however that although NPE is unexpected, usage as shown will not work: since XML documents MUST have just ONE ROOT ELEMENT, and as such it is not possible to write root value sequences this way.
This is limitation of XML (and underlying XML generators) more than Jackson.
I realize that in your example the issue is reported for the first value so this particular case might be supportable, but there would be failure for second value.

If you absolutely want to produce such output, you will need to use different approach: use separate writeValue() calls, passing OutputStream.
So something like:

ObjectMapper xmlMapper = new XmlMapper();
ObjectWriter w = xmlMapper.writer()
w.writeValue(out, value1);
w.writeValue(out, value2);

and this would "work" in the sense that separate calls would create separate generators that are unaware of previously written root elements.

@cowtowncoder cowtowncoder transferred this issue from FasterXML/jackson-databind Sep 2, 2021
@cowtowncoder cowtowncoder modified the milestones: 2,, 2.13.1 Oct 19, 2021
@cowtowncoder
Copy link
Member

Hmmh. Ok, turns out that after proper handling of null, code does sort of work, for the simplest of cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants