Skip to content

Commit 059ccee

Browse files
authored
Use @JsonProperty for Enum values also when READ_ENUMS_USING_TO_STRING enabled (#4036)
1 parent ebe7165 commit 059ccee

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/main/java/com/fasterxml/jackson/databind/util/EnumResolver.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,10 @@ public static EnumResolver constructUsingToString(DeserializationConfig config,
176176
final Enum<?>[] enumConstants = _enumConstants(enumCls0);
177177

178178
// introspect
179+
final String[] names = new String[enumConstants.length];
179180
final String[][] allAliases = new String[enumConstants.length][];
180181
if (ai != null) {
182+
ai.findEnumValues(config, annotatedClass, enumConstants, names);
181183
ai.findEnumAliases(config, annotatedClass, enumConstants, allAliases);
182184
}
183185

@@ -186,7 +188,11 @@ public static EnumResolver constructUsingToString(DeserializationConfig config,
186188
HashMap<String, Enum<?>> map = new HashMap<String, Enum<?>>();
187189
for (int i = enumConstants.length; --i >= 0; ) {
188190
Enum<?> enumValue = enumConstants[i];
189-
map.put(enumValue.toString(), enumValue);
191+
String name = names[i];
192+
if (name == null) {
193+
name = enumValue.toString();
194+
}
195+
map.put(name, enumValue);
190196
String[] aliases = allAliases[i];
191197
if (aliases != null) {
192198
for (String alias : aliases) {

src/test/java/com/fasterxml/jackson/databind/deser/enums/EnumDeserializationTest.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ static enum EnumWithPropertyAnno {
8989
public String toString() {
9090
return "bb";
9191
}
92-
}
92+
},
93+
94+
@JsonProperty("cc")
95+
C
9396
;
9497
}
9598

@@ -539,6 +542,29 @@ public void testEnumWithJsonPropertyRename() throws Exception
539542
assertSame(EnumWithPropertyAnno.B, result[0]);
540543
assertSame(EnumWithPropertyAnno.A, result[1]);
541544
}
545+
546+
public void testEnumWithJsonPropertyRenameWithToString() throws Exception {
547+
EnumWithPropertyAnno a = MAPPER.readerFor(EnumWithPropertyAnno.class)
548+
.with(DeserializationFeature.READ_ENUMS_USING_TO_STRING)
549+
.readValue(q("a"));
550+
assertSame(EnumWithPropertyAnno.A, a);
551+
552+
EnumWithPropertyAnno b = MAPPER.readerFor(EnumWithPropertyAnno.class)
553+
.with(DeserializationFeature.READ_ENUMS_USING_TO_STRING)
554+
.readValue(q("b"));
555+
assertSame(EnumWithPropertyAnno.B, b);
556+
557+
EnumWithPropertyAnno bb = MAPPER.readerFor(EnumWithPropertyAnno.class)
558+
.with(DeserializationFeature.READ_ENUMS_USING_TO_STRING)
559+
.with(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL)
560+
.readValue(q("bb"));
561+
assertNull(bb);
562+
563+
EnumWithPropertyAnno c = MAPPER.readerFor(EnumWithPropertyAnno.class)
564+
.with(DeserializationFeature.READ_ENUMS_USING_TO_STRING)
565+
.readValue(q("cc"));
566+
assertSame(EnumWithPropertyAnno.C, c);
567+
}
542568

543569
/**
544570
* {@link #testEnumWithJsonPropertyRename()}

0 commit comments

Comments
 (0)