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
@JsonAnySetteron 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
- Fix regression #4639 (#4738) — committed to FasterXML/jackson-databind by JooHyukKim 2 years ago
- Initial fix for #4639 — committed to JooHyukKim/jackson-databind by JooHyukKim 2 years ago
- Fix `@JsonAnySetter` issue with "setter" method (related to #4639) (#4790) — committed to FasterXML/jackson-databind by JooHyukKim 2 years ago
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?
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.