Jackson-databind: @JsonIgnoreProperties ignoreUnknown does not work with @JsonDeserialize builder

Created on 25 Apr 2016  路  5Comments  路  Source: FasterXML/jackson-databind

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.

Most helpful comment

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.

All 5 comments

Ah. Ok, so... looks like annotation is found from Builder class, not target bean. This makes sense from implementation perspective since that's also where inclusion/exclusion and other criteria is specified. Target class is only used for finding custom handlers like builder.

I can see that this is confusing, so I'll have to think about whether something should be done to allow usage on target class too. It would probably be possible to do that but I'll have to think of whether it'd less or more confusing, given that some annotations absolutely need to be in builder class anyway.
So it may be that this would be more of a documentation issue.

After thinking it through I think current behavior makes most sense, even if it means that some annotations are needed for builder, and others for target class. Mismatch of property names really is relevant for builder (since that's where "setter" method is needed; nothing in target type is ever called).
I updated javadocs a bit to hopefully hint at solution for others who may encounter this problem.

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.

@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).

@cowtowncoder Off topic - I find myself in a situation with Lombok actually - https://github.com/rzwitserloot/lombok/issues/1114

What other auto generating packages are around? I thought Lombok was the "defacto" but its such a slow moving project can't really expect anything not available sigh

Was this page helpful?
0 / 5 - 0 ratings