Skip to content

Commit

Permalink
Use @JsonProperty for Enum values also when `READ_ENUMS_USING_TO_ST…
Browse files Browse the repository at this point in the history
…RING` enabled (#4036)
  • Loading branch information
iProdigy authored Jul 15, 2023
1 parent ebe7165 commit 059ccee
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,10 @@ public static EnumResolver constructUsingToString(DeserializationConfig config,
final Enum<?>[] enumConstants = _enumConstants(enumCls0);

// introspect
final String[] names = new String[enumConstants.length];
final String[][] allAliases = new String[enumConstants.length][];
if (ai != null) {
ai.findEnumValues(config, annotatedClass, enumConstants, names);
ai.findEnumAliases(config, annotatedClass, enumConstants, allAliases);
}

Expand All @@ -186,7 +188,11 @@ public static EnumResolver constructUsingToString(DeserializationConfig config,
HashMap<String, Enum<?>> map = new HashMap<String, Enum<?>>();
for (int i = enumConstants.length; --i >= 0; ) {
Enum<?> enumValue = enumConstants[i];
map.put(enumValue.toString(), enumValue);
String name = names[i];
if (name == null) {
name = enumValue.toString();
}
map.put(name, enumValue);
String[] aliases = allAliases[i];
if (aliases != null) {
for (String alias : aliases) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ static enum EnumWithPropertyAnno {
public String toString() {
return "bb";
}
}
},

@JsonProperty("cc")
C
;
}

Expand Down Expand Up @@ -539,6 +542,29 @@ public void testEnumWithJsonPropertyRename() throws Exception
assertSame(EnumWithPropertyAnno.B, result[0]);
assertSame(EnumWithPropertyAnno.A, result[1]);
}

public void testEnumWithJsonPropertyRenameWithToString() throws Exception {
EnumWithPropertyAnno a = MAPPER.readerFor(EnumWithPropertyAnno.class)
.with(DeserializationFeature.READ_ENUMS_USING_TO_STRING)
.readValue(q("a"));
assertSame(EnumWithPropertyAnno.A, a);

EnumWithPropertyAnno b = MAPPER.readerFor(EnumWithPropertyAnno.class)
.with(DeserializationFeature.READ_ENUMS_USING_TO_STRING)
.readValue(q("b"));
assertSame(EnumWithPropertyAnno.B, b);

EnumWithPropertyAnno bb = MAPPER.readerFor(EnumWithPropertyAnno.class)
.with(DeserializationFeature.READ_ENUMS_USING_TO_STRING)
.with(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL)
.readValue(q("bb"));
assertNull(bb);

EnumWithPropertyAnno c = MAPPER.readerFor(EnumWithPropertyAnno.class)
.with(DeserializationFeature.READ_ENUMS_USING_TO_STRING)
.readValue(q("cc"));
assertSame(EnumWithPropertyAnno.C, c);
}

/**
* {@link #testEnumWithJsonPropertyRename()}
Expand Down

0 comments on commit 059ccee

Please sign in to comment.