Skip to content

Commit 0d0a3d0

Browse files
committed
fix: Remove duplicate oneOf schemas during pre-processing
1 parent 65c3126 commit 0d0a3d0

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,14 @@ public void postProcessParameter(CodegenParameter parameter) {
10091009
@Override
10101010
@SuppressWarnings("unused")
10111011
public void preprocessOpenAPI(OpenAPI openAPI) {
1012+
if (openAPI.getComponents() != null) {
1013+
// Remove duplicate oneOf
1014+
Map<String, Schema> schemas = new HashMap<>(openAPI.getComponents().getSchemas());
1015+
for (var schema : schemas.values()) {
1016+
ModelUtils.deduplicateOneOfSchema(schema);
1017+
}
1018+
}
1019+
10121020
if (useOneOfInterfaces && openAPI.getComponents() != null) {
10131021
// we process the openapi schema here to find oneOf schemas and create interface models for them
10141022
Map<String, Schema> schemas = new HashMap<>(openAPI.getComponents().getSchemas());

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2243,6 +2243,19 @@ public static Schema simplyOneOfAnyOfWithOnlyOneNonNullSubSchema(OpenAPI openAPI
22432243
return schema;
22442244
}
22452245

2246+
/**
2247+
* Removes duplicate `oneOf` from a given schema
2248+
*
2249+
* @param schema Schema
2250+
*/
2251+
public static void deduplicateOneOfSchema(Schema<?> schema) {
2252+
if (schema.getOneOf() == null) {
2253+
return;
2254+
}
2255+
Set<Schema> deduplicated = new LinkedHashSet<>(schema.getOneOf());
2256+
schema.setOneOf(new ArrayList<>(deduplicated));
2257+
}
2258+
22462259
/**
22472260
* Check if the schema is of type 'null' or schema itself is pointing to null
22482261
* <p>

0 commit comments

Comments
 (0)