Skip to content

Commit 2aa21dd

Browse files
authored
Make PropertyNamingStrategy skip renaming on Enums (#4311)
1 parent cc6a1ae commit 2aa21dd

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

release-notes/CREDITS-2.x

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1728,3 +1728,8 @@ Jan Pachol (janpacho@github)
17281728
* Reported #4175: Exception when deserialization of `private` record with
17291729
default constructor
17301730
(2.16.0)
1731+
1732+
Pieter Dirk Soels (Badbond@github)
1733+
* Reprted #4302: Problem deserializing some type of Enums when using
1734+
`PropertyNamingStrategy`
1735+
(2.16.2)

release-notes/VERSION-2.x

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ Project: jackson-databind
88

99
2.16.2 (not yet released)
1010

11+
#4302: Problem deserializing some type of Enums when using `PropertyNamingStrategy`
12+
(reported by Pieter D-S)
13+
(fix contributed by Joo-Hyuk K)
1114
#4303: `ObjectReader` is not serializable if it's configured for polymorphism
1215
(reported by @asardaes)
1316
(fix contributed by Joo-Hyuk K)

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,6 +1128,10 @@ 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+
}
11311135
// As per [databind#428] need to skip renaming if property has
11321136
// explicitly defined name, unless feature is enabled
11331137
if (!prop.isExplicitlyNamed() || _config.isEnabled(MapperFeature.ALLOW_EXPLICIT_PROPERTY_RENAMING)) {
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.fasterxml.jackson.databind.deser.enums;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
7+
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
10+
import static com.fasterxml.jackson.databind.BaseMapTest.jsonMapperBuilder;
11+
import static com.fasterxml.jackson.databind.BaseTest.q;
12+
13+
// [databind#4302]
14+
public class EnumSameName4302Test
15+
{
16+
17+
enum Field4302Enum {
18+
FOO(0);
19+
20+
public final int foo;
21+
22+
Field4302Enum(int foo) {
23+
this.foo = foo;
24+
}
25+
}
26+
27+
enum Getter4302Enum {
28+
BAR("bar");
29+
30+
public String bar;
31+
32+
Getter4302Enum(String bar) {
33+
this.bar = bar;
34+
}
35+
36+
public String getBar() {
37+
return "bar";
38+
}
39+
}
40+
41+
enum Setter4302Enum {
42+
CAT("dog");
43+
44+
public String cat;
45+
46+
Setter4302Enum(String cat) {
47+
this.cat = cat;
48+
}
49+
50+
public void setCat(String cat) {
51+
this.cat = cat;
52+
}
53+
}
54+
55+
private final ObjectMapper MAPPER = jsonMapperBuilder()
56+
.propertyNamingStrategy(PropertyNamingStrategies.LOWER_CASE)
57+
.build();
58+
59+
@Test
60+
void testShouldWork() throws Exception
61+
{
62+
// First, try roundtrip with same-ignore-case name field
63+
assertEquals(Field4302Enum.FOO,
64+
MAPPER.readValue("\"FOO\"", Field4302Enum.class));
65+
assertEquals(q("FOO"),
66+
MAPPER.writeValueAsString(Field4302Enum.FOO));
67+
68+
// Now, try roundtrip with same-ignore-case name getter
69+
assertEquals(Getter4302Enum.BAR,
70+
MAPPER.readValue("\"BAR\"", Getter4302Enum.class));
71+
assertEquals(q("BAR"),
72+
MAPPER.writeValueAsString(Getter4302Enum.BAR));
73+
74+
// Now, try roundtrip with same-ignore-case name setter
75+
Setter4302Enum.CAT.setCat("cat");
76+
assertEquals(Setter4302Enum.CAT,
77+
MAPPER.readValue("\"CAT\"", Setter4302Enum.class));
78+
assertEquals(q("CAT"),
79+
MAPPER.writeValueAsString(Setter4302Enum.CAT));
80+
}
81+
}
82+

0 commit comments

Comments
 (0)