jackson-dataformat-xml: XML wrapper doesn't work with java records

Using @JacksonXmlElementWrapper and @JacksonXmlProperty annotations with Java records results in deserialization exception.

For example, I’d expect following XML:

<Request>
  <messages>
    <message>
      <text>given text</text>
    </message>
  </messages>
</Request>

to deserialize into following java records:

public record Request(
        @JacksonXmlElementWrapper(localName = "messages")
        @JacksonXmlProperty(localName = "message")
        List<Message> messages
) {
    public Request {
    }

    private Request() {
        this(null);
    }
}

public record Message(String text) {
    public Message {
    }

    private  Message() {
        this(null);
    }
}

However, it results in an exception:

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Invalid definition for property 'messages' (of type `Request`): Could not find creator property with name 'messages' (known Creator properties: [message])
 at [Source: (StringReader); line: 1, column: 10]

This is unexpected because deserialization works with equivalent java classes.

This happens with Java 17 and Jackson version 2.13.2, which is the latest one as of this writing.

I’ve put together a minimal project which showcases the issue: jackson-record-showcase.

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 6
  • Comments: 21 (12 by maintainers)

Commits related to this issue

Most upvoted comments

@MikePryadko @JacksonXmlElementWrapper works fine for POJOs; although one thing worth noting is that Jackson is not – and never planned to be, nor will be – actual JAXB implementation. Support for JAXB annotations is for interoperability, best effort. What is supported are Jackson’s annotations; a subset of JAXB annotations work.

But there seem to be issues specific to use with Java Records and Kotlin Data classes. They have their own peculiarities wrt handling.

I don’t think handling has worked better before. You can consider issues an incomplete support for specific combination of things.

And all help in actually solving the problem is welcome. But less so for commentary on “why isn’t it STILL FIXED IN 2023”. It could be 2048 and if no one has figured out how to make things work it wouldn’t work. OSS projects do not really work well with Shaming as incentive.

Excuse me, but WTF?! 😡 😡 😡 version: 2.15.0-rc3 Java 17

code:

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonRootName;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;

import java.util.List;

public class Main {

    @JsonRootName("root")
    record VerySimple (
            @JsonProperty("text") String text,
            @JsonProperty("msg") @JacksonXmlElementWrapper(localName = "msgList") List<String> msg
    ) { }

    public static void main(String[] args) throws JsonProcessingException {
        var mapper = new XmlMapper();
        mapper.enable(SerializationFeature.INDENT_OUTPUT);
        mapper.enable(ToXmlGenerator.Feature.WRITE_XML_DECLARATION);

        System.out.println(mapper.writeValueAsString(new VerySimple("text", List.of("hello","world"))));
        var xml = """
                <?xml version='1.0' encoding='UTF-8'?>
                <root>
                  <text>text</text>
                  <msgList>
                    <msg>hello</msg>
                    <msg>world</msg>
                  </msgList>
                </root>""";
        var obj = mapper.readValue(xml, VerySimple.class);
        System.out.println(obj);
    }
}

output:

<?xml version='1.0' encoding='UTF-8'?>
<root>
  <text>text</text>
  <msgList>
    <msg>hello</msg>
    <msg>world</msg>
  </msgList>
</root>

Exception in thread "main" com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Invalid definition for property 'msgList' (of type `Main$VerySimple`): Could not find creator property with name 'msgList' (known Creator properties: [text, msg])
 at [Source: (StringReader); line: 2, column: 1]
	at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:62)
	at com.fasterxml.jackson.databind.DeserializationContext.reportBadPropertyDefinition(DeserializationContext.java:1910)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.addBeanProps(BeanDeserializerFactory.java:636)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.buildBeanDeserializer(BeanDeserializerFactory.java:277)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.createBeanDeserializer(BeanDeserializerFactory.java:151)
	at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer2(DeserializerCache.java:415)
	at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer(DeserializerCache.java:350)
	at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:264)
	at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:244)
	at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:142)
	at com.fasterxml.jackson.databind.DeserializationContext.findRootValueDeserializer(DeserializationContext.java:654)
	at com.fasterxml.jackson.databind.ObjectMapper._findRootDeserializer(ObjectMapper.java:4861)
	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4731)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3677)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3645)
	at Main.main(Main.java:34)

@JooHyukKim could you elaborate on how that is failing? Not matching when it should? I am not sure what kind of extension/override would be needed.

@cowtowncoder yes, my bad. It was https://github.com/FasterXML/jackson-module-kotlin/issues/153 If the fix for java and kotlin is different from each other. That will need to be reopened?

Try v2.15.0-rc3. This version has better Record support.