jackson-databind: `@JsonAnySetter` on field ignoring unrecognized properties if they are declared before the last recognized properties in JSON

Search before asking

  • I searched in the issues and found nothing similar.

Describe the bug

Say a bean class only recognize property/field p1, and we have the following JSONs:

{
    "p1": ..., // goes to p1 field
    "p2": ..., // goes to @JsonAnySetter field
    "p3": ... // goes to @JsonAnySetter field
}

{
    "p2": ..., // goes missing ⚠
    "p1": ..., // goes to p1 field
    "p3": ... // goes to @JsonAnySetter field
}


{
    "p2": ..., // goes missing ⚠
    "p3": ..., // goes missing ⚠
    "p1": ... // goes to p1 field
}

Version Information

2.18

Reproduction

public static class Bean {

    private int b;
    private int d;
    @JsonAnySetter
    private Map<String, ?> any;

    @JsonCreator
    public Bean(@JsonProperty("b") int b, @JsonProperty("d") int d) {
        this.b = b;
        this.d = d;
    }
}
...

String json = "{\"a\":1,\"b\":2,\"c\":3,\"d\":4,\"e\":5,\"f\":6}";

Bean bean = new ObjectMapper().readValue(json, Bean.class);
assertEquals(2, bean.b);
assertEquals(4, bean.d);
// failed with:
// org.opentest4j.AssertionFailedError: 
// Expected :{b=2, c=3, e=5, f=6}
// Actual   :{e=5, f=6}
assertEquals(Map.of("a", 1, "c", 3, "e", 5, "f", 6), bean.any);

Expected behavior

No response

Additional context

  • Not reproducible for @JsonAnySetter on constructor parameter.
  • Not reproducible when I tested by reverting #4558 on my local machine.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 6
  • Comments: 15 (7 by maintainers)

Commits related to this issue

Most upvoted comments

we are talking about 2.18 not 2.8. Just to clear things up 😉 @davsclaus

Oh thanks yeah so many version numbers - Again thanks for all the hard work as OSS maintainer. I know the feeling and how much it takes to care and sit and do this year after year.

@bsa01 We don’t usually re-open closed issues (too confusing for release notes), so I was to suggest a new issue. But since @JooHyukKim created PR, I think we are ok.

Hi @cowtowncoder. I got the same problem with 2.18.1 but instead when using @JsonAnySetter at setter level instead of field level. Do you want me to open another issue, or track it here?

public class Bean {
    final int                b;
    final int                d;
    final Map<String,Object> any = new HashMap<>();

    @JsonCreator
    public Bean(@JsonProperty("b") int b, @JsonProperty("d") int d) {
        this.b = b;
        this.d = d;
    }

    @JsonAnySetter
    public void setAny(String name, Object value) {
        any.put(name, value);
    }
}
...
String json = "{\"a\":1,\"b\":2,\"c\":3,\"d\":4,\"e\":5,\"f\":6}";
Bean bean = new ObjectMapper().readValue(json, Bean.class);
assertEquals(2, bean.b);
assertEquals(4, bean.d);
assertEquals(Map.of("a", 1, "c", 3, "e", 5, "f", 6), bean.any);
// failed with:
// Expected :{f=6, e=5, c=3, a=1}
// Actual   :{e=5, f=6}

That’s a fair point, @cowtowncoder. I will need to provide a reproducer as the issue is most likely in the same are but the case is not as simple as the test case added with the fix which is clearly passing.

@erdi You may need to file a new issue outlining specific remaining problem, with reproduction (sounds like there are multiple problems within same feature). And as usual, if reproducible with Java-only, issue for jackson-databind; if just for Kotlin, on jackson-module-kotlin.

The important bit is that there is a fix ready to be merged so that the next release, whenever it happens, will no longer contain the regression. Thanks.