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.

@JsonIgnoreProperties ignoreUnknown does not work with @JsonDeserialize builder

See original GitHub issue

Trying to unmarshall an object like:

@JsonDeserialize(builder = TestObject.Builder.class)
@JsonIgnoreProperties(ignoreUnknown = true)
public class TestObject {

    private final String property1;

    private TestObject(Builder builder) {
        property1 = builder.property1;
    }

    public static Builder builder() {
        return new Builder();
    }

    public String getProperty1() {
        return property1;
    }

    public static class Builder {

        private String property1;

        public Builder withProperty1(String property1) {
            this.property1 = property1;
            return this;
        }

        public TestObject build() {
            return new TestObject(this);
        }

    }
}

from a String like:

{"property1":"value1", "property2":"value2"}

causes UnrecognizedPropertyException.

The same String when unmarshalled to

@JsonIgnoreProperties(ignoreUnknown = true)
public class TestObjectNoBuilder {

    private String property1;

    public String getProperty1() {
        return property1;
    }

    public void setProperty1(String property1) {
        this.property1 = property1;
    }

}

works as expected - there’s no exception for property2.

Tested with Jackson 2.6.5 and 2.7.3. Might be similar to https://github.com/FasterXML/jackson-databind/issues/1118.

Issue Analytics

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

github_iconTop GitHub Comments

5reactions
ChrisRenkecommented, Jan 29, 2019

I recently ran into this general issue in conjunction with the Lombok library, which generates builders with a single annotation. Sadly, I cannot seem to figure out a workaround as it is not currently possible with Lombok to add annotations to its generated builder classes, and thus I cannot give the builder a @JsonIgnoreProperties(ignoreUnknown = true) annotation. If I replace the generated Lombok builder with a hand-rolled identical builder and give annotate the builder class with @JsonIgnoreProperties(ignoreUnknown = true), everything functions correctly.

The reasoning for closing the issue seems sound, though I did want to leave this breadcrumb in case anyone’s searching for answers in the same state as me and ends up here. I’m going to go to the lombok project and propose being able to append annotations to the generated builders, as it both solves the issue at hand with Jackson and extends an existing Lombok paradigm.

1reaction
cowtowncodercommented, Jan 29, 2019

@ChrisRenke Thank you for sharing this. Depending on how collaboration with Lombok team goes, I am open to considering this problem, and it may make sense to file a new issue focused specifically around case of builders generated by packages like Lombok (auto-values, immutables; there seem to be a few around).

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - UnrecognizedPropertyException when using Jackson ...
It fails when it's deserializing the @id field that was added by the @JsonIdentityInfo . You need to handle the @id field.
Read more >
Jackson Unmarshalling JSON with Unknown Properties
The goal is to simply ignore any JSON properties that cannot be mapped to an existing Java field. For example, say we need...
Read more >
Java – Builder pattern with Jackson for deserializing - iTecNote
I tried @JsonDeserialize(builder = MyBuilder.class) and is not working. This is required in REST jersey. I am currently jersey-media-jackson maven module ...
Read more >
JsonPOJOBuilder (jackson-databind 2.8.0 API) - FasterXML
Note that this annotation is NOT used to define what is the Builder class for a ... example JsonIgnoreProperties can be used to...
Read more >
Security update for jackson-databind, jackson-dataformats ...
+ '@JsonValue' with integer for enum does not deserialize ... + JsonIgnoreProperties(ignoreUnknown = true) does not work on field and method ...
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