Skip to content

Fix regression #4639 #4738

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

Merged
merged 4 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ Project: jackson-databind
#4508: Deserialized JsonAnySetter field in Kotlin data class is null
(reported by @MaximValeev)
(fix by Joo-Hyuk K)
#4639: @JsonAnySetter on field ignoring unrecognized properties if they are
declared before the last recognized properties in JSON
(reported by Sim Y-T)
(fix by Joo-Hyuk K)
#4718: Should not fail on trying to serialize `java.time.DateTimeException`
#4724: Deserialization behavior change with Records, `@JsonCreator` and
`@JsonValue` between 2.17 and 2.18
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,12 @@ protected Object _deserializeUsingPropertyBased(final JsonParser p, final Deseri
// "any property"?
if (_anySetter != null) {
try {
buffer.bufferAnyParameterProperty(_anySetter, propName, _anySetter.deserialize(p, ctxt));
// [databind#4639] Since 2.18.1 AnySetter might not part of the creator, but just some field.
if (_anySetter.isFieldType()) {
buffer.bufferAnyProperty(_anySetter, propName, _anySetter.deserialize(p, ctxt));
} else {
buffer.bufferAnyParameterProperty(_anySetter, propName, _anySetter.deserialize(p, ctxt));
}
Comment on lines +508 to +513
Copy link
Member Author

@JooHyukKim JooHyukKim Oct 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While resolving #562, I missed case like #4639 where there is creator, but any-setter is field.

} catch (Exception e) {
wrapAndThrow(e, _beanType.getRawClass(), propName, ctxt);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ Object readResolve() {
*/
public int getParameterIndex() { return -1; }

/**
* Method called to check whether this property is field
*
* @since 2.18.1
*/
public boolean isFieldType() { return _setterIsField; }

/**
* Create an instance of value to pass through Creator parameter.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.fasterxml.jackson.databind.deser;

import java.util.Map;

import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

// [databind#4639] 2.18.1 : regression when using @JsonAnySetter outside of @JsonCreator
public class JsonAnySetterFieldWithCreatorDeser4639Test
extends DatabindTestUtil
{

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;
}
}

@Test
public void testJsonAnySetter()
throws Exception
{
String json = "{\"a\":1,\"b\":2,\"c\":3,\"d\":4,\"e\":5,\"f\":6}";

Bean bean = newJsonMapper().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(mapOf("a", 1, "c", 3, "e", 5, "f", 6), bean.any);
}

private Map<String, Integer> mapOf(String a, int i, String b, int i1, String c, int i2, String d, int i3)
{
Map<String, Integer> map = new java.util.HashMap<>();
map.put(a, i);
map.put(b, i1);
map.put(c, i2);
map.put(d, i3);
return map;
}

}