Skip to content
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

Fix @JsonAnySetter issue with "setter" method (related to #4639) #4790

Merged
merged 3 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,9 @@ protected Object _deserializeUsingPropertyBased(final JsonParser p, final Deseri
if (_anySetter != null) {
try {
// [databind#4639] Since 2.18.1 AnySetter might not part of the creator, but just some field.
if (_anySetter.isFieldType()) {
if (_anySetter.isFieldType() ||
// [databind#4639] 2.18.2: Also should account for setter type :-/
_anySetter.isSetterType()) {
buffer.bufferAnyProperty(_anySetter, propName, _anySetter.deserialize(p, ctxt));
} else {
buffer.bufferAnyParameterProperty(_anySetter, propName, _anySetter.deserialize(p, ctxt));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,13 @@ Object readResolve() {
*/
public boolean isFieldType() { return _setterIsField; }

/**
* Method called to check whether this property is method
*
* @return 2.18.2
*/
public boolean isSetterType() { return _setter instanceof AnnotatedMethod; }

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

import java.util.HashMap;
import java.util.Map;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
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
Expand All @@ -30,13 +32,49 @@ public Bean(@JsonProperty("b") int b, @JsonProperty("d") int d) {
}
}

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

@JsonCreator
public SomeBean(@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);
}
}

final ObjectMapper MAPPER = newJsonMapper();

@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);
Bean bean = MAPPER.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);
}

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

SomeBean bean = MAPPER.readValue(json, SomeBean.class);
assertEquals(2, bean.b);
assertEquals(4, bean.d);

Expand Down