Skip to content

Commit 9036c7f

Browse files
committed
Revert back the fix: #5342 probably not fixable as it is (esp. without breaking #4388)
1 parent 00dc340 commit 9036c7f

File tree

3 files changed

+37
-48
lines changed

3 files changed

+37
-48
lines changed

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

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -383,49 +383,35 @@ protected ValueSerializer<Object> constructBeanOrAddOnSerializer(SerializationCo
383383
BeanProperty.Std anyProp = new BeanProperty.Std(name, valueType, null,
384384
anyGetter, PropertyMetadata.STD_OPTIONAL);
385385

386-
// Check if there is an accessor exposed for the anyGetter
387-
BeanPropertyWriter anyGetterProp = null;
388-
int anyGetterIndex = -1;
389-
for (int i = 0; i < props.size(); i++) {
390-
BeanPropertyWriter prop = props.get(i);
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;
386+
// Check if there is an accessor exposed for the anyGetter.
387+
// First: by physical accessor (same Getter method or Field)
388+
final int propCount = props.size();
389+
int ix = -1;
390+
// [databind#5342]: First match only when the BeanPropertyWriter uses the same
391+
// underlying member (method or field) as the @JsonAnyGetter accessor.
392+
for (int i = 0; i < propCount; i++) {
393+
AnnotatedMember propMember = props.get(i).getMember();
394+
if (propMember != null) {
395+
if (Objects.equals(propMember.getMember(), anyGetter.getMember())) {
396+
ix = i;
397+
break;
416398
}
417399
}
418-
419-
if (matches) {
420-
anyGetterProp = prop;
421-
anyGetterIndex = i;
422-
break;
400+
}
401+
// If that doesn't work, try match by logical property name
402+
if (ix < 0) {
403+
final String anyName = anyGetter.getName();
404+
for (int i = 0; i < propCount; i++) {
405+
if (Objects.equals(anyName, props.get(i).getName())) {
406+
ix = i;
407+
break;
408+
}
423409
}
424410
}
425-
if (anyGetterIndex != -1) {
426-
// There is prop is already in place, just need to replace it
427-
AnyGetterWriter anyGetterWriter = new AnyGetterWriter(anyGetterProp, anyProp, anyGetter, anySer);
428-
props.set(anyGetterIndex, anyGetterWriter);
411+
if (ix >= 0) {
412+
BeanPropertyWriter anyGetterProp = props.get(ix);
413+
// There is prop already in place, just need to replace it
414+
props.set(ix, new AnyGetterWriter(anyGetterProp, anyProp, anyGetter, anySer));
429415
} else {
430416
// Otherwise just add it at the end, but won't be sorted...
431417
// This is case where JsonAnyGetter is private/protected,

src/test/java/tools/jackson/databind/ser/AnyGetterOrdering4388Test.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@ public String getKey() {
140140
}
141141
}
142142

143-
@JsonPropertyOrder({ "firstProperty", "secondProperties", "thirdProperty", "forthProperty" })
143+
@JsonPropertyOrder({ "firstProperty", "secondProperties", "thirdProperty", "fourthProperty" })
144144
static class PrivateAnyGetterPojo {
145-
public int firstProperty = 1, forthProperty = 4, thirdProperty = 3;
145+
public int firstProperty = 1, fourthProperty = 4, thirdProperty = 3;
146146

147147
@JsonAnyGetter
148148
private Map<String, Object> secondProperties = new HashMap<>();
@@ -157,7 +157,7 @@ public Map<String, Object> secondProperties() {
157157
}
158158
}
159159

160-
@JsonPropertyOrder({ "firstProperty", "secondProperties", "thirdProperty", "forthProperty" })
160+
@JsonPropertyOrder({ "firstProperty", "secondProperties", "thirdProperty", "fourthProperty" })
161161
static class PrivateAnyGetterPojoSorted extends PrivateAnyGetterPojo {
162162
public Map<String, Object> getSecondProperties() {
163163
return super.secondProperties;
@@ -338,7 +338,7 @@ public void testPrivateAnyGetter() throws Exception {
338338
assertEquals(a2q("{" +
339339
"'firstProperty':1," +
340340
"'thirdProperty':3," +
341-
"'forthProperty':4," +
341+
"'fourthProperty':4," +
342342
"'secondProperty':2}"), // private accesor, wont' work here
343343
json);
344344
}
@@ -353,7 +353,7 @@ public void testPrivateAnyGetterSorted() throws Exception {
353353
"'firstProperty':1," +
354354
"'secondProperty':2," + // private accesor, wont' work here
355355
"'thirdProperty':3," +
356-
"'forthProperty':4}"),
356+
"'fourthProperty':4}"),
357357
json);
358358
}
359359

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

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

33
import java.util.HashMap;
44
import java.util.Map;
@@ -9,13 +9,17 @@
99

1010
import tools.jackson.databind.ObjectMapper;
1111
import tools.jackson.databind.testutil.DatabindTestUtil;
12+
import tools.jackson.databind.testutil.failure.JacksonTestFailureExpected;
1213

1314
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
1415

1516
import static org.junit.jupiter.api.Assertions.assertEquals;
1617
import static org.junit.jupiter.api.Assertions.assertNotNull;
1718

18-
// [databind#5342] JsonAnyGetter method serialization can override JsonProperty serialization on serialized name conflict
19+
// [databind#5342] JsonAnyGetter method serialization can override JsonProperty
20+
// serialization on serialized name conflict
21+
//
22+
// No plans to fix: left in place for now
1923
public class AnyGetterNameConflictSerialization5342Test
2024
extends DatabindTestUtil
2125
{
@@ -49,6 +53,7 @@ public void hidden(Map<String, Object> additionalPropertiesProperty) {
4953

5054
private final ObjectMapper MAPPER = newJsonMapper();
5155

56+
@JacksonTestFailureExpected
5257
@Test
5358
public void anyGetter5342()
5459
throws Exception
@@ -60,7 +65,6 @@ public void anyGetter5342()
6065
hidden.put("fizz", "buzz");
6166
pojo.hidden(hidden);
6267

63-
6468
String JSON = MAPPER.writeValueAsString(pojo);
6569
// was in 2.18 : {"foo":"bar","additionalProperties": {"fizz":"buzz"}}
6670
// now in 2.19 : {"foo":"bar"}... need FIX!
@@ -76,5 +80,4 @@ public void anyGetter5342()
7680
assertNotNull(actual.hidden());
7781
assertEquals(1, actual.hidden().size());
7882
}
79-
8083
}

0 commit comments

Comments
 (0)