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.

Jackson deserializer not using JacksonXmlProperty localname when used with JsonPOJOBuilder

See original GitHub issue

Hello,

I’m currently developing a library to enable conversion between MSML with custom elements and Java’s POJOs. I have encountered several issues related to jackson (de)serialization in the last few weeks needing workarounds that I would prefer to avoid. I have already dug over issues there and tentative solutions before posting here.

This issue report concerns the JacksonXmlProperty annotation’s localname not used by Jackson Deserializer when JsonPOJOBuilder is also used. Ideally, jackson should be aware of the local name specified by this annotation when deserializing with the pojo builder annotation also used.

This issue occurs to me on the following code:

@Builder
@JacksonXmlRootElement(localName = "stats")
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonDeserialize(builder = StatsQuery.StatsQueryBuilder.class)
public class StatsQuery {
    @JacksonXmlProperty(localName = "queryid", isAttribute = true)
    private String queryId;

    @JacksonXmlProperty(localName = "list", isAttribute = true)
    private String list;

    @JacksonXmlProperty(localName = "reset", isAttribute = true)
    @Builder.Default private Boolean reset = true;

    @JsonPOJOBuilder(withPrefix = "")
    public static class StatsQueryBuilder {
    }
}

You can see there that the java field is using camelCase per conventions and the xml property are defined in downcase. The builder class need to be specified so I can have reset value set to true and not null as I use Lombok as a builder generator

Attempting a deserialization with the above code results in the following exception: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "queryid" (class StatsQuery$StatsQueryBuilder), not marked as ignorable (3 known properties: "queryId", "list", "reset"]).

The problem seems to be that in this case of having @JsonPOJOBuilder, jackson uses java field names and not @JacksonXMLProperty as it should. This occurs for every fields of my project when the java name is different (or has a different case) than the property, which is the goal of having a local name attribute.

The temporary solution, I have found was to also set the JAXB annotation @JsonProperty(“queryid”) on the field. However you can easily see that mentioning 2 annotations with the same supposed goal is not a proper way of writing good code as the two annotations are coupled (modifying one causes modifying the other).

If you need more information, please tell me.

System information

Java version : 11 Jackson version : jackson-dataformat-xml@2.10.2 Woodstox : woodstox-core@6.0.3 Lombok : lombok@1.18.12

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:20 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
janriekecommented, Apr 6, 2020

FYI: I created a PR for lombok (rzwitserloot/lombok#2416 ) to also automatically copy @JacksonXmlProperty to the builder setter method (as it already does for @JsonProperty). This would solve this issue at least for lombok users. I still suggest to document that Jackson does not consider those annotations on a setter method parameter.

0reactions
cowtowncodercommented, May 24, 2020

It would make to see if you can see something that matches. There are, for example:

so I am bit torn on exactly how to formulate RFE. It sounds like this would be more complicated than just associating class-annotations from value type to builder (which is relatively easier), and somehow tried to associate annotations on properties to help guide builder.

So I guess, yes, please file a jackson-databind request: it can be referring to @JsonProperty (but mentioned @JacksonXmlProperty as alias) since main mechanism would be about inclusion, naming.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jackson xml 2.9.0: @JacksonXmlElementWrapper not ...
This problem is really tricky because Jackson collects metadata from different places: fields, getters, setters, constructor parameters.
Read more >
More Jackson Annotations - Baeldung
In order to demonstrate the use @JsonIdentityReference, we will define two different bean classes, without and with this annotation.
Read more >
Jackson Deserialization and the Builder Pattern
Tutorial that shows how to use the builder pattern with Jackson for deserialization purposes. It also covers how to do it if we're...
Read more >
Issue with order of inheritance type attribute - Google Groups
Hi guys, I'm currently facing an odd behavior with how polymorphic XML objects are deserialized when using Jackson 2.13.1. The deserialization fails when ......
Read more >
Solving the XML Problem with Jackson - Stackify
Configuring the Jackson XML Module · setDefaultUseWrapper – defines whether or not to use a wrapper for non-annotated List properties by default ...
Read more >

github_iconTop Related Medium Post

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