Skip to content

Commit de90b6b

Browse files
committed
Implement retaining the enum description as x-enum-desriptions for oneof enum
1 parent aa58fde commit de90b6b

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,7 +1347,7 @@ protected Schema processSimplifyOneOfEnum(Schema schema) {
13471347
* @return Simplified schema
13481348
*/
13491349
protected Schema simplifyComposedSchemaWithEnums(Schema schema, List<Object> subSchemas, String composedType) {
1350-
List<Object> enumValues = new ArrayList<>();
1350+
Map<Object, String> enumValues = new LinkedHashMap<>();
13511351

13521352
if(schema.getTypes() != null && schema.getTypes().size() > 1) {
13531353
// we cannot handle enums with multiple types
@@ -1387,7 +1387,21 @@ protected Schema simplifyComposedSchemaWithEnums(Schema schema, List<Object> sub
13871387
}
13881388
}
13891389
// Add all enum values from this sub-schema to our collection
1390-
enumValues.addAll(subSchema.getEnum());
1390+
if(subSchema.getEnum().size() == 1) {
1391+
String description = subSchema.getTitle() == null ? "" : subSchema.getTitle();
1392+
if(subSchema.getDescription() != null) {
1393+
if(!description.isEmpty()) {
1394+
description += " - ";
1395+
}
1396+
description += subSchema.getDescription();
1397+
}
1398+
enumValues.put(subSchema.getEnum().get(0), description);
1399+
} else {
1400+
for(Object e: subSchema.getEnum()) {
1401+
enumValues.put(e, "");
1402+
}
1403+
}
1404+
13911405
}
13921406

13931407
return createSimplifiedEnumSchema(schema, enumValues, schemaType, composedType);
@@ -1403,7 +1417,7 @@ protected Schema simplifyComposedSchemaWithEnums(Schema schema, List<Object> sub
14031417
* @param composedType Type of composed schema being simplified
14041418
* @return Simplified enum schema
14051419
*/
1406-
protected Schema createSimplifiedEnumSchema(Schema originalSchema, List<Object> enumValues, String schemaType, String composedType) {
1420+
protected Schema createSimplifiedEnumSchema(Schema originalSchema, Map<Object, String> enumValues, String schemaType, String composedType) {
14071421
// Clear the composed schema type
14081422
if ("oneOf".equals(composedType)) {
14091423
originalSchema.setOneOf(null);
@@ -1416,9 +1430,11 @@ protected Schema createSimplifiedEnumSchema(Schema originalSchema, List<Object>
14161430
ModelUtils.setType(originalSchema, schemaType);
14171431
}
14181432

1419-
// Set the combined enum values (deduplicated to avoid duplicates)
1420-
List<Object> uniqueEnumValues = enumValues.stream().distinct().collect(Collectors.toList());
1421-
originalSchema.setEnum(uniqueEnumValues);
1433+
originalSchema.setEnum(new ArrayList<>(enumValues.keySet()));
1434+
if(enumValues.values().stream().anyMatch(e -> !e.isEmpty())) {
1435+
//set x-enum-descriptions only if there's at least one non-empty description
1436+
originalSchema.addExtension("x-enum-descriptions", new ArrayList<>(enumValues.values()));
1437+
}
14221438

14231439
LOGGER.debug("Simplified {} with enum sub-schemas to single enum: {}", composedType, originalSchema);
14241440

modules/openapi-generator/src/test/java/org/openapitools/codegen/OpenAPINormalizerTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ public void testSimplifyOneOfAnyOfEnum() throws Exception {
186186
assertNull(anyOfParam.getSchema().getAnyOf());
187187
assertEquals(anyOfParam.getSchema().getType(), "string");
188188
assertEquals(anyOfParam.getSchema().getEnum(), Arrays.asList("anyof 1", "anyof 2"));
189+
assertEquals(anyOfParam.getSchema().getExtensions().get("x-enum-descriptions"), Arrays.asList("title 1", "title 2"));
189190

190191
Schema combinedRefsEnum = openAPI.getComponents().getSchemas().get("combinedRefsEnum");
191192

modules/openapi-generator/src/test/resources/3_0/simplifyOneOfWithEnums_test.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ paths:
5151
schema:
5252
type: string
5353
anyOf:
54-
- enum: [ "anyof 1" ]
55-
- enum: [ "anyof 2" ]
54+
- title: title 1
55+
enum: [ "anyof 1" ]
56+
- title: title 2
57+
enum: [ "anyof 2" ]
5658
responses:
5759
'200':
5860
description: Success

0 commit comments

Comments
 (0)