Skip to content

Fix for #162 (XML Empty tag to Empty string) #218

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

Merged
merged 1 commit into from
Jun 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -371,21 +371,23 @@ private final int _next() throws XMLStreamException

private final String _collectUntilTag() throws XMLStreamException
{
String text = null;
if (_xmlReader.isEmptyElement()) {
_xmlReader.next();
return null;
}

StringBuilder text = new StringBuilder();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmh. Why? This was an optimization for the common case of just single segment, in which case construction of intermediate StringBuilder was avoided. It's probably not a big deal either way, but usually I prefer not fixing things that ain't broke.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh. Actually, I see. So, this was to get empty String over null.
I think I'll modify this bit differently, after first merging,.


while (true) {
switch (_xmlReader.next()) {
case XMLStreamConstants.START_ELEMENT:
case XMLStreamConstants.END_ELEMENT:
case XMLStreamConstants.END_DOCUMENT:
return text;
// note: SPACE is ignorable (and seldom seen), not to be included
return text.toString();
// note: SPACE is ignorable (and seldom seen), not to be included
case XMLStreamConstants.CHARACTERS:
case XMLStreamConstants.CDATA:
if (text == null) {
text = _xmlReader.getText();
} else { // can be optimized in future, if need be:
text += _xmlReader.getText();
}
text.append(_xmlReader.getText());
break;
default:
// any other type (proc instr, comment etc) is just ignored
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.fasterxml.jackson.dataformat.xml.failing;
package com.fasterxml.jackson.dataformat.xml.deser;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -40,6 +40,14 @@ public void testEmptyString162() throws Exception
assertEquals("", name.last);
}

public void testEmptyElement() throws Exception
{
Name name = MAPPER.readValue("<name><first/><last></last></name>", Name.class);
assertNotNull(name);
assertNull(name.first);
assertEquals("", name.last);
}

public void testEmptyStringElement() throws Exception
{
// then with empty element
Expand Down