Skip to content

@JsonAnySetter on field ignoring unrecognized properties if they are declared before the last recognized properties in JSON #4639

@yihtserns

Description

@yihtserns

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    2.18Issues planned at 2.18 or later

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions