diff --git a/openapi-core/src/main/java/io/ballerina/openapi/core/generators/common/OASModifier.java b/openapi-core/src/main/java/io/ballerina/openapi/core/generators/common/OASModifier.java index 4ed7aefe2..01a899cd3 100644 --- a/openapi-core/src/main/java/io/ballerina/openapi/core/generators/common/OASModifier.java +++ b/openapi-core/src/main/java/io/ballerina/openapi/core/generators/common/OASModifier.java @@ -115,86 +115,107 @@ public OpenAPI modifyWithBallerinaConventions(OpenAPI openapi, Map pathEntry : openAPI.getPaths().entrySet()) { - PathItem pathItem = pathEntry.getValue(); - for (PathItem.HttpMethod method : pathItem.readOperationsMap().keySet()) { - Operation operation = pathItem.readOperationsMap().get(method); - if (operation.getRequestBody() != null) { - checkForInlineObjects(operation.getRequestBody().getContent()); - } - if (operation.getParameters() != null) { - for (Parameter parameter : operation.getParameters()) { - if (parameter.getSchema() != null) { - updateInlineSchema(parameter.getSchema()); - } - } - } - for (Map.Entry responseEntry : operation.getResponses().entrySet()) { - ApiResponse response = responseEntry.getValue(); - if (response.getContent() != null) { - checkForInlineObjects(response.getContent()); - } - } + private void modifyOASWithObjectPropertyNameInlineObjectSchema(OpenAPI openAPI) { + openAPI.getPaths().forEach((path, pathItem) -> processPathItem(pathItem)); + } + + private void processPathItem(PathItem pathItem) { + pathItem.readOperationsMap().forEach((method, operation) -> processOperationWithInlineObjectSchema(operation)); + } + + private void processOperationWithInlineObjectSchema(Operation operation) { + if (operation.getRequestBody() != null) { + updateInlineObjectInContent(operation.getRequestBody().getContent()); + } + processParametersWithInlineObjectSchema(operation.getParameters()); + processResponsesWithInlineObjectSchema(operation.getResponses()); + } + + private void processParametersWithInlineObjectSchema(List parameters) { + if (parameters == null) { + return; + } + + for (Parameter parameter : parameters) { + if (parameter.getSchema() != null) { + updateInlineObjectSchema(parameter.getSchema()); } } } + private void processResponsesWithInlineObjectSchema(Map responses) { + responses.forEach((status, response) -> { + if (response.getContent() != null) { + updateInlineObjectInContent(response.getContent()); + } + }); + } - private static void updateInlineSchema(Schema schema) { + private void updateInlineObjectSchema(Schema schema) { if (schema == null) { return; } - if (schema instanceof ComposedSchema) { - ComposedSchema composedSchema = (ComposedSchema) schema; - if (composedSchema.getAllOf() != null) { - for (Schema subSchema : composedSchema.getAllOf()) { - updateInlineSchema(subSchema); - } - } - if (composedSchema.getAnyOf() != null) { - for (Schema subSchema : composedSchema.getAnyOf()) { - updateInlineSchema(subSchema); - } - } - if (composedSchema.getOneOf() != null) { - for (Schema subSchema : composedSchema.getOneOf()) { - updateInlineSchema(subSchema); - } - } - } else if (schema.getType() == null && schema.get$ref() == null && schema.getProperties() != null) { + handleInlineObjectSchemaInComposedSchema(schema); + handleInlineObjectSchema(schema); + handleInlineObjectSchemaItems(schema); + handleInlineObjectSchemaInAdditionalProperties(schema); + handleInlineObjectSchemaProperties(schema); + } + + private void handleInlineObjectSchemaInComposedSchema(Schema schema) { + if (schema instanceof ComposedSchema composedSchema) { + processSubSchemas(composedSchema.getAllOf()); + processSubSchemas(composedSchema.getAnyOf()); + processSubSchemas(composedSchema.getOneOf()); + } + } + + private void processSubSchemas(List subSchemas) { + if (subSchemas != null) { + subSchemas.forEach(this::updateInlineObjectSchema); + } + } + + private static void handleInlineObjectSchema(Schema schema) { + if (isInlineObjectSchema(schema)) { Map properties = schema.getProperties(); - if (Objects.nonNull(properties) && !properties.isEmpty()) { - schema.setProperties(getPropertiesWithBallerinaNameExtension(properties)); - } - } else if (schema instanceof ObjectSchema objectSchema && schema.getProperties() != null) { - Map properties = objectSchema.getProperties(); - if (Objects.nonNull(properties) && !properties.isEmpty()) { + if (properties != null && !properties.isEmpty()) { schema.setProperties(getPropertiesWithBallerinaNameExtension(properties)); } } + } + + private static boolean isInlineObjectSchema(Schema schema) { + return schema instanceof ObjectSchema || + (schema.getType() == null && schema.get$ref() == null && schema.getProperties() != null); + } + + private void handleInlineObjectSchemaItems(Schema schema) { if (schema.getItems() != null) { - updateInlineSchema(schema.getItems()); + updateInlineObjectSchema(schema.getItems()); } + } + + private void handleInlineObjectSchemaInAdditionalProperties(Schema schema) { if (schema.getAdditionalProperties() instanceof Schema) { - updateInlineSchema((Schema) schema.getAdditionalProperties()); + updateInlineObjectSchema((Schema) schema.getAdditionalProperties()); } + } + + private void handleInlineObjectSchemaProperties(Schema schema) { if (schema.getProperties() != null) { - Map properties = schema.getProperties(); - for (Map.Entry property : properties.entrySet()) { - updateInlineSchema(property.getValue()); - } + schema.getProperties().values().forEach(this::updateInlineObjectSchema); } } - private static void checkForInlineObjects(Map content) { + private void updateInlineObjectInContent(Map content) { for (Map.Entry entry : content.entrySet()) { Schema schema = entry.getValue().getSchema(); - updateInlineSchema(schema); + updateInlineObjectSchema(schema); } }