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
567 stars 221 forks source link

Empty text is ignored in @JacksonXmlText #565

Open aarcangeli opened 1 year ago

aarcangeli commented 1 year ago

Hi, I'm trying to parse an xml file like this:

<?xml version='1.0'?>
<replacements xml:space='preserve' incomplete_format='false'>
<replacement offset='106' length='8'>    </replacement>
</replacements>

This xml is generated by clang-format -output-replacements-xml, so it is critical that whitespaces are correctly parsed as string values.

However, when I parse it with XmlMapper, the returned value is null

assertEquals("    ", response.replacements[0].value) // value is null

My setup

build.gradle.kts:

dependencies {
  implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.14.1")
}
@JacksonXmlRootElement(localName = "replacements")
public class ClangFormatResponse {

  public static ClangFormatResponse unmarshal(@NotNull String stdout) {
    try {
      XmlMapper xmlMapper = new XmlMapper();
      return xmlMapper.readValue(stdout, ClangFormatResponse.class);
    }
    catch (JsonProcessingException e) {
      throw new RuntimeException(e);
    }
  }

  @JacksonXmlProperty(localName = "space", isAttribute = true)
  public String space;

  @JacksonXmlProperty(localName = "incomplete_format", isAttribute = true)
  public boolean incompleteFormat;

  @JacksonXmlProperty(localName = "replacement")
  @JacksonXmlElementWrapper(useWrapping = false)
  public final List<ClangFormatReplacement> replacements = new ArrayList<>();
}

public class ClangFormatReplacement {
  @JacksonXmlProperty(isAttribute = true)
  public int offset;

  @JacksonXmlProperty(isAttribute = true)
  public int length;

  @JacksonXmlText(false)
  public String value;
}

My debug

From my analysis, I found that the text value is ignored due to this check