Skip to content

Commit

Permalink
Fix #4302 differently (#4314)
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder authored Jan 12, 2024
1 parent 2aa21dd commit 48de322
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ public boolean isPublic() {
return Modifier.isPublic(getModifiers());
}

/**
* @since 2.17
*/
public boolean isStatic() {
return Modifier.isStatic(getModifiers());
}

public abstract String getName();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1128,10 +1128,6 @@ protected void _renameUsing(Map<String, POJOPropertyBuilder> propMap,
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 @@ -623,6 +623,21 @@ public AnnotatedField getField()
continue;
}
}
// 11-Jan-2024, tatu: Wrt [databind#4302] problem here is that we have
// Enum constant fields (static!) added due to change in 2.16.0 (see
// {@code AnnotatedFieldCollector#_isIncludableField}) and they can
// conflict with actual fields.
/// Let's resolve conflict in favor of non-static Field.
final boolean currStatic = field.isStatic();
final boolean nextStatic = nextField.isStatic();

if (currStatic != nextStatic) {
if (currStatic) {
field = nextField;
}
continue;
}

throw new IllegalArgumentException("Multiple fields representing property \""+getName()+"\": "
+field.getFullName()+" vs "+nextField.getFullName());
}
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 48de322

Please sign in to comment.