Skip to content

Commit 1dface0

Browse files
authored
Enable DeserializationFeature.READ_ENUMS_USING_TO_STRING by default (3.0) (#4566)
1 parent a419046 commit 1dface0

File tree

8 files changed

+23
-12
lines changed

8 files changed

+23
-12
lines changed

release-notes/VERSION

+2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ Versions: 3.x (for earlier see VERSION-2.x)
5858
#4160: Deprecate `DefaultTyping.EVERYTHING` in `2.x` and remove in `3.0`
5959
#4381: Prevent construction of `null`-valued `JsonNode`s (like `TextNode`)
6060
#4552: Change `MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS` default to `false` for 3.0
61+
#4566: Enable `DeserializationFeature.READ_ENUMS_USING_TO_STRING` by default (3.0)
62+
(contributed by Joo-Hyuk K)
6163
- Remove `MappingJsonFactory`
6264
- Add context parameter for `TypeSerializer` contextualization (`forProperty()`)
6365
- Default for `JsonNodeFeature.STRIP_TRAILING_BIGDECIMAL_ZEROES` changed to `false` for 3.0

src/main/java/tools/jackson/databind/DeserializationFeature.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -388,17 +388,17 @@ public enum DeserializationFeature implements ConfigFeature
388388
ACCEPT_FLOAT_AS_INT(true),
389389

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

403403
/**
404404
* Feature that allows unknown Enum values to be parsed as {@code null} values.

src/test/java/tools/jackson/databind/deser/creators/EnumCreatorTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ public void testNoArgEnumCreator() throws Exception
343343
@Test
344344
public void testEnumCreators1291() throws Exception
345345
{
346-
ObjectMapper mapper = new ObjectMapper();
346+
ObjectMapper mapper = jsonMapperBuilder().disable(DeserializationFeature.READ_ENUMS_USING_TO_STRING).build();
347347
String json = mapper.writeValueAsString(Enum1291.V2);
348348
Enum1291 result = mapper.readValue(json, Enum1291.class);
349349
assertSame(Enum1291.V2, result);

src/test/java/tools/jackson/databind/deser/enums/EnumDefaultReadTest.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ enum MixinOverloadedDefault {
9191
/**********************************************************
9292
*/
9393

94-
private final ObjectMapper MAPPER = newJsonMapper();
94+
private final ObjectMapper MAPPER = jsonMapperBuilder()
95+
.disable(DeserializationFeature.READ_ENUMS_USING_TO_STRING)
96+
.build();
9597

9698
@Test
9799
public void testWithoutCustomFeatures() throws Exception

src/test/java/tools/jackson/databind/deser/enums/EnumDeserFromIntJsonValueTest.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

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

11-
import static tools.jackson.databind.testutil.DatabindTestUtil.newJsonMapper;
11+
import static tools.jackson.databind.testutil.DatabindTestUtil.jsonMapperBuilder;
1212

1313
public class EnumDeserFromIntJsonValueTest
1414
{
@@ -44,7 +44,8 @@ enum Bean1850LongField {
4444
Bean1850LongField(long x) { this.x = x; }
4545
}
4646

47-
private final ObjectMapper MAPPER = newJsonMapper();
47+
private final ObjectMapper MAPPER = jsonMapperBuilder()
48+
.disable(DeserializationFeature.READ_ENUMS_USING_TO_STRING).build();
4849

4950
// [databind#1850] pass tests
5051

@@ -60,6 +61,7 @@ public void testEnumFromInt1850Method() throws Exception
6061
public void testEnumFromInt1850Field() throws Exception
6162
{
6263
String json = MAPPER.writeValueAsString(Bean1850IntField.A);
64+
6365
Bean1850IntField e2 = MAPPER.readValue(json, Bean1850IntField.class);
6466
assertEquals(Bean1850IntField.A, e2);
6567
}

src/test/java/tools/jackson/databind/deser/enums/EnumDeserMixin2787Test.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.fasterxml.jackson.annotation.JsonAlias;
66
import com.fasterxml.jackson.annotation.JsonProperty;
77

8+
import tools.jackson.databind.DeserializationFeature;
89
import tools.jackson.databind.MapperFeature;
910
import tools.jackson.databind.ObjectMapper;
1011
import tools.jackson.databind.exc.InvalidFormatException;
@@ -197,6 +198,7 @@ private ObjectMapper mapperWithMixIn(Class<?> target, Class<?> mixin) {
197198
}
198199

199200
private JsonMapper.Builder builderWithMixIn(Class<?> target, Class<?> mixin) {
200-
return JsonMapper.builder().addMixIn(target, mixin);
201+
return JsonMapper.builder().disable(DeserializationFeature.READ_ENUMS_USING_TO_STRING)
202+
.addMixIn(target, mixin);
201203
}
202204
}

src/test/java/tools/jackson/databind/deser/enums/EnumDeserializationTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,7 @@ public void testEnumWithJsonPropertyRenameMixin() throws Exception
615615
public void testDeserWithToString1161() throws Exception
616616
{
617617
Enum1161 result = MAPPER.readerFor(Enum1161.class)
618+
.without(DeserializationFeature.READ_ENUMS_USING_TO_STRING)
618619
.readValue(q("A"));
619620
assertSame(Enum1161.A, result);
620621

src/test/java/tools/jackson/databind/jsontype/deftyping/TestDefaultForObject.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ public Validity validateBaseType(DatabindContext ctxt, JavaType baseType) {
106106
/**********************************************************
107107
*/
108108

109-
private final ObjectMapper MAPPER = newJsonMapper();
109+
private final ObjectMapper MAPPER = jsonMapperBuilder()
110+
.disable(DeserializationFeature.READ_ENUMS_USING_TO_STRING).build();
110111

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

249250
// and then with it
250251
ObjectMapper m = jsonMapperBuilder()
252+
.disable(DeserializationFeature.READ_ENUMS_USING_TO_STRING)
251253
.activateDefaultTyping(NoCheckSubTypeValidator.instance)
252254
.build();
253255
String json = m.writeValueAsString(input);

0 commit comments

Comments
 (0)