Skip to content

Commit 48de322

Browse files
authored
Fix #4302 differently (#4314)
1 parent 2aa21dd commit 48de322

File tree

4 files changed

+46
-7
lines changed

4 files changed

+46
-7
lines changed

src/main/java/com/fasterxml/jackson/databind/introspect/Annotated.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ public boolean isPublic() {
3636
return Modifier.isPublic(getModifiers());
3737
}
3838

39+
/**
40+
* @since 2.17
41+
*/
42+
public boolean isStatic() {
43+
return Modifier.isStatic(getModifiers());
44+
}
45+
3946
public abstract String getName();
4047

4148
/**

src/main/java/com/fasterxml/jackson/databind/introspect/POJOPropertiesCollector.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,10 +1128,6 @@ protected void _renameUsing(Map<String, POJOPropertyBuilder> propMap,
11281128
for (POJOPropertyBuilder prop : props) {
11291129
PropertyName fullName = prop.getFullName();
11301130
String rename = null;
1131-
// [databind#4302] since 2.17, Need to skip renaming for Enum properties
1132-
if (!prop.hasSetter() && prop.getPrimaryType().isEnumType()) {
1133-
continue;
1134-
}
11351131
// As per [databind#428] need to skip renaming if property has
11361132
// explicitly defined name, unless feature is enabled
11371133
if (!prop.isExplicitlyNamed() || _config.isEnabled(MapperFeature.ALLOW_EXPLICIT_PROPERTY_RENAMING)) {

src/main/java/com/fasterxml/jackson/databind/introspect/POJOPropertyBuilder.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,21 @@ public AnnotatedField getField()
623623
continue;
624624
}
625625
}
626+
// 11-Jan-2024, tatu: Wrt [databind#4302] problem here is that we have
627+
// Enum constant fields (static!) added due to change in 2.16.0 (see
628+
// {@code AnnotatedFieldCollector#_isIncludableField}) and they can
629+
// conflict with actual fields.
630+
/// Let's resolve conflict in favor of non-static Field.
631+
final boolean currStatic = field.isStatic();
632+
final boolean nextStatic = nextField.isStatic();
633+
634+
if (currStatic != nextStatic) {
635+
if (currStatic) {
636+
field = nextField;
637+
}
638+
continue;
639+
}
640+
626641
throw new IllegalArgumentException("Multiple fields representing property \""+getName()+"\": "
627642
+field.getFullName()+" vs "+nextField.getFullName());
628643
}

src/test/java/com/fasterxml/jackson/databind/deser/enums/EnumSameName4302Test.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77

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

10+
import static com.fasterxml.jackson.databind.BaseMapTest.a2q;
1011
import static com.fasterxml.jackson.databind.BaseMapTest.jsonMapperBuilder;
11-
import static com.fasterxml.jackson.databind.BaseTest.q;
12+
import static com.fasterxml.jackson.databind.BaseMapTest.q;
1213

1314
// [databind#4302]
1415
public class EnumSameName4302Test
1516
{
16-
1717
enum Field4302Enum {
1818
FOO(0);
1919

@@ -52,12 +52,21 @@ public void setCat(String cat) {
5252
}
5353
}
5454

55+
static class Field4302Wrapper {
56+
public Field4302Enum wrapped;
57+
58+
Field4302Wrapper() { }
59+
public Field4302Wrapper(Field4302Enum w) {
60+
wrapped = w;
61+
}
62+
}
63+
5564
private final ObjectMapper MAPPER = jsonMapperBuilder()
5665
.propertyNamingStrategy(PropertyNamingStrategies.LOWER_CASE)
5766
.build();
5867

5968
@Test
60-
void testShouldWork() throws Exception
69+
void testStandaloneShouldWork() throws Exception
6170
{
6271
// First, try roundtrip with same-ignore-case name field
6372
assertEquals(Field4302Enum.FOO,
@@ -78,5 +87,17 @@ void testShouldWork() throws Exception
7887
assertEquals(q("CAT"),
7988
MAPPER.writeValueAsString(Setter4302Enum.CAT));
8089
}
90+
91+
@Test
92+
void testWrappedShouldWork() throws Exception
93+
{
94+
// First, try roundtrip with same-ignore-case name field
95+
Field4302Wrapper input = new Field4302Wrapper(Field4302Enum.FOO);
96+
String json = MAPPER.writeValueAsString(input);
97+
assertEquals(a2q("{'wrapped':'FOO'}"), json);
98+
99+
Field4302Wrapper result = MAPPER.readValue(json, Field4302Wrapper.class);
100+
assertEquals(Field4302Enum.FOO, result.wrapped);
101+
}
81102
}
82103

0 commit comments

Comments
 (0)