From 03b5c71ef0e485a02722994ba8c8ce1c79ab52c5 Mon Sep 17 00:00:00 2001 From: lnash94 Date: Fri, 21 Jan 2022 12:20:45 +0530 Subject: [PATCH] Add fix for cyclic array scenarios --- .../service/OpenAPIComponentMapper.java | 9 ++++--- .../expected_gen/record/cyclic_record.yaml | 25 +++++++++++++++++++ .../record/cyclic_record.bal | 9 +++++++ 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/openapi-bal-service/src/main/java/io/ballerina/openapi/converter/service/OpenAPIComponentMapper.java b/openapi-bal-service/src/main/java/io/ballerina/openapi/converter/service/OpenAPIComponentMapper.java index d2e91adc3..188e1a7f1 100644 --- a/openapi-bal-service/src/main/java/io/ballerina/openapi/converter/service/OpenAPIComponentMapper.java +++ b/openapi-bal-service/src/main/java/io/ballerina/openapi/converter/service/OpenAPIComponentMapper.java @@ -189,7 +189,7 @@ private ObjectSchema generateObjectSchemaFromRecordFields(Map sc schema = components.getSchemas(); } if (property instanceof ArraySchema) { - mapArrayToArraySchema(schema, field.getValue(), (ArraySchema) property); + mapArrayToArraySchema(schema, field.getValue(), (ArraySchema) property, componentName); schema = components.getSchemas(); } // Add API documentation for record field @@ -307,7 +307,8 @@ private Schema mapEnumValues(EnumSymbol enumSymbol) { /** * Generate arraySchema for ballerina record as array type. */ - private void mapArrayToArraySchema(Map schema, RecordFieldSymbol field, ArraySchema property) { + private void mapArrayToArraySchema(Map schema, RecordFieldSymbol field, ArraySchema property, + String componentName) { TypeSymbol symbol = field.typeDescriptor(); int arrayDimensions = 0; @@ -331,7 +332,9 @@ private void mapArrayToArraySchema(Map schema, RecordFieldSymbol } else { symbolProperty.set$ref(symbol.getName().orElseThrow().trim()); TypeReferenceTypeSymbol typeRecord = (TypeReferenceTypeSymbol) symbol; - createComponentSchema(schema, typeRecord); + if (!isSameRecord(componentName, typeRecord)) { + createComponentSchema(schema, typeRecord); + } } } // Handle nested array type diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/record/cyclic_record.yaml b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/record/cyclic_record.yaml index f220c40d9..7f9860f3f 100644 --- a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/record/cyclic_record.yaml +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/record/cyclic_record.yaml @@ -34,6 +34,18 @@ paths: $ref: '#/components/schemas/ResponseError02' "202": description: Accepted + /pet03: + post: + operationId: operation_post_/pet03 + responses: + "200": + description: Ok + content: + application/json: + schema: + $ref: '#/components/schemas/ResponseError03' + "202": + description: Accepted components: schemas: ResponseError: @@ -60,3 +72,16 @@ components: oneOf: - $ref: '#/components/schemas/ResponseError02' - type: string + ResponseError03: + required: + - id + type: object + properties: + id: + type: integer + format: int32 + nullable: true + resError: + type: array + items: + $ref: '#/components/schemas/ResponseError03' diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/record/cyclic_record.bal b/openapi-cli/src/test/resources/ballerina-to-openapi/record/cyclic_record.bal index 60a120e1a..dc6e3a6f9 100644 --- a/openapi-cli/src/test/resources/ballerina-to-openapi/record/cyclic_record.bal +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/record/cyclic_record.bal @@ -10,6 +10,11 @@ type ResponseError02 record {| ResponseError02|string resError?; |}; +type ResponseError03 record {| + int? id; + ResponseError03[] resError?; +|}; + listener http:Listener ep0 = new (443, config = {host: "petstore.swagger.io"}); service /payloadV on ep0 { @@ -21,4 +26,8 @@ service /payloadV on ep0 { http:Accepted accept = {body: ()}; return accept; } + resource function post pet03() returns ResponseError03|http:Accepted { + http:Accepted accept = {body: ()}; + return accept; + } }