77
77
import javax .xml .bind .annotation .XmlSchema ;
78
78
import java .io .IOException ;
79
79
import java .lang .annotation .Annotation ;
80
+ import java .lang .reflect .Field ;
80
81
import java .lang .reflect .InvocationTargetException ;
81
82
import java .lang .reflect .Method ;
82
83
import java .lang .reflect .Type ;
@@ -955,20 +956,33 @@ protected boolean _isOptionalType(JavaType propType) {
955
956
.contains (propType .getRawClass ().getCanonicalName ());
956
957
}
957
958
959
+ /**
960
+ * Adds each enum property value to the model schema
961
+ *
962
+ * @param propClass the enum class for which to add properties
963
+ * @param property the schema to add properties to
964
+ */
958
965
protected void _addEnumProps (Class <?> propClass , Schema property ) {
959
966
final boolean useIndex = _mapper .isEnabled (SerializationFeature .WRITE_ENUMS_USING_INDEX );
960
967
final boolean useToString = _mapper .isEnabled (SerializationFeature .WRITE_ENUMS_USING_TO_STRING );
961
968
962
-
963
- Optional <Method > jsonValueMethod = Arrays .stream (propClass .getMethods ())
969
+ Optional <Method > jsonValueMethod = Arrays .stream (propClass .getDeclaredMethods ())
964
970
.filter (m -> m .isAnnotationPresent (JsonValue .class ))
965
971
.filter (m -> m .getAnnotation (JsonValue .class ).value ())
966
972
.findFirst ();
967
973
974
+ Optional <Field > jsonValueField = Arrays .stream (propClass .getDeclaredFields ())
975
+ .filter (f -> f .isAnnotationPresent (JsonValue .class ))
976
+ .filter (f -> f .getAnnotation (JsonValue .class ).value ())
977
+ .findFirst ();
978
+
979
+ jsonValueMethod .ifPresent (m -> m .setAccessible (true ));
980
+ jsonValueField .ifPresent (m -> m .setAccessible (true ));
968
981
@ SuppressWarnings ("unchecked" )
969
982
Class <Enum <?>> enumClass = (Class <Enum <?>>) propClass ;
970
983
971
984
Enum <?>[] enumConstants = enumClass .getEnumConstants ();
985
+
972
986
if (enumConstants != null ) {
973
987
String [] enumValues = _intr .findEnumValues (propClass , enumConstants ,
974
988
new String [enumConstants .length ]);
@@ -977,11 +991,13 @@ protected void _addEnumProps(Class<?> propClass, Schema property) {
977
991
String n ;
978
992
979
993
String enumValue = enumValues [en .ordinal ()];
980
- String s = jsonValueMethod .flatMap (m -> ReflectionUtils .safeInvoke (m , en ))
981
- .map (Object ::toString ).orElse (null );
994
+ String methodValue = jsonValueMethod .flatMap (m -> ReflectionUtils .safeInvoke (m , en )). map ( Object :: toString ). orElse ( null );
995
+ String fieldValue = jsonValueField . flatMap ( f -> ReflectionUtils . safeGet ( f , en )) .map (Object ::toString ).orElse (null );
982
996
983
- if (s != null ) {
984
- n = s ;
997
+ if (methodValue != null ) {
998
+ n = methodValue ;
999
+ } else if (fieldValue != null ) {
1000
+ n = fieldValue ;
985
1001
} else if (enumValue != null ) {
986
1002
n = enumValue ;
987
1003
} else if (useIndex ) {
0 commit comments