FasterXML / jackson-dataformat-xml

Extension for Jackson JSON processor that adds support for serializing POJOs as XML (and deserializing from XML) as an alternative to JSON
Apache License 2.0
562 stars 221 forks source link

XmlMapper is adding a new empty line at the end of file #546

Closed M4NN3 closed 1 year ago

M4NN3 commented 1 year ago

XmlMapper, when using writerWithDefaultPrettyPrinterand writing value to a new fle is adding an empty line at the end of the file. This is not happening when using writeValueAsString

Roomka commented 1 year ago

I took a look at the source code and it looks like this behavior is caused by https://github.com/FasterXML/jackson-dataformat-xml/blob/2.14/src/main/java/com/fasterxml/jackson/dataformat/xml/ser/ToXmlGenerator.java#L619.

From my understanding, the intention was to add a new line feed to separate entities outside of the root element (that is, in prolog or epilog), but it is also added after the document body in the scenario in which a document doesn't contain an epilog.

I am not sure if it was intended to behave in this way or if it can be considered a bug.

cowtowncoder commented 1 year ago

@Roomka if anyone could provide a test to show that with change to remove extra linefeed:

  1. Case WITH epilog (comment or PI) works as expected (comment/PI is on line following last closing element) AND
  2. Case WITHOUT anything else works (only single linefeed after last closing element)

I'd be happy to merge said PR. I don't have time to do this myself right now but would be happy to code review, get merged.

Timing now is pretty tight -- I am about to publish 2.14.0 final in 2-3 days -- but regardless of whether it'd go in 2.14.0 or later (I think this'd be ok for a patch... maybe), it'd be nice improvement.

Roomka commented 1 year ago

Hi @cowtowncoder,

not sure if I am missing something but from my understanding, there is no way to add the epilog in an object mapped by the object mapper.

I opened a PR https://github.com/FasterXML/jackson-dataformat-xml/pull/554 removing the part of the code that adds extra linefeed and updating the unit test affected by that change accordingly.

cowtowncoder commented 1 year ago

Ah. Yes, you are right, there is no way to add prolog or epilog stuff quite yet. Thank you for #554 -- I'll try to have a look soon, but am just bit swamped at work. But I'll get there eventually. :)

cowtowncoder commented 1 year ago

As per my notes on #546, I would want a full reproduction here, to make sure problem (if any!) is understood.

Basically: with indentation I

  1. WOULD expect a single linefeed at the end of document
  2. WOULD NOT expect more than one consecutive linefeeds at the end; that would look like an empty line.
cowtowncoder commented 1 year ago

Ok as far as I can see this is actually behaving as I would expect.

To change behavior what you can do is to sub-=class DefaultXmlPrettyPrinter and override method to do nothing:

public void writePrologLinefeed(XMLStreamWriter2 sw) throws XMLStreamException { }

This would change serialization so that no linefeeds would be added before opening root element or after closing one.

In theory it could be possible to make this configurable but it would get bit tricky, esp. if was to go through XmlMapper.

I will close this as "not a bug" for now: to request configurability it'd be good file a new issue with reference to this one as context.