Skip to content

Commit

Permalink
Fix regression #4639 (#4738)
Browse files Browse the repository at this point in the history
  • Loading branch information
JooHyukKim authored Oct 10, 2024
1 parent cc05d34 commit 365cee6
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
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));
}
} 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;
}

}

0 comments on commit 365cee6

Please sign in to comment.