Skip to content

Commit dd52ac8

Browse files
committed
Separate to different functions
1 parent bd54930 commit dd52ac8

File tree

1 file changed

+70
-51
lines changed
  • openapi-core/src/main/java/io/ballerina/openapi/core/generators/common

1 file changed

+70
-51
lines changed

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

+70-51
Original file line numberDiff line numberDiff line change
@@ -119,79 +119,98 @@ public OpenAPI modifyWithBallerinaConventions(OpenAPI openapi, Map<String, Strin
119119
return openapi;
120120
}
121121

122-
private static void modifyOASWithObjectPropertyNameInlineSchema(OpenAPI openAPI) {
123-
for (Map.Entry<String, PathItem> pathEntry : openAPI.getPaths().entrySet()) {
124-
PathItem pathItem = pathEntry.getValue();
125-
for (PathItem.HttpMethod method : pathItem.readOperationsMap().keySet()) {
126-
Operation operation = pathItem.readOperationsMap().get(method);
127-
if (operation.getRequestBody() != null) {
128-
checkForInlineObjects(operation.getRequestBody().getContent());
129-
}
130-
if (operation.getParameters() != null) {
131-
for (Parameter parameter : operation.getParameters()) {
132-
if (parameter.getSchema() != null) {
133-
updateInlineSchema(parameter.getSchema());
134-
}
135-
}
136-
}
137-
for (Map.Entry<String, ApiResponse> responseEntry : operation.getResponses().entrySet()) {
138-
ApiResponse response = responseEntry.getValue();
139-
if (response.getContent() != null) {
140-
checkForInlineObjects(response.getContent());
141-
}
142-
}
122+
private void modifyOASWithObjectPropertyNameInlineSchema(OpenAPI openAPI) {
123+
openAPI.getPaths().forEach((path, pathItem) -> processPathItem(pathItem));
124+
}
125+
126+
private void processPathItem(PathItem pathItem) {
127+
pathItem.readOperationsMap().forEach((method, operation) -> processOperationWithInlineSchema(operation));
128+
}
129+
130+
private void processOperationWithInlineSchema(Operation operation) {
131+
if (operation.getRequestBody() != null) {
132+
updateInlineObjectInContent(operation.getRequestBody().getContent());
133+
}
134+
processParametersWithInlineSchema(operation.getParameters());
135+
processResponsesWithInlineSchema(operation.getResponses());
136+
}
137+
138+
private void processParametersWithInlineSchema(List<Parameter> parameters) {
139+
if (parameters == null) return;
140+
141+
for (Parameter parameter : parameters) {
142+
if (parameter.getSchema() != null) {
143+
updateInlineSchema(parameter.getSchema());
143144
}
144145
}
145146
}
146147

148+
private void processResponsesWithInlineSchema(Map<String, ApiResponse> responses) {
149+
responses.forEach((status, response) -> {
150+
if (response.getContent() != null) {
151+
updateInlineObjectInContent(response.getContent());
152+
}
153+
});
154+
}
147155

148-
private static void updateInlineSchema(Schema schema) {
156+
private void updateInlineSchema(Schema<?> schema) {
149157
if (schema == null) {
150158
return;
151159
}
152-
if (schema instanceof ComposedSchema) {
153-
ComposedSchema composedSchema = (ComposedSchema) schema;
154-
if (composedSchema.getAllOf() != null) {
155-
for (Schema subSchema : composedSchema.getAllOf()) {
156-
updateInlineSchema(subSchema);
157-
}
158-
}
159-
if (composedSchema.getAnyOf() != null) {
160-
for (Schema subSchema : composedSchema.getAnyOf()) {
161-
updateInlineSchema(subSchema);
162-
}
163-
}
164-
if (composedSchema.getOneOf() != null) {
165-
for (Schema subSchema : composedSchema.getOneOf()) {
166-
updateInlineSchema(subSchema);
167-
}
168-
}
169-
} else if (schema.getType() == null && schema.get$ref() == null && schema.getProperties() != null) {
160+
handleInlineSchemaInComposedSchema(schema);
161+
handleInlineObjectSchema(schema);
162+
handleInlineSchemaItems(schema);
163+
handleInlineSchemaInAdditionalProperties(schema);
164+
handleInlineSchemaProperties(schema);
165+
}
166+
167+
private void handleInlineSchemaInComposedSchema(Schema<?> schema) {
168+
if (schema instanceof ComposedSchema composedSchema) {
169+
processSubSchemas(composedSchema.getAllOf());
170+
processSubSchemas(composedSchema.getAnyOf());
171+
processSubSchemas(composedSchema.getOneOf());
172+
}
173+
}
174+
175+
private void processSubSchemas(List<Schema> subSchemas) {
176+
if (subSchemas != null) {
177+
subSchemas.forEach(this::updateInlineSchema);
178+
}
179+
}
180+
181+
private static void handleInlineObjectSchema(Schema<?> schema) {
182+
if (isInlineObjectSchema(schema)) {
170183
Map<String, Schema> properties = schema.getProperties();
171-
if (Objects.nonNull(properties) && !properties.isEmpty()) {
172-
schema.setProperties(getPropertiesWithBallerinaNameExtension(properties));
173-
}
174-
} else if (schema instanceof ObjectSchema objectSchema && schema.getProperties() != null) {
175-
Map<String, Schema> properties = objectSchema.getProperties();
176-
if (Objects.nonNull(properties) && !properties.isEmpty()) {
184+
if (properties != null && !properties.isEmpty()) {
177185
schema.setProperties(getPropertiesWithBallerinaNameExtension(properties));
178186
}
179187
}
188+
}
189+
190+
private static boolean isInlineObjectSchema(Schema<?> schema) {
191+
return schema instanceof ObjectSchema ||
192+
(schema.getType() == null && schema.get$ref() == null && schema.getProperties() != null);
193+
}
194+
195+
private void handleInlineSchemaItems(Schema<?> schema) {
180196
if (schema.getItems() != null) {
181197
updateInlineSchema(schema.getItems());
182198
}
199+
}
200+
201+
private void handleInlineSchemaInAdditionalProperties(Schema<?> schema) {
183202
if (schema.getAdditionalProperties() instanceof Schema) {
184203
updateInlineSchema((Schema) schema.getAdditionalProperties());
185204
}
205+
}
206+
207+
private void handleInlineSchemaProperties(Schema<?> schema) {
186208
if (schema.getProperties() != null) {
187-
Map<String, Schema> properties = schema.getProperties();
188-
for (Map.Entry<String, Schema> property : properties.entrySet()) {
189-
updateInlineSchema(property.getValue());
190-
}
209+
schema.getProperties().values().forEach(this::updateInlineSchema);
191210
}
192211
}
193212

194-
private static void checkForInlineObjects(Map<String, io.swagger.v3.oas.models.media.MediaType> content) {
213+
private void updateInlineObjectInContent(Map<String, io.swagger.v3.oas.models.media.MediaType> content) {
195214
for (Map.Entry<String, io.swagger.v3.oas.models.media.MediaType> entry : content.entrySet()) {
196215
Schema schema = entry.getValue().getSchema();
197216
updateInlineSchema(schema);

0 commit comments

Comments
 (0)