-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Handle Enum
introspection of values and aliases via AnnotatedClass
instead of Class<?>
#3832
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
Changes from 52 commits
0ff49d2
d9b70a9
b11db13
fbcc1c3
3ebe045
4cdef6c
bcd9025
ed21cf3
e33619e
39ff50d
51f923b
1f7bbc1
9478871
2a80825
b9a0677
05d77bb
ac04e46
caac7f5
b8447f7
89d80de
4440da0
e132e9d
244a871
baa9c8c
5143724
ad73809
a20d61d
d3a57ee
872dd10
8ae8ff4
e89a778
abbee55
d4d56e2
c07dd2f
47788f0
99cf1f7
0669e3c
1bd982a
833f6c4
dca4506
e3f7b1b
ac2af2b
aa589e5
50570b9
f91657c
c78a0e4
5d98b69
5a85d0b
3b89923
db48e04
6bbf646
f68e42b
24f3f45
1f5baf6
426fef5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -240,6 +240,45 @@ public String[] findEnumValues(Class<?> enumType, Enum<?>[] enumValues, String[] | |
return names; | ||
} | ||
|
||
@Override | ||
public String[] findEnumValues(MapperConfig<?> config, Enum<?>[] enumValues, String[] names, | ||
AnnotatedClass annotatedClass) { | ||
// First collect all JsonProperty.value() | ||
HashMap<String, String> enumToPropertyMap = new HashMap<>(); | ||
|
||
for (AnnotatedField field : annotatedClass.fields()) { | ||
JsonProperty property = field.getAnnotation(JsonProperty.class); | ||
JooHyukKim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (property != null) { | ||
enumToPropertyMap.put(field.getName(), property.value()); | ||
|
||
} | ||
} | ||
final Class<?> enumType = annotatedClass.getRawType(); | ||
HashMap<String,String> expl = null; | ||
for (Field f : enumType.getDeclaredFields()) { | ||
|
||
if (!f.isEnumConstant()) { | ||
continue; | ||
} | ||
String n = enumToPropertyMap.get(f.getName()); | ||
if (n == null || n.isEmpty()) { | ||
|
||
continue; | ||
} | ||
if (expl == null) { | ||
expl = new HashMap<String,String>(); | ||
} | ||
expl.put(f.getName(), n); | ||
} | ||
// and then stitch them together if and as necessary | ||
if (expl != null) { | ||
for (int i = 0, end = enumValues.length; i < end; ++i) { | ||
String defName = enumValues[i].name(); | ||
String explValue = expl.get(defName); | ||
if (explValue != null) { | ||
names[i] = explValue; | ||
} | ||
} | ||
} | ||
return names; | ||
} | ||
JooHyukKim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@Override // since 2.11 | ||
public void findEnumAliases(Class<?> enumType, Enum<?>[] enumValues, String[][] aliasList) | ||
{ | ||
|
@@ -264,6 +303,23 @@ public void findEnumAliases(Class<?> enumType, Enum<?>[] enumValues, String[][] | |
} | ||
} | ||
|
||
@Override | ||
public void findEnumAliases(MapperConfig<?> config, Enum<?>[] enumValues, String[][] aliasList, AnnotatedClass annotatedClass) | ||
{ | ||
HashMap<String, String[]> enumToAliasMap = new HashMap<>(); | ||
for (AnnotatedField field : annotatedClass.fields()) { | ||
JsonAlias alias = field.getAnnotation(JsonAlias.class); | ||
if (alias != null) { | ||
enumToAliasMap.putIfAbsent(field.getName(), alias.value()); | ||
} | ||
} | ||
|
||
for (int i = 0, end = enumValues.length; i < end; ++i) { | ||
Enum<?> enumValue = enumValues[i]; | ||
aliasList[i] = enumToAliasMap.getOrDefault(enumValue.name(), new String[]{}); | ||
} | ||
} | ||
|
||
JooHyukKim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/** | ||
* Finds the Enum value that should be considered the default value, if possible. | ||
* <p> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1209,6 +1209,10 @@ protected JsonSerializer<?> buildEnumSerializer(SerializationConfig config, | |
if (format.getShape() == JsonFormat.Shape.OBJECT) { | ||
// one special case: suppress serialization of "getDeclaringClass()"... | ||
((BasicBeanDescription) beanDesc).removeProperty("declaringClass"); | ||
// [databind#2787]: remove self-referencing enum fields introduced by annotation flattening of mixins | ||
if (type.isEnumType()){ | ||
_removeEnumSelfReferences(((BasicBeanDescription) beanDesc)); | ||
} | ||
// returning null will mean that eventually BeanSerializer gets constructed | ||
return null; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is where |
||
|
@@ -1224,6 +1228,30 @@ protected JsonSerializer<?> buildEnumSerializer(SerializationConfig config, | |
return ser; | ||
} | ||
|
||
/** | ||
* Helper method used in the serialization of an {@link Enum} to remove any self-referencing properties from | ||
* the bean description before it is transformed into a JSON Object as configured by {@link JsonFormat.Shape#OBJECT}. | ||
* <p> | ||
* Basically, this method iterates through the properties of the bean description and removes any that are | ||
* an enum type and a subtype of the enum class to be serialized. | ||
* | ||
* @param beanDesc the bean description to remove Enum properties from. | ||
* | ||
* @since 2.16 : [databind#2787]: remove self-referencing enum fields introduced by annotation flattening of mixins | ||
*/ | ||
private void _removeEnumSelfReferences(BasicBeanDescription beanDesc) { | ||
Class<?> aClass = ClassUtil.findEnumType(beanDesc.getBeanClass()); | ||
Iterator<BeanPropertyDefinition> it = beanDesc.findProperties().iterator(); | ||
while (it.hasNext()) { | ||
BeanPropertyDefinition property = it.next(); | ||
// Remove enums, because they are self-referenced | ||
JavaType propType = property.getPrimaryType(); | ||
if (propType.isEnumType() && propType.isTypeOrSubTypeOf(aClass)) { | ||
it.remove(); | ||
} | ||
} | ||
} | ||
|
||
/* | ||
/********************************************************** | ||
/* Other helper methods | ||
|
Uh oh!
There was an error while loading. Please reload this page.