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 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
7 changes: 6 additions & 1 deletion release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -1730,6 +1730,11 @@ Jan Pachol (janpacho@github)
(2.16.0)

Pieter Dirk Soels (Badbond@github)
* Reprted #4302: Problem deserializing some type of Enums when using
* Reported #4302: Problem deserializing some type of Enums when using
`PropertyNamingStrategy`
(2.16.2)

Stephane Bailliez (sbailliez@github)
* Reported #4409: Deserialization of enums with name defined with different cases
leads to `InvalidDefinitionException`: Multiple fields representing property
(2.16.2)
4 changes: 4 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ Project: jackson-databind
#4355: Jackson 2.16 fails attempting to obtain `ObjectWriter` for an `Enum` of which
some value returns null from `toString()`
(reported by @YutaHiguchi-bsn)
#4409: Deserialization of enums with name defined with different cases leads to
`InvalidDefinitionException`: Multiple fields representing property
(reported by Stephane B)
(fix contributed by Joo-Hyuk K)

2.16.1 (24-Dec-2023)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.fasterxml.jackson.annotation.JacksonInject;
import com.fasterxml.jackson.annotation.JsonCreator;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.cfg.HandlerInstantiator;
import com.fasterxml.jackson.databind.cfg.MapperConfig;
Expand Down Expand Up @@ -1123,6 +1124,10 @@ protected void _renameProperties(Map<String, POJOPropertyBuilder> props)
protected void _renameUsing(Map<String, POJOPropertyBuilder> propMap,
PropertyNamingStrategy naming)
{
// [databind#4409]: Need to skip renaming for Enums, unless Enums are handled as OBJECT format
if (_type.isEnumType() && (_findFormatShape() != JsonFormat.Shape.OBJECT)) {
return;
}
POJOPropertyBuilder[] props = propMap.values().toArray(new POJOPropertyBuilder[propMap.size()]);
propMap.clear();
for (POJOPropertyBuilder prop : props) {
Expand Down Expand Up @@ -1171,6 +1176,29 @@ protected void _renameUsing(Map<String, POJOPropertyBuilder> propMap,
}
}

/**
* Helper method called to check if given property should be renamed using {@link PropertyNamingStrategies}.
*<p>
* NOTE: copied+simplified version of {@code BasicBeanDescription.findExpectedFormat()}.
*
* @since 2.16.2
*/
private JsonFormat.Shape _findFormatShape()
{
JsonFormat.Shape shape = null;
JsonFormat.Value format = _annotationIntrospector.findFormat(_classDef);
if (format != null) {
shape = format.getShape();
}
JsonFormat.Value defFormat = _config.getDefaultPropertyFormat(_classDef.getRawType());
if (defFormat != null) {
if (shape == null) {
shape = defFormat.getShape();
}
}
return shape;
}

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
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