From bd54930cc1e7eec9f39416185385282f3f5f59e8 Mon Sep 17 00:00:00 2001 From: lnash94 Date: Wed, 25 Sep 2024 21:56:11 +0530 Subject: [PATCH 1/4] Decouple flatten from Sanitize command --- .../io/ballerina/openapi/cmd/Sanitize.java | 2 +- .../sanitized_openapi_composed_schema.json | 501 ++++++++---------- .../core/generators/common/OASModifier.java | 83 ++- 3 files changed, 305 insertions(+), 281 deletions(-) diff --git a/openapi-cli/src/main/java/io/ballerina/openapi/cmd/Sanitize.java b/openapi-cli/src/main/java/io/ballerina/openapi/cmd/Sanitize.java index a4c7dc412..70501b633 100644 --- a/openapi-cli/src/main/java/io/ballerina/openapi/cmd/Sanitize.java +++ b/openapi-cli/src/main/java/io/ballerina/openapi/cmd/Sanitize.java @@ -54,7 +54,7 @@ public String getDefaultFileName() { @Override public Optional generate(String openAPIFileContent) { Optional filteredOpenAPI = getFilteredOpenAPI(openAPIFileContent); - return filteredOpenAPI.flatMap(this::getFlattenOpenAPI).flatMap(this::sanitizeOpenAPI); + return filteredOpenAPI.flatMap(this::sanitizeOpenAPI); } private Optional sanitizeOpenAPI(OpenAPI openAPI) { diff --git a/openapi-cli/src/test/resources/cmd/sanitize/sanitized_openapi_composed_schema.json b/openapi-cli/src/test/resources/cmd/sanitize/sanitized_openapi_composed_schema.json index 0f28589f5..205e5954c 100644 --- a/openapi-cli/src/test/resources/cmd/sanitize/sanitized_openapi_composed_schema.json +++ b/openapi-cli/src/test/resources/cmd/sanitize/sanitized_openapi_composed_schema.json @@ -1,116 +1,135 @@ { - "openapi": "3.0.1", - "info": { - "title": "Api V1", - "version": "0.0.0" + "openapi" : "3.0.1", + "info" : { + "title" : "Api V1", + "version" : "0.0.0" }, - "servers": [ - { - "url": "http://{server}:{port}/api/v1", - "variables": { - "server": { - "default": "localhost" - }, - "port": { - "default": "8080" - } + "servers" : [ { + "url" : "http://{server}:{port}/api/v1", + "variables" : { + "server" : { + "default" : "localhost" + }, + "port" : { + "default" : "8080" } } - ], - "paths": { - "/albums": { - "get": { - "tags": [ - "albums" - ], - "operationId": "getAlbums", - "parameters": [ - { - "name": "_artists_", - "in": "query", - "required": false, - "style": "form", - "explode": true, - "schema": { - "type": "array", - "items": { - "type": "string" - }, - "default": [] + } ], + "paths" : { + "/albums" : { + "get" : { + "tags" : [ "albums" ], + "operationId" : "getAlbums", + "parameters" : [ { + "name" : "_artists_", + "in" : "query", + "required" : false, + "style" : "form", + "explode" : true, + "schema" : { + "type" : "array", + "items" : { + "type" : "string" }, - "x-ballerina-name": "artists" + "default" : [ ] }, - { - "name": "X-API-VERSION", - "in": "header", - "required": false, - "style": "simple", - "explode": false, - "schema": { - "type": "string", - "default": "v1" - }, - "x-ballerina-name": "xAPIVERSION" - } - ], - "responses": { - "200": { - "description": "Ok", - "content": { - "application/json": { - "schema": { - "type": "array", - "items": { - "$ref": "#/components/schemas/Album" + "x-ballerina-name" : "artists" + }, { + "name" : "X-API-VERSION", + "in" : "header", + "required" : false, + "style" : "simple", + "explode" : false, + "schema" : { + "type" : "string", + "default" : "v1" + }, + "x-ballerina-name" : "xAPIVERSION" + } ], + "responses" : { + "200" : { + "description" : "Ok", + "content" : { + "application/json" : { + "schema" : { + "type" : "array", + "items" : { + "$ref" : "#/components/schemas/Album" } } } } }, - "400": { - "description": "BadRequest", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/ErrorPayload" + "400" : { + "description" : "BadRequest", + "content" : { + "application/json" : { + "schema" : { + "$ref" : "#/components/schemas/ErrorPayload" } } } } } }, - "post": { - "tags": [ - "albums" - ], - "operationId": "postAlbum", - "requestBody": { - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/AlbumsBody" + "post" : { + "tags" : [ "albums" ], + "operationId" : "postAlbum", + "requestBody" : { + "content" : { + "application/json" : { + "schema" : { + "oneOf" : [ { + "required" : [ "_id", "artist", "title" ], + "type" : "object", + "properties" : { + "artist" : { + "type" : "string" + }, + "_id" : { + "type" : "string", + "x-ballerina-name" : "id" + }, + "title" : { + "type" : "string" + } + }, + "additionalProperties" : false + }, { + "required" : [ "artist", "title" ], + "type" : "object", + "properties" : { + "artist" : { + "type" : "string" + }, + "title" : { + "type" : "string" + } + }, + "additionalProperties" : false + } ] } } }, - "required": true + "required" : true }, - "responses": { - "201": { - "description": "Created", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/Album" + "responses" : { + "201" : { + "description" : "Created", + "content" : { + "application/json" : { + "schema" : { + "$ref" : "#/components/schemas/Album" } } } }, - "400": { - "description": "BadRequest", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/ErrorPayload" + "400" : { + "description" : "BadRequest", + "content" : { + "application/json" : { + "schema" : { + "$ref" : "#/components/schemas/ErrorPayload" } } } @@ -118,51 +137,47 @@ } } }, - "/albums/{id}": { - "get": { - "tags": [ - "albums" - ], - "operationId": "getAlbumById", - "parameters": [ - { - "name": "id", - "in": "path", - "required": true, - "style": "simple", - "explode": false, - "schema": { - "type": "string" - } + "/albums/{id}" : { + "get" : { + "tags" : [ "albums" ], + "operationId" : "getAlbumById", + "parameters" : [ { + "name" : "id", + "in" : "path", + "required" : true, + "style" : "simple", + "explode" : false, + "schema" : { + "type" : "string" } - ], - "responses": { - "200": { - "description": "Ok", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/Album" + } ], + "responses" : { + "200" : { + "description" : "Ok", + "content" : { + "application/json" : { + "schema" : { + "$ref" : "#/components/schemas/Album" } } } }, - "400": { - "description": "BadRequest", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/ErrorPayload" + "400" : { + "description" : "BadRequest", + "content" : { + "application/json" : { + "schema" : { + "$ref" : "#/components/schemas/ErrorPayload" } } } }, - "404": { - "description": "NotFound", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/Message" + "404" : { + "description" : "NotFound", + "content" : { + "application/json" : { + "schema" : { + "$ref" : "#/components/schemas/Message" } } } @@ -170,41 +185,53 @@ } } }, - "/albums/{id}/artist": { - "get": { - "tags": [ - "artists" - ], - "operationId": "getArtistByAlbum", - "parameters": [ - { - "name": "id", - "in": "path", - "required": true, - "style": "simple", - "explode": false, - "schema": { - "type": "string" - } + "/albums/{id}/artist" : { + "get" : { + "tags" : [ "artists" ], + "operationId" : "getArtistByAlbum", + "parameters" : [ { + "name" : "id", + "in" : "path", + "required" : true, + "style" : "simple", + "explode" : false, + "schema" : { + "type" : "string" } - ], - "responses": { - "200": { - "description": "Ok", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/InlineResponse200" + } ], + "responses" : { + "200" : { + "description" : "Ok", + "content" : { + "application/json" : { + "schema" : { + "required" : [ "albums", "id", "name" ], + "type" : "object", + "properties" : { + "albums" : { + "type" : "array", + "items" : { + "$ref" : "#/components/schemas/Album" + } + }, + "name" : { + "type" : "string" + }, + "id" : { + "type" : "string" + } + }, + "additionalProperties" : false } } } }, - "400": { - "description": "BadRequest", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/ErrorPayload" + "400" : { + "description" : "BadRequest", + "content" : { + "application/json" : { + "schema" : { + "$ref" : "#/components/schemas/ErrorPayload" } } } @@ -213,147 +240,63 @@ } } }, - "components": { - "schemas": { - "ErrorPayload": { - "required": [ - "message", - "method", - "path", - "reason", - "status", - "timestamp" - ], - "type": "object", - "properties": { - "reason": { - "type": "string" + "components" : { + "schemas" : { + "ErrorPayload" : { + "required" : [ "message", "method", "path", "reason", "status", "timestamp" ], + "type" : "object", + "properties" : { + "reason" : { + "type" : "string" }, - "path": { - "type": "string" + "path" : { + "type" : "string" }, - "method": { - "type": "string" + "method" : { + "type" : "string" }, - "message": { - "type": "string" + "message" : { + "type" : "string" }, - "timestamp": { - "type": "string" + "timestamp" : { + "type" : "string" }, - "status": { - "type": "integer", - "format": "int64" + "status" : { + "type" : "integer", + "format" : "int64" } } }, - "AlbumsBody": { - "oneOf": [ - { - "$ref": "#/components/schemas/AlbumsOneOf1" - }, - { - "$ref": "#/components/schemas/AlbumsalbumsOneOf12" - } - ] - }, - "Message": { - "required": [ - "code", - "message" - ], - "type": "object", - "properties": { - "code": { - "type": "integer", - "format": "int64" - }, - "message": { - "type": "string" - } - }, - "additionalProperties": false - }, - "AlbumsalbumsOneOf12": { - "required": [ - "artist", - "title" - ], - "type": "object", - "properties": { - "artist": { - "type": "string" - }, - "title": { - "type": "string" - } - }, - "additionalProperties": false - }, - "Album": { - "required": [ - "_id", - "artist", - "title" - ], - "type": "object", - "properties": { - "artist": { - "type": "string" - }, - "_id": { - "type": "string", - "x-ballerina-name": "id" - }, - "title": { - "type": "string" - } - }, - "additionalProperties": false - }, - "InlineResponse200": { - "required": [ - "albums", - "id", - "name" - ], - "type": "object", - "properties": { - "albums": { - "type": "array", - "items": { - "$ref": "#/components/schemas/Album" - } - }, - "name": { - "type": "string" + "Message" : { + "required" : [ "code", "message" ], + "type" : "object", + "properties" : { + "code" : { + "type" : "integer", + "format" : "int64" }, - "id": { - "type": "string" + "message" : { + "type" : "string" } }, - "additionalProperties": false + "additionalProperties" : false }, - "AlbumsOneOf1": { - "required": [ - "_id", - "artist", - "title" - ], - "type": "object", - "properties": { - "artist": { - "type": "string" + "Album" : { + "required" : [ "_id", "artist", "title" ], + "type" : "object", + "properties" : { + "artist" : { + "type" : "string" }, - "_id": { - "type": "string", - "x-ballerina-name": "id" + "_id" : { + "type" : "string", + "x-ballerina-name" : "id" }, - "title": { - "type": "string" + "title" : { + "type" : "string" } }, - "additionalProperties": false + "additionalProperties" : false } } } 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 cfb8cf831..4ed7aefe2 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 @@ -32,6 +32,7 @@ import io.swagger.v3.oas.models.media.ObjectSchema; import io.swagger.v3.oas.models.media.Schema; import io.swagger.v3.oas.models.parameters.Parameter; +import io.swagger.v3.oas.models.responses.ApiResponse; import io.swagger.v3.parser.core.models.ParseOptions; import io.swagger.v3.parser.core.models.SwaggerParseResult; @@ -114,9 +115,89 @@ 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 static void updateInlineSchema(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) { + 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()) { + schema.setProperties(getPropertiesWithBallerinaNameExtension(properties)); + } + } + if (schema.getItems() != null) { + updateInlineSchema(schema.getItems()); + } + if (schema.getAdditionalProperties() instanceof Schema) { + updateInlineSchema((Schema) schema.getAdditionalProperties()); + } + if (schema.getProperties() != null) { + Map properties = schema.getProperties(); + for (Map.Entry property : properties.entrySet()) { + updateInlineSchema(property.getValue()); + } + } + } + + private static void checkForInlineObjects(Map content) { + for (Map.Entry entry : content.entrySet()) { + Schema schema = entry.getValue().getSchema(); + updateInlineSchema(schema); + } + } + private static void modifyOASWithObjectPropertyName(OpenAPI openapi) { Components components = openapi.getComponents(); if (Objects.isNull(components) || Objects.isNull(components.getSchemas())) { @@ -338,7 +419,7 @@ private static Schema getSchemaWithBallerinaNameExtension(String propertyName, S } if (Objects.nonNull(propertySchema.get$ref())) { - Schema refSchema = new Schema<>(); + Schema refSchema = new Schema<>(); refSchema.set$ref(propertySchema.get$ref()); propertySchema.set$ref(null); propertySchema.addAllOfItem(refSchema); From b497212d4b41750eb4731e1ede405755ee2c84af Mon Sep 17 00:00:00 2001 From: lnash94 Date: Wed, 25 Sep 2024 22:43:16 +0530 Subject: [PATCH 2/4] Separate to different functions --- .../core/generators/common/OASModifier.java | 131 ++++++++++-------- 1 file changed, 76 insertions(+), 55 deletions(-) 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); } } From 7291142f50733aee52a6174c959a35aaeb57b407 Mon Sep 17 00:00:00 2001 From: lnash94 Date: Thu, 26 Sep 2024 11:12:56 +0530 Subject: [PATCH 3/4] Fix review suggestions --- .../cli-help/ballerina-openapi-sanitize.help | 4 +--- .../core/generators/common/OASModifier.java | 21 ++++++++++--------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/openapi-cli/src/main/resources/cli-help/ballerina-openapi-sanitize.help b/openapi-cli/src/main/resources/cli-help/ballerina-openapi-sanitize.help index d2154a38a..cd5aac50d 100644 --- a/openapi-cli/src/main/resources/cli-help/ballerina-openapi-sanitize.help +++ b/openapi-cli/src/main/resources/cli-help/ballerina-openapi-sanitize.help @@ -12,9 +12,7 @@ SYNOPSIS DESCRIPTION Sanitize the OpenAPI contract file according to the best naming - practices of Ballerina. The OpenAPI contract is flatten and the type - schema names are made Ballerina friendly. The Ballerina name - extensions are added to the schemas which can not be modified + practices of Ballerina. The Ballerina name extensions are added to the schemas which can not be modified directly. 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 01a899cd3..6aaac3d61 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 @@ -128,7 +128,7 @@ private void processPathItem(PathItem pathItem) { } private void processOperationWithInlineObjectSchema(Operation operation) { - if (operation.getRequestBody() != null) { + if (Objects.nonNull(operation.getRequestBody())) { updateInlineObjectInContent(operation.getRequestBody().getContent()); } processParametersWithInlineObjectSchema(operation.getParameters()); @@ -136,12 +136,12 @@ private void processOperationWithInlineObjectSchema(Operation operation) { } private void processParametersWithInlineObjectSchema(List parameters) { - if (parameters == null) { + if (Objects.isNull(parameters)) { return; } for (Parameter parameter : parameters) { - if (parameter.getSchema() != null) { + if (Objects.nonNull(parameter.getSchema())) { updateInlineObjectSchema(parameter.getSchema()); } } @@ -149,14 +149,14 @@ private void processParametersWithInlineObjectSchema(List parameters) private void processResponsesWithInlineObjectSchema(Map responses) { responses.forEach((status, response) -> { - if (response.getContent() != null) { + if (Objects.nonNull(response.getContent())) { updateInlineObjectInContent(response.getContent()); } }); } private void updateInlineObjectSchema(Schema schema) { - if (schema == null) { + if (Objects.isNull(schema)) { return; } handleInlineObjectSchemaInComposedSchema(schema); @@ -175,7 +175,7 @@ private void handleInlineObjectSchemaInComposedSchema(Schema schema) { } private void processSubSchemas(List subSchemas) { - if (subSchemas != null) { + if (Objects.nonNull(subSchemas)) { subSchemas.forEach(this::updateInlineObjectSchema); } } @@ -183,7 +183,7 @@ private void processSubSchemas(List subSchemas) { private static void handleInlineObjectSchema(Schema schema) { if (isInlineObjectSchema(schema)) { Map properties = schema.getProperties(); - if (properties != null && !properties.isEmpty()) { + if (Objects.nonNull(properties) && !properties.isEmpty()) { schema.setProperties(getPropertiesWithBallerinaNameExtension(properties)); } } @@ -191,11 +191,12 @@ private static void handleInlineObjectSchema(Schema schema) { private static boolean isInlineObjectSchema(Schema schema) { return schema instanceof ObjectSchema || - (schema.getType() == null && schema.get$ref() == null && schema.getProperties() != null); + (Objects.isNull(schema.getType()) && Objects.isNull(schema.get$ref()) && + Objects.nonNull(schema.getProperties())); } private void handleInlineObjectSchemaItems(Schema schema) { - if (schema.getItems() != null) { + if (Objects.nonNull(schema.getItems())) { updateInlineObjectSchema(schema.getItems()); } } @@ -207,7 +208,7 @@ private void handleInlineObjectSchemaInAdditionalProperties(Schema schema) { } private void handleInlineObjectSchemaProperties(Schema schema) { - if (schema.getProperties() != null) { + if (Objects.nonNull(schema.getProperties())) { schema.getProperties().values().forEach(this::updateInlineObjectSchema); } } From b30ae39666d29f909a32bb60da3b06d4eb1ba1a3 Mon Sep 17 00:00:00 2001 From: lnash94 Date: Thu, 3 Oct 2024 08:14:25 +0530 Subject: [PATCH 4/4] Update with the review suggestions --- .../main/resources/cli-help/ballerina-openapi-sanitize.help | 4 ++-- .../ballerina/openapi/core/generators/common/OASModifier.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/openapi-cli/src/main/resources/cli-help/ballerina-openapi-sanitize.help b/openapi-cli/src/main/resources/cli-help/ballerina-openapi-sanitize.help index cd5aac50d..cbd6607e6 100644 --- a/openapi-cli/src/main/resources/cli-help/ballerina-openapi-sanitize.help +++ b/openapi-cli/src/main/resources/cli-help/ballerina-openapi-sanitize.help @@ -12,8 +12,8 @@ SYNOPSIS DESCRIPTION Sanitize the OpenAPI contract file according to the best naming - practices of Ballerina. The Ballerina name extensions are added to the schemas which can not be modified - directly. + practices of Ballerina. The Ballerina name extensions are added + to the schemas which can not be modified directly. OPTIONS 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 6aaac3d61..677686bcb 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,11 +115,11 @@ public OpenAPI modifyWithBallerinaConventions(OpenAPI openapi, Map processPathItem(pathItem)); }