Skip to content

Deserialization fails with XmlMapper and DeserializationFeature.UNWRAP_ROOT_VALUE #374

@dewarim

Description

@dewarim

Simple Test case: https://github.com/dewarim/jackson-xml-debug

Using jackson-dataformat-xml version 2.10.1 @ Java 11

Given: a POJO:

@JacksonXmlRootElement(localName = "Root")
@JsonRootName("Root")
public class Root {

    @JsonProperty("id")
    String id;

    public Root(String id) {
        this.id = id;
    }

    public Root() {
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Root root = (Root) o;
        return Objects.equals(id, root.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }
}

When trying to do

        ObjectMapper mapper = new XmlMapper();
       mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
        Root deserializedRoot = mapper.readValue(value, Root.class);

I get:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Root name 'id' does not match expected ('Root') for type [simple type, class test.debug.Root]
at [Source: (StringReader); line: 1, column: 7] (through reference chain: test.debug.Root["id"])

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions