jackson-dataformat-xml: Deserialization of Xml with `@JacksonXmlText`, `java.util.Map` fails
This is a reproduction of #198. It was mentioned opening a new issue is preferred.
The issue is, that @JacksonXmlText seems to work as intended for serialization, but not for deserialization.
Hence, my reproduction of the original issue with 2.15.4:
I have the following models and tests:
@JacksonXmlRootElement(localName = "ITEMROOT")
public record ItemRoot(
@JsonProperty("Item") @JacksonXmlElementWrapper(useWrapping = false)
List<Item> item) {
public record Item(
@JsonProperty("name") @JacksonXmlProperty(isAttribute = true) String name,
@JacksonXmlText String value) {
@JsonCreator
public Item(final Map<String, String> item) {
this(item.get("name"), item.get(""));
}
}
}
class Tests {
@Test
void testDeserializeItemRoot() throws JsonProcessingException {
final var xmlMapper = new XmlMapper().registerModule(new ParanamerModule());
final var itemRoot =
new ItemRoot(
List.of(
new ItemRoot.Item("name1", "value1"),
new ItemRoot.Item("name2", "value2")));
final var itemRootSerialized =
xmlMapper.writerWithDefaultPrettyPrinter().writeValueAsString(itemRoot);
final var itemRootXml =
"""
<ITEMROOT>
<Item name="name1">value1</Item>
<Item name="name2">value2</Item>
</ITEMROOT>
""";
assertEquals(itemRootXml, itemRootSerialized);
final var itemRootDeserialized = xmlMapper.readValue(itemRootXml, ItemRoot.class);
assertEquals(itemRoot, itemRootDeserialized);
}
}
First, I serialize the model to verify what I actually want to deserialize is correct and then I serialize the XML again.
The tests pass because of @JsonCreator in Item. Without the annotation, I get the following error on the xmlMapper.readValue():
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Invalid definition for property '' (of type `ch.sample.model.ItemRoot$Item`): Could not find creator property with name '' (known Creator properties: [name, value])
at [Source: (StringReader); line: 1, column: 1]
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 3
- Comments: 16 (7 by maintainers)
This even fails for me with the extra
@JsonCreatorconstructor. (Jackson 2.18.1, Java 23)Thank you @kistlers. Yes, a new issue with reproduction works. I labelled it with
recordsince that is likely relevant here.