Skip to content

Deserialization of Xml with @JacksonXmlText, java.util.Map fails #615

Open
@kistlers

Description

@kistlers

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]

Metadata

Metadata

Assignees

No one assigned

    Labels

    pr-neededFeature request for which PR likely needed (no active development but idea is workable)test-needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions