Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JsonIgnoreProperties(ignoreUnknown = true) does not work on field and method level #2627

Closed
robotmrv opened this issue Feb 24, 2020 · 2 comments
Milestone

Comments

@robotmrv
Copy link

robotmrv commented Feb 24, 2020

Expected Result:

com.fasterxml.jackson.annotation.JsonIgnoreProperties#ignoreUnknown should work on filed/getter level the same way as com.fasterxml.jackson.annotation.JsonIgnoreProperties#value works

Actual Result:

com.fasterxml.jackson.annotation.JsonIgnoreProperties#ignoreUnknown works only on Class level.
and fails with exception

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "extra" (class com.example.IgnoreUnknownTest$MyPojo), not marked as ignorable (one known property: "name"])
 at [Source: (String)"{"value": {"name": "my_name", "extra": "val"}, "type":"Json"}"; line: 1, column: 41] (through reference chain: com.example.IgnoreUnknownTest$MyPojoValue["value"]->com.example.IgnoreUnknownTest$MyPojo["extra"])

	at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:61)
	at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:823)
	at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1153)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1589)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1567)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:294)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
	at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:530)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeWithErrorWrapping(BeanDeserializer.java:528)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeUsingPropertyBased(BeanDeserializer.java:417)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1287)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:326)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:159)
	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4014)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3005)
	at com.example.IgnoreUnknownTest.shouldDeserializeWithUnknownProperties(IgnoreUnknownTest.java:17)

Test case

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.assertj.core.api.Assertions;
import org.junit.Test;

import java.beans.ConstructorProperties;
import java.io.IOException;

public class IgnoreUnknownTest {

    @Test
    public void shouldDeserializeWithUnknownProperties() throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        String json = "{\"value\": {\"name\": \"my_name\", \"extra\": \"val\"}, \"type\":\"Json\"}";
        MyPojoValue value = objectMapper.readValue(json, MyPojoValue.class);
        Assertions.assertThat(value).isNotNull();
        Assertions.assertThat(value.getValue()).isNotNull();
        Assertions.assertThat(value.getValue().getName()).isEqualTo("my_name");
    }

    @JsonIgnoreProperties(ignoreUnknown = true)
    abstract static class GenericHolder<T> {
        @JsonIgnoreProperties(ignoreUnknown = true)// <-- this does not work
//        @JsonIgnoreProperties("extra") //with this test passes
        private final T value;

        public GenericHolder(T value) {
            this.value = value;
        }

        public T getValue() {
            return value;
        }

        @Override
        public String toString() {
            return getClass().getSimpleName() + "{" +
                    "value=" + value +
                    '}';
        }
    }

    static class MyPojoValue extends GenericHolder<MyPojo> {
        @ConstructorProperties("value")
        public MyPojoValue(MyPojo value) {
            super(value);
        }
    }

    static class MyPojo {
        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return getClass().getSimpleName() + "{" +
                    "name='" + name + '\'' +
                    '}';
        }
    }
}

Jackson versions:

com.fasterxml.jackson.core:jackson-annotations:2.9.10
com.fasterxml.jackson.core:jackson-core:2.9.10
com.fasterxml.jackson.core:jackson-databind:2.9.10.2

reproduces with:
com.fasterxml.jackson.core:jackson-annotations:2.10.3-SNAPSHOT
com.fasterxml.jackson.core:jackson-core:2.10.3-SNAPSHOT
com.fasterxml.jackson.core:jackson-databind:2.10.3-SNAPSHOT

com.fasterxml.jackson.core:jackson-annotations:2.11.0-SNAPSHOT
com.fasterxml.jackson.core:jackson-core:2.11.0-SNAPSHOT
com.fasterxml.jackson.core:jackson-databind:2.11.0-SNAPSHOT

com.fasterxml.jackson.core:jackson-annotations:3.0-SNAPSHOT
com.fasterxml.jackson.core:jackson-core:3.0.0-SNAPSHOT
com.fasterxml.jackson.core:jackson-databind:3.0.0-SNAPSHOT

@cowtowncoder
Copy link
Member

Thank you for reporting this. Seems like a problem.

@cowtowncoder
Copy link
Member

Ok, I can reproduce this with simplified test. Seems to have occurred with 2.9 and earlier; in a way this is not a feature that was necessarily intended (and hence no tests to verify). But it seems reasonable to expect it should work similar to how listing explicit names works, so I will see if it could be made to work.
Depending on scope of changes (assuming fix found), may go either in 2.10 patch (small changes), or 2.11.0 (bigger changes).

cowtowncoder added a commit that referenced this issue Mar 25, 2020
@cowtowncoder cowtowncoder added 2.11 and removed 2.10 labels Mar 31, 2020
@cowtowncoder cowtowncoder added this to the 2.11.0 milestone Mar 31, 2020
@cowtowncoder cowtowncoder removed the 2.11 label Apr 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants