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

Make PropertyNamingStrategy not affect Enums #4414

Merged
merged 5 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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,9 +1128,7 @@ protected void _renameUsing(Map<String, POJOPropertyBuilder> propMap,
for (POJOPropertyBuilder prop : props) {
PropertyName fullName = prop.getFullName();
String rename = null;
// 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)) {
if (_shouldRename(prop)) {
if (_forSerialization) {
if (prop.hasGetter()) {
rename = naming.nameForGetterMethod(_config, prop.getGetter(), fullName.getSimpleName());
Expand Down Expand Up @@ -1171,6 +1169,27 @@ protected void _renameUsing(Map<String, POJOPropertyBuilder> propMap,
}
}

/**
* Helper method called to check if given property should be renamed using {@link PropertyNamingStrategies}
*
* @since 2.16
*/
private boolean _shouldRename(POJOPropertyBuilder prop) {
// [databind#4409]: Need to skip renaming for Enums, unless OBJECT format
if (getType().isEnumType() && Objects.equals(prop.getPrimaryType(), getType())) {
if (prop.isObjectFormatShape()) {
Copy link
Member

Choose a reason for hiding this comment

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

This is in wrong place: it would check for annotation of a property of Enum type referring to another type and being annotated as Shape.OBJECT -- that would not cover cases where Enum class is annotated or -- if supported, not sure it is -- property of another (almost certainly non-Enum) type pointing Enum type and having Shape override.

return true;
}
return false;
}
// 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)) {
return true;
}
return false;
}

protected void _renameWithWrappers(Map<String, POJOPropertyBuilder> props)
{
// 11-Sep-2012, tatu: To support 'MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import java.util.*;
import java.util.stream.Collectors;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSetter;
import com.fasterxml.jackson.annotation.Nulls;
import com.fasterxml.jackson.annotation.*;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.cfg.ConfigOverride;
Expand Down Expand Up @@ -1416,6 +1413,15 @@ protected Class<?> _rawTypeOf(AnnotatedMember m) {
return m.getType().getRawClass();
}

protected boolean isObjectFormatShape() {
JsonFormat.Value format = _annotationIntrospector.findFormat(getAccessor());
if (format != null) {
JsonFormat.Shape shape = format.getShape();
return shape == JsonFormat.Shape.OBJECT;
}
return false;
}

/*
/**********************************************************
/* Helper classes
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.fasterxml.jackson.databind.deser.enums;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;

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

import static com.fasterxml.jackson.databind.BaseMapTest.jsonMapperBuilder;

// [databind#4409]: PropertyNamingStrategy should not affect to Enums
class EnumWithNamingStrategy4409Test {

enum ColorMode {
RGB,
RGBa,
RGBA
}

static class Bug {
public ColorMode colorMode;
}

@Test
public void testEnumAndPropertyNamingStrategy() throws Exception {
ObjectMapper mapper = jsonMapperBuilder()
.propertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE)
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.build();

Bug bug = mapper.readValue("{ \"color_mode\": \"RGBa\"}", Bug.class);

// fails
assertEquals(ColorMode.RGBa, bug.colorMode);
}
}
Loading