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

Use @JsonProperty for Enum values also when READ_ENUMS_USING_TO_STRING enabled #4036

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
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 Expand Up @@ -216,15 +222,21 @@ public static EnumResolver constructUsingToString(DeserializationConfig config,
final Class<Enum<?>> enumCls = _enumClass(enumCls0);
final Enum<?>[] enumConstants = _enumConstants(enumCls0);
HashMap<String, Enum<?>> map = new HashMap<String, Enum<?>>();
final String[] names = new String[enumConstants.length];
final String[][] allAliases = new String[enumConstants.length][];
if (ai != null) {
ai.findEnumValues(enumCls, enumConstants, names);
ai.findEnumAliases(enumCls, enumConstants, allAliases);
}

// from last to first, so that in case of duplicate values, first wins
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioend in the comment, we may not need to change behavior in this deprecated method

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 @@ -539,6 +539,13 @@ public void testEnumWithJsonPropertyRename() throws Exception
assertSame(EnumWithPropertyAnno.B, result[0]);
assertSame(EnumWithPropertyAnno.A, result[1]);
}

public void testEnumWithJsonPropertyRenameWithToString() throws Exception {
EnumWithPropertyAnno result = MAPPER.readerFor(EnumWithPropertyAnno.class)
.with(DeserializationFeature.READ_ENUMS_USING_TO_STRING)
.readValue(q("a"));
assertSame(EnumWithPropertyAnno.A, result);
}
iProdigy marked this conversation as resolved.
Show resolved Hide resolved

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