Skip to content

Commit

Permalink
Enable DeserializationFeature.READ_ENUMS_USING_TO_STRING by default…
Browse files Browse the repository at this point in the history
… (3.0) (#4566)
  • Loading branch information
JooHyukKim authored Jun 7, 2024
1 parent a419046 commit 1dface0
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 12 deletions.
2 changes: 2 additions & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ Versions: 3.x (for earlier see VERSION-2.x)
#4160: Deprecate `DefaultTyping.EVERYTHING` in `2.x` and remove in `3.0`
#4381: Prevent construction of `null`-valued `JsonNode`s (like `TextNode`)
#4552: Change `MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS` default to `false` for 3.0
#4566: Enable `DeserializationFeature.READ_ENUMS_USING_TO_STRING` by default (3.0)
(contributed by Joo-Hyuk K)
- Remove `MappingJsonFactory`
- Add context parameter for `TypeSerializer` contextualization (`forProperty()`)
- Default for `JsonNodeFeature.STRIP_TRAILING_BIGDECIMAL_ZEROES` changed to `false` for 3.0
12 changes: 6 additions & 6 deletions src/main/java/tools/jackson/databind/DeserializationFeature.java
Original file line number Diff line number Diff line change
Expand Up @@ -388,17 +388,17 @@ public enum DeserializationFeature implements ConfigFeature
ACCEPT_FLOAT_AS_INT(true),

/**
* Feature that determines standard deserialization mechanism used for
* Enum values: if enabled, Enums are assumed to have been serialized using
* return value of <code>Enum.toString()</code>;
* if disabled, return value of <code>Enum.name()</code> is assumed to have been used.
* Feature that determines the deserialization mechanism used for
* Enum values: if enabled, Enums are assumed to have been serialized using
* return value of {@code Enum.toString()};
* if disabled, return value of {@code Enum.name()} is assumed to have been used.
*<p>
* Note: this feature should usually have same value
* as {@link SerializationFeature#WRITE_ENUMS_USING_TO_STRING}.
*<p>
* Feature is disabled by default.
* Feature is enabled by default as of Jackson 3.0 (in 2.x it was disabled).
*/
READ_ENUMS_USING_TO_STRING(false),
READ_ENUMS_USING_TO_STRING(true),

/**
* Feature that allows unknown Enum values to be parsed as {@code null} values.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ public void testNoArgEnumCreator() throws Exception
@Test
public void testEnumCreators1291() throws Exception
{
ObjectMapper mapper = new ObjectMapper();
ObjectMapper mapper = jsonMapperBuilder().disable(DeserializationFeature.READ_ENUMS_USING_TO_STRING).build();
String json = mapper.writeValueAsString(Enum1291.V2);
Enum1291 result = mapper.readValue(json, Enum1291.class);
assertSame(Enum1291.V2, result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ enum MixinOverloadedDefault {
/**********************************************************
*/

private final ObjectMapper MAPPER = newJsonMapper();
private final ObjectMapper MAPPER = jsonMapperBuilder()
.disable(DeserializationFeature.READ_ENUMS_USING_TO_STRING)
.build();

@Test
public void testWithoutCustomFeatures() throws Exception
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

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

import static tools.jackson.databind.testutil.DatabindTestUtil.newJsonMapper;
import static tools.jackson.databind.testutil.DatabindTestUtil.jsonMapperBuilder;

public class EnumDeserFromIntJsonValueTest
{
Expand Down Expand Up @@ -44,7 +44,8 @@ enum Bean1850LongField {
Bean1850LongField(long x) { this.x = x; }
}

private final ObjectMapper MAPPER = newJsonMapper();
private final ObjectMapper MAPPER = jsonMapperBuilder()
.disable(DeserializationFeature.READ_ENUMS_USING_TO_STRING).build();

// [databind#1850] pass tests

Expand All @@ -60,6 +61,7 @@ public void testEnumFromInt1850Method() throws Exception
public void testEnumFromInt1850Field() throws Exception
{
String json = MAPPER.writeValueAsString(Bean1850IntField.A);

Bean1850IntField e2 = MAPPER.readValue(json, Bean1850IntField.class);
assertEquals(Bean1850IntField.A, e2);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonProperty;

import tools.jackson.databind.DeserializationFeature;
import tools.jackson.databind.MapperFeature;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.exc.InvalidFormatException;
Expand Down Expand Up @@ -197,6 +198,7 @@ private ObjectMapper mapperWithMixIn(Class<?> target, Class<?> mixin) {
}

private JsonMapper.Builder builderWithMixIn(Class<?> target, Class<?> mixin) {
return JsonMapper.builder().addMixIn(target, mixin);
return JsonMapper.builder().disable(DeserializationFeature.READ_ENUMS_USING_TO_STRING)
.addMixIn(target, mixin);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,7 @@ public void testEnumWithJsonPropertyRenameMixin() throws Exception
public void testDeserWithToString1161() throws Exception
{
Enum1161 result = MAPPER.readerFor(Enum1161.class)
.without(DeserializationFeature.READ_ENUMS_USING_TO_STRING)
.readValue(q("A"));
assertSame(Enum1161.A, result);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ public Validity validateBaseType(DatabindContext ctxt, JavaType baseType) {
/**********************************************************
*/

private final ObjectMapper MAPPER = newJsonMapper();
private final ObjectMapper MAPPER = jsonMapperBuilder()
.disable(DeserializationFeature.READ_ENUMS_USING_TO_STRING).build();

/**
* Unit test that verifies that a bean is stored with type information,
Expand Down Expand Up @@ -248,6 +249,7 @@ public void testEnumAsObject() throws Exception

// and then with it
ObjectMapper m = jsonMapperBuilder()
.disable(DeserializationFeature.READ_ENUMS_USING_TO_STRING)
.activateDefaultTyping(NoCheckSubTypeValidator.instance)
.build();
String json = m.writeValueAsString(input);
Expand Down

0 comments on commit 1dface0

Please sign in to comment.