Skip to content

Commit 62b4247

Browse files
committed
Fix #5342: Prevent @JsonAnyGetter from overriding @JsonProperty conflicts
1 parent 1fc539c commit 62b4247

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

src/main/java/tools/jackson/databind/ser/BeanSerializerFactory.java

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -388,11 +388,35 @@ protected ValueSerializer<Object> constructBeanOrAddOnSerializer(SerializationCo
388388
int anyGetterIndex = -1;
389389
for (int i = 0; i < props.size(); i++) {
390390
BeanPropertyWriter prop = props.get(i);
391-
// Either any-getter as field...
392-
if (Objects.equals(prop.getName(), anyGetter.getName())
393-
// or as method
394-
|| Objects.equals(prop.getMember().getMember(), anyGetter.getMember()))
395-
{
391+
AnnotatedMember propMember = prop.getMember();
392+
if (propMember == null) {
393+
continue;
394+
}
395+
396+
boolean matches = false;
397+
// [databind#5342]: Match only when the BeanPropertyWriter uses the same underlying member
398+
// (method or field) as the @JsonAnyGetter accessor.
399+
if (Objects.equals(propMember.getMember(), anyGetter.getMember())) {
400+
matches = true;
401+
} else if (anyGetter instanceof AnnotatedField
402+
&& propMember instanceof AnnotatedMethod) {
403+
String anyName = anyGetter.getName();
404+
String mName = propMember.getName();
405+
String capitalized = "";
406+
407+
if (anyName.isEmpty()) {
408+
capitalized = anyName;
409+
} else if (anyName.length() == 1) {
410+
capitalized = anyName.toUpperCase(Locale.ROOT);
411+
} else {
412+
capitalized = Character.toUpperCase(anyName.charAt(0)) + anyName.substring(1);
413+
}
414+
if (mName.equals("get" + capitalized) || mName.equals("is" + capitalized)) {
415+
matches = true;
416+
}
417+
}
418+
419+
if (matches) {
396420
anyGetterProp = prop;
397421
anyGetterIndex = i;
398422
break;

src/test/java/tools/jackson/databind/tofix/AnyGetterNameConflictSerialization5342Test.java renamed to src/test/java/tools/jackson/databind/ser/AnyGetterNameConflictSerialization5342Test.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package tools.jackson.databind.tofix;
1+
package tools.jackson.databind.ser;
22

33
import java.util.HashMap;
44
import java.util.Map;
@@ -9,7 +9,6 @@
99

1010
import tools.jackson.databind.ObjectMapper;
1111
import tools.jackson.databind.testutil.DatabindTestUtil;
12-
import tools.jackson.databind.testutil.failure.JacksonTestFailureExpected;
1312

1413
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
1514

@@ -50,7 +49,6 @@ public void hidden(Map<String, Object> additionalPropertiesProperty) {
5049

5150
private final ObjectMapper MAPPER = newJsonMapper();
5251

53-
@JacksonTestFailureExpected
5452
@Test
5553
public void anyGetter5342()
5654
throws Exception

0 commit comments

Comments
 (0)