Skip to content

Commit 860a89a

Browse files
committed
Avoid duplicate component generation by default
1 parent ddeabc1 commit 860a89a

2 files changed

Lines changed: 18 additions & 8 deletions

File tree

openapi-cli/src/main/java/io/ballerina/openapi/cmd/SubCmdBase.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ private void generateOpenAPI() {
195195
public abstract String getDefaultFileName();
196196

197197
public Optional<OpenAPI> getFlattenOpenAPI(OpenAPI openAPI) {
198-
// Flatten the OpenAPI definition with `flattenComposedSchemas: true` and `skipMatches: true`
199-
InlineModelResolver inlineModelResolver = new InlineModelResolver(true, true);
198+
// Flatten the OpenAPI definition with `flattenComposedSchemas: true` and `skipMatches: false`
199+
InlineModelResolver inlineModelResolver = new InlineModelResolver(true, false);
200200
inlineModelResolver.flatten(openAPI);
201201
return Optional.of(openAPI);
202202
}

openapi-core/src/main/java/io/ballerina/openapi/core/generators/common/InlineModelResolver.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,14 @@ private void flattenBody(String pathname, RequestBody body) {
116116
if (model.getProperties() != null && !model.getProperties().isEmpty()) {
117117
flattenProperties(model.getProperties(), pathname);
118118
String modelName = resolveModelName(model.getTitle(), genericName);
119-
mediaType.setSchema(new Schema().$ref(modelName));
120-
addGenerated(modelName, model);
121-
openAPI.getComponents().addSchemas(modelName, model);
119+
String existing = matchGenerated(model);
120+
if (existing != null) {
121+
mediaType.setSchema(new Schema().$ref(existing));
122+
} else {
123+
mediaType.setSchema(new Schema().$ref(modelName));
124+
addGenerated(modelName, model);
125+
openAPI.getComponents().addSchemas(modelName, model);
126+
}
122127
} else if (model instanceof ComposedSchema composedSchema) {
123128
flattenComposedSchema(composedSchema, pathname);
124129
if (model.get$ref() == null) {
@@ -170,9 +175,14 @@ private void flattenParams(String pathname, List<Parameter> parameters) {
170175
if (model.getProperties() != null && !model.getProperties().isEmpty()) {
171176
flattenProperties(model.getProperties(), pathname);
172177
String modelName = resolveModelName(model.getTitle(), parameter.getName());
173-
parameter.setSchema(new Schema().$ref(modelName));
174-
addGenerated(modelName, model);
175-
openAPI.getComponents().addSchemas(modelName, model);
178+
String existing = matchGenerated(model);
179+
if (existing != null) {
180+
parameter.setSchema(new Schema().$ref(existing));
181+
} else {
182+
parameter.setSchema(new Schema().$ref(modelName));
183+
addGenerated(modelName, model);
184+
openAPI.getComponents().addSchemas(modelName, model);
185+
}
176186
}
177187
}
178188
} else if (model instanceof ComposedSchema) {

0 commit comments

Comments
 (0)