Skip to content

Commit 3b3ae6e

Browse files
committed
Fix #22753: Use referenced schema names instead of operation-based names for external ref schemas
1 parent 342febd commit 3b3ae6e

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,23 @@ private void flattenContent(Content content, String name) {
496496
if (schema == null) {
497497
continue;
498498
}
499-
String schemaName = resolveModelName(schema.getTitle(), name); // name example: testPost_request
499+
// Check if schema has a $ref pointing to a component schema
500+
// If so, prefer the referenced schema name over operation-based naming
501+
String schemaName;
502+
if (schema.get$ref() != null) {
503+
String refName = ModelUtils.getSimpleRef(schema.get$ref());
504+
if (refName != null && openAPI.getComponents() != null &&
505+
openAPI.getComponents().getSchemas() != null &&
506+
openAPI.getComponents().getSchemas().containsKey(refName)) {
507+
// Use the referenced schema name instead of operation-based name
508+
schemaName = resolveModelName(schema.getTitle(), refName);
509+
} else {
510+
// Fallback to original behavior if ref doesn't point to a component schema
511+
schemaName = resolveModelName(schema.getTitle(), name);
512+
}
513+
} else {
514+
schemaName = resolveModelName(schema.getTitle(), name); // name example: testPost_request
515+
}
500516
// Recursively gather/make inline models within this schema if any
501517
gatherInlineModels(schema, schemaName);
502518
if (isModelNeeded(schema)) {

0 commit comments

Comments
 (0)