Java Record with @JacksonXmlText stop working with 2.18
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
just in case, could you try 2.18.3?
Same issue with 2.18.3
XML-specific, will move to proper repo.
this looks a bit like #739 which was written for #734
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.
Closed #734 in favor of this one.
Suspecting that marker "" for
@JacksonXmlTextgetting 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.
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 original reporter did claim example worked with 2.17.2. Are you trying the exactly same class definition?
@cowtowncoder I created #776 and added some comments about the broken case from #734 and what seems to be going wrong.
@cowtowncoder yes, exactly the same. I tried java 17, the author didn't say java version
@Aaur1s huh. That's odd. JDK version unlikely to matter.
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());
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
I tried ParameterNamesModule again and it doesn't work. My earlier attempt must have had some issue.
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.
Side note: related to #306.