Skip to content

Commit

Permalink
Try fixing #4302 different way
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jan 11, 2024
1 parent 2aa21dd commit 4b6f8ac
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1123,15 +1123,16 @@ protected void _renameProperties(Map<String, POJOPropertyBuilder> props)
protected void _renameUsing(Map<String, POJOPropertyBuilder> propMap,
PropertyNamingStrategy naming)
{
// [databind#4302] since 2.17, Need to skip renaming for Enum properties
if (_type.isEnumType()) {
return;
}

POJOPropertyBuilder[] props = propMap.values().toArray(new POJOPropertyBuilder[propMap.size()]);
propMap.clear();
for (POJOPropertyBuilder prop : props) {
PropertyName fullName = prop.getFullName();
String rename = null;
// [databind#4302] since 2.17, Need to skip renaming for Enum properties
if (!prop.hasSetter() && prop.getPrimaryType().isEnumType()) {
continue;
}
// As per [databind#428] need to skip renaming if property has
// explicitly defined name, unless feature is enabled
if (!prop.isExplicitlyNamed() || _config.isEnabled(MapperFeature.ALLOW_EXPLICIT_PROPERTY_RENAMING)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

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

import static com.fasterxml.jackson.databind.BaseMapTest.a2q;
import static com.fasterxml.jackson.databind.BaseMapTest.jsonMapperBuilder;
import static com.fasterxml.jackson.databind.BaseTest.q;
import static com.fasterxml.jackson.databind.BaseMapTest.q;

// [databind#4302]
public class EnumSameName4302Test
{

enum Field4302Enum {
FOO(0);

Expand Down Expand Up @@ -52,12 +52,21 @@ public void setCat(String cat) {
}
}

static class Field4302Wrapper {
public Field4302Enum wrapped;

Field4302Wrapper() { }
public Field4302Wrapper(Field4302Enum w) {
wrapped = w;
}
}

private final ObjectMapper MAPPER = jsonMapperBuilder()
.propertyNamingStrategy(PropertyNamingStrategies.LOWER_CASE)
.build();

@Test
void testShouldWork() throws Exception
void testStandaloneShouldWork() throws Exception
{
// First, try roundtrip with same-ignore-case name field
assertEquals(Field4302Enum.FOO,
Expand All @@ -78,5 +87,17 @@ void testShouldWork() throws Exception
assertEquals(q("CAT"),
MAPPER.writeValueAsString(Setter4302Enum.CAT));
}

@Test
void testWrappedShouldWork() throws Exception
{
// First, try roundtrip with same-ignore-case name field
Field4302Wrapper input = new Field4302Wrapper(Field4302Enum.FOO);
String json = MAPPER.writeValueAsString(input);
assertEquals(a2q("{'wrapped':'FOO'}"), json);

Field4302Wrapper result = MAPPER.readValue(json, Field4302Wrapper.class);
assertEquals(Field4302Enum.FOO, result.wrapped);
}
}

0 comments on commit 4b6f8ac

Please sign in to comment.