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

Xml type resolver fails with NPE when property name is not specified in polymorphic (de)serialization #451

Closed
MichalStehlikCz opened this issue Feb 9, 2021 · 1 comment · Fixed by qoomon/maven-git-versioning-extension#99
Milestone

Comments

@MichalStehlikCz
Copy link

If property name is not specified in polymorphic serialization, serialization fails as in following example

public class XmlPolyTest {

  @JsonTypeInfo(use = DEDUCTION)
  @JsonSubTypes(@JsonSubTypes.Type(Child1.class))
  public interface Parent {}

  public static class Child1 implements Parent {
    private final String property1;

    @JsonCreator(mode = Mode.PROPERTIES)
    public Child1(String property1) {
      this.property1 = property1;
    }

    public String getProperty1() {
      return property1;
    }
  }

  @Test
  void jacksonXmlTest() throws JsonProcessingException {
    var xmlMapper = new XmlMapper();
    assertThat(xmlMapper.writeValueAsString(new Child1("value1")))
        .contains("<property1>value1</property1>");
  }
}

Produces

java.lang.NullPointerException
	at com.fasterxml.jackson.dataformat.xml.util.StaxUtil.sanitizeXmlTypeName(StaxUtil.java:81)
	at com.fasterxml.jackson.dataformat.xml.XmlTypeResolverBuilder.typeProperty(XmlTypeResolverBuilder.java:45)
	at com.fasterxml.jackson.dataformat.xml.XmlTypeResolverBuilder.typeProperty(XmlTypeResolverBuilder.java:25)
	at com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector._findTypeResolver(JacksonAnnotationIntrospector.java:1517)

Workaround is to specify dummy property name (which is ignored in use=DEDUCTION), but there should be probably nullness check (probably in XmlTypeResolverBuilder#typeProperty around call to StaxUtil.sanitizeXmlTypeName?)

@cowtowncoder
Copy link
Member

Thank you for reporting this.

Agreed, NPE should not be exposed. I assume this is with 2.12.1?

@cowtowncoder cowtowncoder modified the milestones: 2.12.-, 2.12.2 Feb 11, 2021
@cowtowncoder cowtowncoder changed the title Xml type resolver fails with NPE when property name is not specified in polymorphic serialization / deserialization Xml type resolver fails with NPE when property name is not specified in polymorphic (de)serialization Feb 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants