Skip to content

Commit f728c95

Browse files
committed
fix: enum schemas - compatibility and 3.1 support
1 parent 817cbaf commit f728c95

File tree

1 file changed

+15
-68
lines changed

1 file changed

+15
-68
lines changed

modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java

Lines changed: 15 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,6 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
338338
@SuppressWarnings("unchecked")
339339
Class<Enum<?>> rawEnumClass = (Class<Enum<?>>) type.getRawClass();
340340
model = _createSchemaForEnum(rawEnumClass);
341-
model = openapi31 ? new JsonSchema().typesItem("string") : new StringSchema();
342-
_addEnumProps(type.getRawClass(), model);
343341
isPrimitive = true;
344342
}
345343
if (model == null) {
@@ -1295,69 +1293,18 @@ protected boolean _isOptionalType(JavaType propType) {
12951293
}
12961294

12971295
/**
1298-
* Adds each enum property value to the model schema
1299-
*
1300-
* @param propClass the enum class for which to add properties
1301-
* @param property the schema to add properties to
1296+
* @deprecated use '_createSchemaForEnum'
13021297
*/
13031298
protected void _addEnumProps(Class<?> propClass, Schema property) {
1304-
final boolean useIndex = _mapper.isEnabled(SerializationFeature.WRITE_ENUMS_USING_INDEX);
1305-
final boolean useToString = _mapper.isEnabled(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
1306-
1307-
Optional<Method> jsonValueMethod = Arrays.stream(propClass.getDeclaredMethods())
1308-
.filter(m -> m.isAnnotationPresent(JsonValue.class))
1309-
.filter(m -> m.getAnnotation(JsonValue.class).value())
1310-
.findFirst();
1311-
1312-
Optional<Field> jsonValueField = Arrays.stream(propClass.getDeclaredFields())
1313-
.filter(f -> f.isAnnotationPresent(JsonValue.class))
1314-
.filter(f -> f.getAnnotation(JsonValue.class).value())
1315-
.findFirst();
1316-
1317-
jsonValueMethod.ifPresent(m -> m.setAccessible(true));
1318-
jsonValueField.ifPresent(m -> m.setAccessible(true));
1319-
@SuppressWarnings("unchecked")
1320-
Class<Enum<?>> enumClass = (Class<Enum<?>>) propClass;
1321-
1322-
Enum<?>[] enumConstants = enumClass.getEnumConstants();
1323-
1324-
if (enumConstants != null) {
1325-
String[] enumValues = _intr().findEnumValues(propClass, enumConstants,
1326-
new String[enumConstants.length]);
1327-
1328-
for (Enum<?> en : enumConstants) {
1329-
String n;
1330-
1331-
Field enumField = ReflectionUtils.findField(en.name(), enumClass);
1332-
if (null != enumField && enumField.isAnnotationPresent(Hidden.class)) {
1333-
continue;
1334-
}
1335-
1336-
String enumValue = enumValues[en.ordinal()];
1337-
String methodValue = jsonValueMethod.flatMap(m -> ReflectionUtils.safeInvoke(m, en)).map(Object::toString).orElse(null);
1338-
String fieldValue = jsonValueField.flatMap(f -> ReflectionUtils.safeGet(f, en)).map(Object::toString).orElse(null);
1339-
1340-
if (methodValue != null) {
1341-
n = methodValue;
1342-
} else if (fieldValue != null) {
1343-
n = fieldValue;
1344-
} else if (enumValue != null) {
1345-
n = enumValue;
1346-
} else if (useIndex) {
1347-
n = String.valueOf(en.ordinal());
1348-
} else if (useToString) {
1349-
n = en.toString();
1350-
} else {
1351-
n = _intr().findEnumValue(en);
1352-
}
1353-
if (isStringSchema(property)) {
1354-
if (openapi31) {
1355-
property.addEnumItemObject(n);
1356-
} else {
1357-
StringSchema sp = (StringSchema) property;
1358-
sp.addEnumItem(n);
1359-
}
1360-
}
1299+
if (propClass.isEnum()) {
1300+
Class<Enum<?>> rawEnumClass = (Class<Enum<?>>) propClass;
1301+
Schema enumSchema = _createSchemaForEnum(rawEnumClass);
1302+
if (enumSchema != null) {
1303+
property.setEnum(enumSchema.getEnum());
1304+
property.setType(enumSchema.getType());
1305+
property.setFormat(enumSchema.getFormat());
1306+
property.setName(enumSchema.getName());
1307+
property.setDescription(enumSchema.getDescription());
13611308
}
13621309
}
13631310
}
@@ -1386,23 +1333,23 @@ protected Schema _createSchemaForEnum(Class<Enum<?>> enumClass) {
13861333
jsonValueField.get().setAccessible(true);
13871334
PrimitiveType primitiveType = PrimitiveType.fromType(jsonValueField.get().getType());
13881335
if (primitiveType != null) {
1389-
schema = primitiveType.createProperty();
1336+
schema = openapi31 ? primitiveType.createProperty31() : primitiveType.createProperty();
13901337
}
13911338
} else if (jsonValueMethod.isPresent()) {
13921339
jsonValueMethod.get().setAccessible(true);
13931340
PrimitiveType primitiveType = PrimitiveType.fromType(jsonValueMethod.get().getReturnType());
13941341
if (primitiveType != null) {
1395-
schema = primitiveType.createProperty();
1342+
schema = openapi31 ? primitiveType.createProperty31() : primitiveType.createProperty();
13961343
}
13971344
}
13981345
if (schema == null) {
1399-
schema = new StringSchema();
1346+
schema = openapi31 ? new JsonSchema().typesItem("string") : new StringSchema();
14001347
}
14011348

14021349
Enum<?>[] enumConstants = enumClass.getEnumConstants();
14031350

14041351
if (enumConstants != null) {
1405-
String[] enumValues = _intr.findEnumValues(enumClass, enumConstants,
1352+
String[] enumValues = _intr().findEnumValues(enumClass, enumConstants,
14061353
new String[enumConstants.length]);
14071354

14081355
for (Enum<?> en : enumConstants) {
@@ -1427,7 +1374,7 @@ protected Schema _createSchemaForEnum(Class<Enum<?>> enumClass) {
14271374
} else if (useToString) {
14281375
n = en.toString();
14291376
} else {
1430-
n = _intr.findEnumValue(en);
1377+
n = _intr().findEnumValue(en);
14311378
}
14321379
schema.addEnumItemObject(n);
14331380
}

0 commit comments

Comments
 (0)