Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Put back enum field without renaming #4313

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1128,8 +1128,13 @@ 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()) {
// [databind#4302] since 2.16, Need to skip renaming for Enum instances (but not for Enum fields)
if (!prop.hasSetter()
&& Objects.nonNull(prop.getField())
&& (Objects.equals(_type, prop.getField().getType()) && prop.getPrimaryType().isEnumType())
) {
Comment on lines +1132 to +1135
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if there is cleaner solution for this... 🤔
Or just extract method to some where (probably not POJOPropertyBuilder, it doens't need to know about Enum stuff)

Copy link
Member

@cowtowncoder cowtowncoder Jan 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I should have noticed -- original "fix" is actually totally wrong.

It applies to properties with Enum value but what we want are properties WITHIN type that has properties.

I am not sure how it would have resolved the problem but I can look into fixing the problem. Odd databind had no test for naming strategy with Enum-valued POJO property.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First thing I think (beyond revert) is to add a test for naming strategy. I can do that.

// Just put back
propMap.put(fullName.getSimpleName(), prop);
JooHyukKim marked this conversation as resolved.
Show resolved Hide resolved
continue;
}
// As per [databind#428] need to skip renaming if property has
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

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

// [databind#4302]
Expand Down Expand Up @@ -52,10 +53,24 @@ public void setCat(String cat) {
}
}

enum AsField4302Enum {
APPLE_SUGAR,
SOME_PERSON;
}

static class AsField4302Bean {
public AsField4302Enum someEnum = AsField4302Enum.APPLE_SUGAR;
public String otherProp = "someOtherField";
}

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

private final ObjectMapper SNAKE_MAPPER = jsonMapperBuilder()
.propertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE)
.build();

@Test
void testShouldWork() throws Exception
{
Expand All @@ -78,5 +93,22 @@ void testShouldWork() throws Exception
assertEquals(q("CAT"),
MAPPER.writeValueAsString(Setter4302Enum.CAT));
}

@Test
void testDeserializeSuccessfulUnaffectedField() throws Exception
{
AsField4302Bean bean = new AsField4302Bean();

// test serialization
assertEquals(
a2q("{'some_enum':'APPLE_SUGAR','other_prop':'someOtherField'}"),
SNAKE_MAPPER.writeValueAsString(bean));

// test deserialization
AsField4302Bean result = SNAKE_MAPPER.readValue(
a2q("{'some_enum':'SOME_PERSON', 'other_prop':'thisField'}"), AsField4302Bean.class);
assertEquals(AsField4302Enum.SOME_PERSON, result.someEnum);
assertEquals("thisField", result.otherProp);
Comment on lines +107 to +111
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test corresponds to the failing test in ion module --when there is Enum field, we should keep renaming it, same as before

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right.

}
}