56
56
import io .swagger .v3 .oas .models .media .IntegerSchema ;
57
57
import io .swagger .v3 .oas .models .media .JsonSchema ;
58
58
import io .swagger .v3 .oas .models .media .MapSchema ;
59
- import io .swagger .v3 .oas .models .media .NumberSchema ;
60
59
import io .swagger .v3 .oas .models .media .ObjectSchema ;
61
60
import io .swagger .v3 .oas .models .media .Schema ;
62
61
import io .swagger .v3 .oas .models .media .StringSchema ;
@@ -306,8 +305,9 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
306
305
}
307
306
308
307
if (model == null && type .isEnumType ()) {
309
- model = new StringSchema ();
310
- _addEnumProps (type .getRawClass (), model );
308
+ @ SuppressWarnings ("unchecked" )
309
+ Class <Enum <?>> rawEnumClass = (Class <Enum <?>>) type .getRawClass ();
310
+ model = _createSchemaForEnum (rawEnumClass );
311
311
isPrimitive = true ;
312
312
}
313
313
if (model == null ) {
@@ -1164,46 +1164,57 @@ protected boolean _isOptionalType(JavaType propType) {
1164
1164
/**
1165
1165
* Adds each enum property value to the model schema
1166
1166
*
1167
- * @param propClass the enum class for which to add properties
1168
- * @param property the schema to add properties to
1167
+ * @param enumClass the enum class for which to add properties
1169
1168
*/
1170
- protected void _addEnumProps (Class <?> propClass , Schema property ) {
1171
- final boolean useIndex = _mapper .isEnabled (SerializationFeature .WRITE_ENUMS_USING_INDEX );
1172
- final boolean useToString = _mapper .isEnabled (SerializationFeature .WRITE_ENUMS_USING_TO_STRING );
1169
+ protected Schema _createSchemaForEnum (Class <Enum <?>> enumClass ) {
1170
+ boolean useIndex = _mapper .isEnabled (SerializationFeature .WRITE_ENUMS_USING_INDEX );
1171
+ boolean useToString = _mapper .isEnabled (SerializationFeature .WRITE_ENUMS_USING_TO_STRING );
1173
1172
1174
- Optional <Method > jsonValueMethod = Arrays .stream (propClass .getDeclaredMethods ())
1173
+ Optional <Method > jsonValueMethod = Arrays .stream (enumClass .getDeclaredMethods ())
1175
1174
.filter (m -> m .isAnnotationPresent (JsonValue .class ))
1176
1175
.filter (m -> m .getAnnotation (JsonValue .class ).value ())
1177
1176
.findFirst ();
1178
1177
1179
- Optional <Field > jsonValueField = Arrays .stream (propClass .getDeclaredFields ())
1178
+ Optional <Field > jsonValueField = Arrays .stream (enumClass .getDeclaredFields ())
1180
1179
.filter (f -> f .isAnnotationPresent (JsonValue .class ))
1181
1180
.filter (f -> f .getAnnotation (JsonValue .class ).value ())
1182
1181
.findFirst ();
1183
1182
1184
- jsonValueMethod .ifPresent (m -> m .setAccessible (true ));
1185
- jsonValueField .ifPresent (m -> m .setAccessible (true ));
1186
- @ SuppressWarnings ("unchecked" )
1187
- Class <Enum <?>> enumClass = (Class <Enum <?>>) propClass ;
1183
+ Schema schema = null ;
1184
+ if (jsonValueField .isPresent ()) {
1185
+ jsonValueField .get ().setAccessible (true );
1186
+ PrimitiveType primitiveType = PrimitiveType .fromType (jsonValueField .get ().getType ());
1187
+ if (primitiveType != null ) {
1188
+ schema = primitiveType .createProperty ();
1189
+ }
1190
+ } else if (jsonValueMethod .isPresent ()) {
1191
+ jsonValueMethod .get ().setAccessible (true );
1192
+ PrimitiveType primitiveType = PrimitiveType .fromType (jsonValueMethod .get ().getReturnType ());
1193
+ if (primitiveType != null ) {
1194
+ schema = primitiveType .createProperty ();
1195
+ }
1196
+ }
1197
+ if (schema == null ) {
1198
+ schema = new StringSchema ();
1199
+ }
1188
1200
1189
1201
Enum <?>[] enumConstants = enumClass .getEnumConstants ();
1190
1202
1191
1203
if (enumConstants != null ) {
1192
- String [] enumValues = _intr .findEnumValues (propClass , enumConstants ,
1204
+ String [] enumValues = _intr .findEnumValues (enumClass , enumConstants ,
1193
1205
new String [enumConstants .length ]);
1194
1206
1195
1207
for (Enum <?> en : enumConstants ) {
1196
- String n ;
1197
-
1198
1208
Field enumField = ReflectionUtils .findField (en .name (), enumClass );
1199
1209
if (null != enumField && enumField .isAnnotationPresent (Hidden .class )) {
1200
1210
continue ;
1201
1211
}
1202
1212
1203
1213
String enumValue = enumValues [en .ordinal ()];
1204
- String methodValue = jsonValueMethod .flatMap (m -> ReflectionUtils .safeInvoke (m , en )). map ( Object :: toString ).orElse (null );
1205
- String fieldValue = jsonValueField .flatMap (f -> ReflectionUtils .safeGet (f , en )). map ( Object :: toString ).orElse (null );
1214
+ Object methodValue = jsonValueMethod .flatMap (m -> ReflectionUtils .safeInvoke (m , en )).orElse (null );
1215
+ Object fieldValue = jsonValueField .flatMap (f -> ReflectionUtils .safeGet (f , en )).orElse (null );
1206
1216
1217
+ Object n ;
1207
1218
if (methodValue != null ) {
1208
1219
n = methodValue ;
1209
1220
} else if (fieldValue != null ) {
@@ -1217,12 +1228,10 @@ protected void _addEnumProps(Class<?> propClass, Schema property) {
1217
1228
} else {
1218
1229
n = _intr .findEnumValue (en );
1219
1230
}
1220
- if (property instanceof StringSchema ) {
1221
- StringSchema sp = (StringSchema ) property ;
1222
- sp .addEnumItem (n );
1223
- }
1231
+ schema .addEnumItemObject (n );
1224
1232
}
1225
1233
}
1234
+ return schema ;
1226
1235
}
1227
1236
1228
1237
protected boolean ignore (final Annotated member , final XmlAccessorType xmlAccessorTypeAnnotation , final String propName , final Set <String > propertiesToIgnore ) {
0 commit comments