jackson-dataformat-xml icon indicating copy to clipboard operation
jackson-dataformat-xml copied to clipboard

Java Record with @JacksonXmlText stop working with 2.18

Open akastyka opened this issue 9 months ago • 16 comments

Search before asking

  • [x] I searched in the issues and found nothing similar.

Describe the bug

Following class can't be deserialized with Jackson 2.18.2: public record Variable( @JacksonXmlProperty(isAttribute = true, localName = "name") String name, @JacksonXmlText String value) {}

deserialization fails with: Invalid definition for property '' (of type com.Variable): Could not find creator property with name '' (known Creator properties: [name, value]) at [Source: (StringReader); line: 1, column: 1]

it works with Jackson 2.17.2. Also it works with 2.18.2 but with POJO only

Version Information

2.18.2

Reproduction

-->

public record Variable(
        @JacksonXmlProperty(isAttribute = true, localName = "name") String name,
        @JacksonXmlText String value) {
    public static void deserialize() throws JsonProcessingException {
        XmlMapper xmlMapper = new XmlMapper();

        Variable deserialized = xmlMapper.readValue(
                "<jackson name=\"value\">test</jackson>", Variable.class);
        System.out.println(deserialized);
    }
}

Expected behavior

Java record is deserialized

Additional context

No response

akastyka avatar Mar 15 '25 15:03 akastyka

just in case, could you try 2.18.3?

pjfanning avatar Mar 15 '25 16:03 pjfanning

Same issue with 2.18.3

akastyka avatar Mar 15 '25 18:03 akastyka

XML-specific, will move to proper repo.

cowtowncoder avatar Mar 16 '25 20:03 cowtowncoder

this looks a bit like #739 which was written for #734

pjfanning avatar Mar 17 '25 16:03 pjfanning

Closed #734 in favor of this one.

Suspecting that marker "" for @JacksonXmlText getting wrongly associated might be causing/related to the issue here. Although Property introspection changes in 2.18 also likely related. But quite odd things work with POJOs with same annotations... handling differs, but 2.18 handles Records more similarly to POJOs that 2.17.

cowtowncoder avatar Mar 17 '25 20:03 cowtowncoder

Closed #734 in favor of this one.

Suspecting that marker "" for @JacksonXmlText getting wrongly associated might be causing/related to the issue here. Although Property introspection changes in 2.18 also likely related. But quite odd things work with POJOs with same annotations... handling differs, but 2.18 handles Records more similarly to POJOs that 2.17.

https://github.com/FasterXML/jackson-dataformat-xml/issues/734#issuecomment-2729763968 seems to suggest that creating a class with the equivalent definition to a record works better in 2.18 than the record.

pjfanning avatar Mar 17 '25 23:03 pjfanning

Doesn't work on 2.17.x, doesn't work in any version (checked from 2.16) and even 3.x. My whole project DTOs structure are records, it's a shame for me it doesn't work, is there some fix I don't see?

Aaur1s avatar Sep 14 '25 22:09 Aaur1s

@Aaur1s original reporter did claim example worked with 2.17.2. Are you trying the exactly same class definition?

cowtowncoder avatar Sep 14 '25 23:09 cowtowncoder

@cowtowncoder I created #776 and added some comments about the broken case from #734 and what seems to be going wrong.

pjfanning avatar Sep 14 '25 23:09 pjfanning

@cowtowncoder yes, exactly the same. I tried java 17, the author didn't say java version

Aaur1s avatar Sep 15 '25 06:09 Aaur1s

@Aaur1s huh. That's odd. JDK version unlikely to matter.

cowtowncoder avatar Sep 15 '25 18:09 cowtowncoder

I tried the test case that we were given for #734 and that passes for me if I add https://github.com/FasterXML/jackson-modules-java8/tree/2.x/parameter-names

mapper.registerModule(new com.fasterxml.jackson.module.paramnames.ParameterNamesModule());

pjfanning avatar Sep 15 '25 19:09 pjfanning

Didn't work for me, tryed 2.17.2, 2.18.2, 2.20.0 with either -parameters, ParameterNamesModule. I just can't make example to work, my code:

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;


public class RunReproducer {
    public static void main(String[] args) throws JsonProcessingException {
        var xmlMapper = new XmlMapper();
        var xml = "<jackson name=\"value\">test</jackson>";
        System.out.println("Input XML: " + xml);


        var deserialized = xmlMapper.readValue(xml, Variable.class);
        System.out.println("Deserialized: " + deserialized);
    }
}
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlText;


public record Variable(
        @JacksonXmlProperty(isAttribute = true, localName = "name")
        String name,
        
        @JacksonXmlText
        String value
) {
}

Java 17, build system maven

Aaur1s avatar Sep 15 '25 21:09 Aaur1s

I tried ParameterNamesModule again and it doesn't work. My earlier attempt must have had some issue.

pjfanning avatar Sep 16 '25 02:09 pjfanning

We should add test (expected to fail....) with POJO with @JsonCreator, will see if I can add.

EDIT: added. And yes, it does fail too.

cowtowncoder avatar Sep 18 '25 03:09 cowtowncoder

Side note: related to #306.

cowtowncoder avatar Sep 18 '25 04:09 cowtowncoder