From 471f78fe54e0ad1b98d9918f296b37ad9a505cf0 Mon Sep 17 00:00:00 2001 From: aneeshafedo Date: Fri, 21 Jul 2023 10:26:17 +0530 Subject: [PATCH 1/3] Add support for byte[] returns --- .../openapi/converter/Constants.java | 1 + .../service/OpenAPIResponseMapper.java | 3 + .../generators/openapi/ResponseTests.java | 10 +++ .../expected_gen/response/byte.yaml | 61 +++++++++++++++++++ .../ballerina-to-openapi/response/byte.bal | 15 +++++ 5 files changed, 90 insertions(+) create mode 100644 openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/response/byte.yaml create mode 100644 openapi-cli/src/test/resources/ballerina-to-openapi/response/byte.bal diff --git a/openapi-bal-service/src/main/java/io/ballerina/openapi/converter/Constants.java b/openapi-bal-service/src/main/java/io/ballerina/openapi/converter/Constants.java index 3247f8df1..88f63b674 100644 --- a/openapi-bal-service/src/main/java/io/ballerina/openapi/converter/Constants.java +++ b/openapi-bal-service/src/main/java/io/ballerina/openapi/converter/Constants.java @@ -46,6 +46,7 @@ public class Constants { public static final String TYPEREFERENCE = "typeReference"; public static final String HTTP_HEADER = "http:Header"; public static final String BYTE_ARRAY = "byte[]"; + public static final String BYTE = "byte"; public static final String OCTET_STREAM = "octet-stream"; public static final String XML = "xml"; public static final String JSON = "json"; diff --git a/openapi-bal-service/src/main/java/io/ballerina/openapi/converter/service/OpenAPIResponseMapper.java b/openapi-bal-service/src/main/java/io/ballerina/openapi/converter/service/OpenAPIResponseMapper.java index 1958b9ee6..19f0941bd 100644 --- a/openapi-bal-service/src/main/java/io/ballerina/openapi/converter/service/OpenAPIResponseMapper.java +++ b/openapi-bal-service/src/main/java/io/ballerina/openapi/converter/service/OpenAPIResponseMapper.java @@ -94,6 +94,8 @@ import static io.ballerina.compiler.syntax.tree.SyntaxKind.TYPE_REFERENCE; import static io.ballerina.openapi.converter.Constants.APPLICATION_PREFIX; import static io.ballerina.openapi.converter.Constants.BODY; +import static io.ballerina.openapi.converter.Constants.BYTE; +import static io.ballerina.openapi.converter.Constants.BYTE_ARRAY; import static io.ballerina.openapi.converter.Constants.CACHE_CONTROL; import static io.ballerina.openapi.converter.Constants.ETAG; import static io.ballerina.openapi.converter.Constants.FALSE; @@ -621,6 +623,7 @@ private Optional getApiResponsesForArrayTypes(OperationAdaptor ope ArraySchema arraySchema = new ArraySchema(); String type02 = array.memberTypeDesc().kind().toString().trim().split("_")[0]. toLowerCase(Locale.ENGLISH); + type02 = type02.equals(BYTE) ? BYTE_ARRAY : type02; Schema openApiSchema = ConverterCommonUtils.getOpenApiSchema(type02); Optional mimeType = convertBallerinaMIMEToOASMIMETypes(type02, customMediaPrefix); if (mimeType.isEmpty()) { diff --git a/openapi-cli/src/test/java/io/ballerina/openapi/generators/openapi/ResponseTests.java b/openapi-cli/src/test/java/io/ballerina/openapi/generators/openapi/ResponseTests.java index e0b8a943d..2916d3f6f 100644 --- a/openapi-cli/src/test/java/io/ballerina/openapi/generators/openapi/ResponseTests.java +++ b/openapi-cli/src/test/java/io/ballerina/openapi/generators/openapi/ResponseTests.java @@ -256,6 +256,16 @@ public void testResponseWithDecimalReturnType() throws IOException { compareWithGeneratedFile(ballerinaFilePath, "response/decimal.yaml"); } + @Test(description = "When the response has byte[] return type") + public void testResponseWithByteArray() throws IOException { + Path ballerinaFilePath = RES_DIR.resolve("response/byte.bal"); + OASContractGenerator openApiConverterUtils = new OASContractGenerator(); + openApiConverterUtils.generateOAS3DefinitionsAllService(ballerinaFilePath, this.tempDir, null + , false); + Assert.assertTrue(openApiConverterUtils.getErrors().isEmpty()); + compareWithGeneratedFile(ballerinaFilePath, "response/byte.yaml"); + } + @Test public void testWithMultipleReturnPayloadSameStatusCode() throws IOException { Path ballerinaFilePath = RES_DIR.resolve("response/same_status_code.bal"); diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/response/byte.yaml b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/response/byte.yaml new file mode 100644 index 000000000..c9fa12ca5 --- /dev/null +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/response/byte.yaml @@ -0,0 +1,61 @@ +openapi: 3.0.1 +info: + title: PayloadV + version: 0.0.0 +servers: + - url: "{server}:{port}/payloadV" + variables: + server: + default: http://localhost + port: + default: "9090" +paths: + /challenges/{challenged}: + get: + operationId: getChallengesChallenged + parameters: + - name: challenged + in: path + required: true + schema: + type: string + responses: + "200": + description: Ok + content: + application/octet-stream: + schema: + type: array + items: + type: string + format: uuid + "500": + description: Internal server error + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorPayload' +components: + schemas: + ErrorPayload: + type: object + properties: + reason: + type: string + description: Reason phrase + path: + type: string + description: Request path + method: + type: string + description: Method type of the request + message: + type: string + description: Error message + timestamp: + type: string + description: Timestamp of the error + status: + type: integer + description: Relevant HTTP status code + format: int32 \ No newline at end of file diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/response/byte.bal b/openapi-cli/src/test/resources/ballerina-to-openapi/response/byte.bal new file mode 100644 index 000000000..8f402509d --- /dev/null +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/response/byte.bal @@ -0,0 +1,15 @@ +import ballerina/http; + +public type ReturnValueXML xml; + +public type ReturnValueCustomRec record {| + *http:BadGateway; + ReturnValueXML body; +|}; + +service /payloadV on new http:Listener(9090) { + + resource function get challenges/[string challenged]() returns byte[]|error { + return error(""); + } +} From 62610610bb8230701702d557cf3dccbd4aac54aa Mon Sep 17 00:00:00 2001 From: aneeshafedo Date: Tue, 8 Aug 2023 09:58:09 +0530 Subject: [PATCH 2/3] Add review suggestions --- .../converter/service/OpenAPIResponseMapper.java | 12 ++++++------ .../converter/utils/ConverterCommonUtils.java | 4 ++-- .../expected_gen/response/byte.yaml | 6 ++---- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/openapi-bal-service/src/main/java/io/ballerina/openapi/converter/service/OpenAPIResponseMapper.java b/openapi-bal-service/src/main/java/io/ballerina/openapi/converter/service/OpenAPIResponseMapper.java index 19f0941bd..19ed05acd 100644 --- a/openapi-bal-service/src/main/java/io/ballerina/openapi/converter/service/OpenAPIResponseMapper.java +++ b/openapi-bal-service/src/main/java/io/ballerina/openapi/converter/service/OpenAPIResponseMapper.java @@ -606,10 +606,10 @@ private Optional getApiResponsesForArrayTypes(OperationAdaptor ope String statusCode = httpMethod.equals(POST) ? HTTP_201 : HTTP_200; String description = httpMethod.equals(POST) ? HTTP_201_DESCRIPTION : HTTP_200_DESCRIPTION; - Map schemas02 = components.getSchemas(); + Map schemas = components.getSchemas(); if (array.memberTypeDesc().kind() == SIMPLE_NAME_REFERENCE) { handleReferenceResponse(operationAdaptor, (SimpleNameReferenceNode) array.memberTypeDesc(), - schemas02, apiResponses, customMediaPrefix, headers); + schemas, apiResponses, customMediaPrefix, headers); } else if (array.memberTypeDesc().kind() == QUALIFIED_NAME_REFERENCE) { Optional optionalAPIResponses = handleQualifiedNameType(new ApiResponses(), customMediaPrefix, headers, apiResponse, @@ -621,11 +621,11 @@ private Optional getApiResponsesForArrayTypes(OperationAdaptor ope } } else { ArraySchema arraySchema = new ArraySchema(); - String type02 = array.memberTypeDesc().kind().toString().trim().split("_")[0]. + String type = array.memberTypeDesc().kind().toString().trim().split("_")[0]. toLowerCase(Locale.ENGLISH); - type02 = type02.equals(BYTE) ? BYTE_ARRAY : type02; - Schema openApiSchema = ConverterCommonUtils.getOpenApiSchema(type02); - Optional mimeType = convertBallerinaMIMEToOASMIMETypes(type02, customMediaPrefix); + type = type.equals(BYTE) ? BYTE_ARRAY : type; + Schema openApiSchema = ConverterCommonUtils.getOpenApiSchema(type); + Optional mimeType = convertBallerinaMIMEToOASMIMETypes(type, customMediaPrefix); if (mimeType.isEmpty()) { return Optional.empty(); } diff --git a/openapi-bal-service/src/main/java/io/ballerina/openapi/converter/utils/ConverterCommonUtils.java b/openapi-bal-service/src/main/java/io/ballerina/openapi/converter/utils/ConverterCommonUtils.java index 6a1173652..a2e72b7b5 100644 --- a/openapi-bal-service/src/main/java/io/ballerina/openapi/converter/utils/ConverterCommonUtils.java +++ b/openapi-bal-service/src/main/java/io/ballerina/openapi/converter/utils/ConverterCommonUtils.java @@ -124,7 +124,7 @@ public static Schema getOpenApiSchema(String type) { case Constants.BYTE_ARRAY: case Constants.OCTET_STREAM: schema = new StringSchema(); - schema.setFormat("uuid"); + schema.setFormat("byte"); break; case Constants.NUMBER: case Constants.DECIMAL: @@ -179,7 +179,7 @@ public static Schema getOpenApiSchema(SyntaxKind type) { break; case BYTE_TYPE_DESC: schema = new StringSchema(); - schema.setFormat("uuid"); + schema.setFormat("byte"); break; case DECIMAL_TYPE_DESC: schema = new NumberSchema(); diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/response/byte.yaml b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/response/byte.yaml index c9fa12ca5..349f136c5 100644 --- a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/response/byte.yaml +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/response/byte.yaml @@ -25,10 +25,8 @@ paths: content: application/octet-stream: schema: - type: array - items: - type: string - format: uuid + type: string + format: byte "500": description: Internal server error content: From 4817f44b7c3808df5369bdb89a7bb145909ccf5c Mon Sep 17 00:00:00 2001 From: aneeshafedo Date: Wed, 9 Aug 2023 12:06:29 +0530 Subject: [PATCH 3/3] Fix build errors --- .../openapi/converter/service/OpenAPIResponseMapper.java | 8 ++++++-- .../expected_gen/request_body/rb_scenario09.yaml | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/openapi-bal-service/src/main/java/io/ballerina/openapi/converter/service/OpenAPIResponseMapper.java b/openapi-bal-service/src/main/java/io/ballerina/openapi/converter/service/OpenAPIResponseMapper.java index 19ed05acd..bad47045b 100644 --- a/openapi-bal-service/src/main/java/io/ballerina/openapi/converter/service/OpenAPIResponseMapper.java +++ b/openapi-bal-service/src/main/java/io/ballerina/openapi/converter/service/OpenAPIResponseMapper.java @@ -629,8 +629,12 @@ private Optional getApiResponsesForArrayTypes(OperationAdaptor ope if (mimeType.isEmpty()) { return Optional.empty(); } - arraySchema.setItems(openApiSchema); - mediaType.setSchema(arraySchema); + if (type.equals(BYTE_ARRAY)) { + mediaType.setSchema(openApiSchema); + } else { + arraySchema.setItems(openApiSchema); + mediaType.setSchema(arraySchema); + } apiResponse.description(description); apiResponse.content(new Content().addMediaType(mimeType.get(), mediaType)); apiResponses.put(statusCode, apiResponse); diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/request_body/rb_scenario09.yaml b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/request_body/rb_scenario09.yaml index d296316bd..fdcfd5b5c 100644 --- a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/request_body/rb_scenario09.yaml +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/request_body/rb_scenario09.yaml @@ -7,7 +7,7 @@ paths: application/octet-stream: schema: type: string - format: uuid + format: byte responses: "200": description: Ok