question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Deserialization fails with `XmlMapper` and `DeserializationFeature.UNWRAP_ROOT_VALUE`

See original GitHub issue

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”])

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
cowtowncodercommented, Sep 19, 2021

Note: as per #485 the behavior WILL CHANGE for 2.13.0 so that:

  • Deserializer WILL AGAIN unwrap root element, but
  • Serializer STILL DOES NOT add wrapping

Second issue may be addressed with a later version (2.14 or later) but first we will want to get back to 2.11 baseline compatibility.

0reactions
ionel-sirbu-crunchcommented, Jul 16, 2021

@ionel-sirbu-crunch Unwrapping in XML has different semantics than with JSON so that feature does nothing with XML. Fix is to prevent attempts for unwrapping since that will not work the way databind would try to apply it.

For your specific problem, please file a new issue with reproduction (showing what specifically you think is failing) and include information on versions where things appeared to work.

Created https://github.com/FasterXML/jackson-dataformat-xml/issues/485 for that matter. Thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

DeserializationFeature (jackson-databind 2.6.0 API) - FasterXML
Feature that determines whether encountering of JSON null is an error when deserializing into Java primitive types (like 'int' or 'double').
Read more >
Jackson json deserialization, ignore root element from json
configure(DeserializationFeature.UNWRAP_ROOT_VALUE, Objects.nonNull(rootAnnotation));. Basically this configuration will help Mapper to identify if provided ...
Read more >
Example usage for com.fasterxml.jackson.dataformat.xml ...
List of usage examples for com.fasterxml.jackson.dataformat.xml XmlMapper readValue ... UNWRAP_ROOT_VALUE, true); mapper.configure(DeserializationFeature.
Read more >
FasterXML/jackson-databind - Gitter
DeserializationFeature import com.fasterxml.jackson.dataformat.xml. ... Hi, I have an issue with my code and somehow this code below throws a ...
Read more >
Jackson Exceptions - Problems and Solutions - Baeldung
The most common Jackson exceptions - the cause of the problem and the solution ... new ObjectMapper(); mapper.enable(DeserializationFeature.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found