From fa0903e8bd9b6ee4b74b813c42f200b3ae604336 Mon Sep 17 00:00:00 2001 From: lnash94 Date: Thu, 14 Sep 2023 10:08:56 +0530 Subject: [PATCH 1/7] Update error code details for the nil type return --- module-ballerina-openapi/Ballerina.toml | 6 +- module-ballerina-openapi/CompilerPlugin.toml | 4 +- .../service/OpenAPIResponseMapper.java | 5 + .../generators/openapi/ResponseTests.java | 10 + .../response/nil_return_type.yaml | 330 ++++++++++++++++++ .../response/nil_return_type.bal | 80 +++++ 6 files changed, 430 insertions(+), 5 deletions(-) create mode 100644 openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/response/nil_return_type.yaml create mode 100644 openapi-cli/src/test/resources/ballerina-to-openapi/response/nil_return_type.bal diff --git a/module-ballerina-openapi/Ballerina.toml b/module-ballerina-openapi/Ballerina.toml index 49e3f3887..06051f370 100644 --- a/module-ballerina-openapi/Ballerina.toml +++ b/module-ballerina-openapi/Ballerina.toml @@ -1,10 +1,10 @@ [package] org= "ballerina" name= "openapi" -version= "@toml.version@" +version= "1.8.0" [[platform.java17.dependency]] -path = "../openapi-validator/build/libs/openapi-validator-@project.version@.jar" +path = "../openapi-validator/build/libs/openapi-validator-1.8.0-SNAPSHOT.jar" groupId = "ballerina" artifactId = "openapi" -version = "@project.version@" +version = "1.8.0-SNAPSHOT" diff --git a/module-ballerina-openapi/CompilerPlugin.toml b/module-ballerina-openapi/CompilerPlugin.toml index 04c7cdeb4..2d697e902 100644 --- a/module-ballerina-openapi/CompilerPlugin.toml +++ b/module-ballerina-openapi/CompilerPlugin.toml @@ -3,7 +3,7 @@ id = "openapi-tools" class = "io.ballerina.openapi.validator.OpenAPIValidatorPlugin" [[dependency]] -path = "../openapi-validator/build/libs/openapi-validator-@project.version@.jar" +path = "../openapi-validator/build/libs/openapi-validator-1.8.0-SNAPSHOT.jar" groupId = "ballerina" artifactId = "openapi" -version = "@project.version@" +version = "1.8.0-SNAPSHOT" 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 b9c3f0242..25e2cedc9 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 @@ -199,6 +199,11 @@ public void getResourceOutput(FunctionDefinitionNode resource, OperationAdaptor ApiResponse apiResponse = new ApiResponse(); apiResponse.description("Accepted"); apiResponses.put("202", apiResponse); + if (operation.getRequestBody() != null || operation.getParameters() != null) { + ApiResponse badRequestResponse = new ApiResponse(); + badRequestResponse.description("BadRequest"); + apiResponses.put("400", badRequestResponse); + } } operation.setResponses(apiResponses); } 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 2916d3f6f..c518809ff 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 @@ -373,6 +373,16 @@ public void testResponseHasServiceConfigWithCorsWithoutBasePath() throws IOExcep Assert.assertTrue(generatedYaml.contains(expectedYamlContent)); } + @Test(description = "When the service has config without mediaType attribute") + public void testNilReturnType() throws IOException { + Path ballerinaFilePath = RES_DIR.resolve("response/nil_return_type.bal"); + OASContractGenerator openApiConverterUtils = new OASContractGenerator(); + openApiConverterUtils.generateOAS3DefinitionsAllService(ballerinaFilePath, this.tempDir, null + , false); + Assert.assertTrue(openApiConverterUtils.getErrors().isEmpty()); + compareWithGeneratedFile(ballerinaFilePath, "response/nil_return_type.yaml"); + } + @AfterMethod public void cleanUp() { TestUtils.deleteDirectory(this.tempDir); diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/response/nil_return_type.yaml b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/response/nil_return_type.yaml new file mode 100644 index 000000000..55d3ba79a --- /dev/null +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/response/nil_return_type.yaml @@ -0,0 +1,330 @@ +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: + /path1: + get: + operationId: getPath1 + responses: + "202": + description: Accepted + put: + operationId: putPath1 + responses: + "202": + description: Accepted + post: + operationId: postPath1 + responses: + "202": + description: Accepted + delete: + operationId: deletePath1 + responses: + "202": + description: Accepted + head: + operationId: headPath1 + responses: + "202": + description: Accepted + patch: + operationId: patchPath1 + responses: + "202": + description: Accepted + /path_with_query: + get: + operationId: getPathWithQuery + parameters: + - name: id + in: query + required: true + schema: + type: string + responses: + "202": + description: Accepted + "400": + description: BadRequest + put: + operationId: putPathWithQuery + parameters: + - name: id + in: query + required: true + schema: + type: string + responses: + "202": + description: Accepted + "400": + description: BadRequest + post: + operationId: postPathWithQuery + parameters: + - name: id + in: query + required: true + schema: + type: string + responses: + "202": + description: Accepted + "400": + description: BadRequest + delete: + operationId: deletePathWithQuery + parameters: + - name: id + in: query + required: true + schema: + type: string + responses: + "202": + description: Accepted + "400": + description: BadRequest + head: + operationId: headPathWithQuery + parameters: + - name: id + in: query + required: true + schema: + type: string + responses: + "202": + description: Accepted + "400": + description: BadRequest + patch: + operationId: patchPathWithQuery + parameters: + - name: id + in: query + required: true + schema: + type: string + responses: + "202": + description: Accepted + "400": + description: BadRequest + /path_with_path/{id}: + get: + operationId: getPathWithPathId + parameters: + - name: id + in: path + required: true + schema: + type: string + responses: + "202": + description: Accepted + "400": + description: BadRequest + put: + operationId: putPathWithPathId + parameters: + - name: id + in: path + required: true + schema: + type: string + responses: + "202": + description: Accepted + "400": + description: BadRequest + post: + operationId: postPathWithPathId + parameters: + - name: id + in: path + required: true + schema: + type: string + responses: + "202": + description: Accepted + "400": + description: BadRequest + delete: + operationId: deletePathWithPathId + parameters: + - name: id + in: path + required: true + schema: + type: string + responses: + "202": + description: Accepted + "400": + description: BadRequest + head: + operationId: headPathWithPathId + parameters: + - name: id + in: path + required: true + schema: + type: string + responses: + "202": + description: Accepted + "400": + description: BadRequest + patch: + operationId: patchPathWithPathId + parameters: + - name: id + in: path + required: true + schema: + type: string + responses: + "202": + description: Accepted + "400": + description: BadRequest + /path_with_headers: + get: + operationId: getPathWithHeaders + parameters: + - name: header + in: header + required: true + schema: + type: string + responses: + "202": + description: Accepted + "400": + description: BadRequest + put: + operationId: putPathWithHeaders + parameters: + - name: header + in: header + required: true + schema: + type: string + responses: + "202": + description: Accepted + "400": + description: BadRequest + post: + operationId: postPathWithHeaders + parameters: + - name: header + in: header + required: true + schema: + type: string + responses: + "202": + description: Accepted + "400": + description: BadRequest + delete: + operationId: deletePathWithHeaders + parameters: + - name: header + in: header + required: true + schema: + type: string + responses: + "202": + description: Accepted + "400": + description: BadRequest + head: + operationId: headPathWithHeaders + parameters: + - name: header + in: header + required: true + schema: + type: string + responses: + "202": + description: Accepted + "400": + description: BadRequest + patch: + operationId: patchPathWithHeaders + parameters: + - name: header + in: header + required: true + schema: + type: string + responses: + "202": + description: Accepted + "400": + description: BadRequest + /path_with_request_body: + put: + operationId: putPathWithRequestBody + requestBody: + content: + text/plain: + schema: + type: string + responses: + "202": + description: Accepted + "400": + description: BadRequest + post: + operationId: postPathWithRequestBody + requestBody: + content: + text/plain: + schema: + type: string + responses: + "202": + description: Accepted + "400": + description: BadRequest + delete: + operationId: deletePathWithRequestBody + requestBody: + content: + text/plain: + schema: + type: string + responses: + "202": + description: Accepted + "400": + description: BadRequest + patch: + operationId: patchPathWithRequestBody + requestBody: + content: + text/plain: + schema: + type: string + responses: + "202": + description: Accepted + "400": + description: BadRequest +components: {} diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/response/nil_return_type.bal b/openapi-cli/src/test/resources/ballerina-to-openapi/response/nil_return_type.bal new file mode 100644 index 000000000..7bc10da83 --- /dev/null +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/response/nil_return_type.bal @@ -0,0 +1,80 @@ +import ballerina/http; + +service /payloadV on new http:Listener(9090) { + resource function get path1() { + }; + resource function get path_with_query(string id) { + }; + resource function get path_with_path/[string id]() { + }; + resource function get path_with_headers(@http:Header string header) { + }; + // resource function get path_with_request_body(@http:Payload string payload) { + // }; + + resource function post path1() { + }; + resource function post path_with_query(string id) { + }; + resource function post path_with_path/[string id]() { + }; + resource function post path_with_headers(@http:Header string header) { + }; + resource function post path_with_request_body(@http:Payload string payload) { + }; + + resource function put path1() { + }; + resource function put path_with_query(string id) { + }; + resource function put path_with_path/[string id]() { + }; + resource function put path_with_headers(@http:Header string header) { + }; + resource function put path_with_request_body(@http:Payload string payload) { + }; + + resource function patch path1() { + }; + resource function patch path_with_query(string id) { + }; + resource function patch path_with_path/[string id]() { + }; + resource function patch path_with_headers(@http:Header string header) { + }; + resource function patch path_with_request_body(@http:Payload string payload) { + }; + + resource function delete path1() { + }; + resource function delete path_with_query(string id) { + }; + resource function delete path_with_path/[string id]() { + }; + resource function delete path_with_headers(@http:Header string header) { + }; + resource function delete path_with_request_body(@http:Payload string payload) { + }; + + resource function head path1() { + }; + resource function head path_with_query(string id) { + }; + resource function head path_with_path/[string id]() { + }; + resource function head path_with_headers(@http:Header string header) { + }; + // resource function head path_with_request_body(@http:Payload string payload) { + // }; + + resource function option path1() { + }; + resource function option path_with_query(string id) { + }; + resource function option path_with_path/[string id]() { + }; + resource function option path_with_headers(@http:Header string header) { + }; + resource function option path_with_request_body(@http:Payload string payload) { + }; +} From c5887d8515af256d69457f0372c74e9e92d6a640 Mon Sep 17 00:00:00 2001 From: lnash94 Date: Thu, 14 Sep 2023 15:13:30 +0530 Subject: [PATCH 2/7] Update error code details for union return type --- module-ballerina-openapi/Ballerina.toml | 6 +- module-ballerina-openapi/CompilerPlugin.toml | 4 +- .../service/OpenAPIResponseMapper.java | 23 +- .../generators/openapi/ResponseTests.java | 12 +- .../response/nil_union_return_type.yaml | 216 ++++++++++++++++++ .../response/nil_union_return_type.bal | 50 ++++ 6 files changed, 299 insertions(+), 12 deletions(-) create mode 100644 openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/response/nil_union_return_type.yaml create mode 100644 openapi-cli/src/test/resources/ballerina-to-openapi/response/nil_union_return_type.bal diff --git a/module-ballerina-openapi/Ballerina.toml b/module-ballerina-openapi/Ballerina.toml index 06051f370..49e3f3887 100644 --- a/module-ballerina-openapi/Ballerina.toml +++ b/module-ballerina-openapi/Ballerina.toml @@ -1,10 +1,10 @@ [package] org= "ballerina" name= "openapi" -version= "1.8.0" +version= "@toml.version@" [[platform.java17.dependency]] -path = "../openapi-validator/build/libs/openapi-validator-1.8.0-SNAPSHOT.jar" +path = "../openapi-validator/build/libs/openapi-validator-@project.version@.jar" groupId = "ballerina" artifactId = "openapi" -version = "1.8.0-SNAPSHOT" +version = "@project.version@" diff --git a/module-ballerina-openapi/CompilerPlugin.toml b/module-ballerina-openapi/CompilerPlugin.toml index 2d697e902..04c7cdeb4 100644 --- a/module-ballerina-openapi/CompilerPlugin.toml +++ b/module-ballerina-openapi/CompilerPlugin.toml @@ -3,7 +3,7 @@ id = "openapi-tools" class = "io.ballerina.openapi.validator.OpenAPIValidatorPlugin" [[dependency]] -path = "../openapi-validator/build/libs/openapi-validator-1.8.0-SNAPSHOT.jar" +path = "../openapi-validator/build/libs/openapi-validator-@project.version@.jar" groupId = "ballerina" artifactId = "openapi" -version = "1.8.0-SNAPSHOT" +version = "@project.version@" 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 25e2cedc9..f6dccf0ad 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 @@ -61,6 +61,7 @@ import io.ballerina.openapi.converter.utils.ConverterCommonUtils; import io.ballerina.tools.diagnostics.Location; import io.swagger.v3.oas.models.Components; +import io.swagger.v3.oas.models.Operation; import io.swagger.v3.oas.models.headers.Header; import io.swagger.v3.oas.models.media.ArraySchema; import io.swagger.v3.oas.models.media.ComposedSchema; @@ -438,7 +439,17 @@ private Optional getAPIResponses(OperationAdaptor operationAdaptor io.swagger.v3.oas.models.media.MediaType mediaType = new io.swagger.v3.oas.models.media.MediaType(); String statusCode = httpMethod.equals(POST) ? HTTP_201 : HTTP_200; String description = httpMethod.equals(POST) ? HTTP_201_DESCRIPTION : HTTP_200_DESCRIPTION; - + if (typeNode.parent().kind() == SyntaxKind.OPTIONAL_TYPE_DESC) { + ApiResponse acceptedRequestResponse = new ApiResponse(); + acceptedRequestResponse.description("Accepted"); + apiResponses.put("202", acceptedRequestResponse); + Operation operation = operationAdaptor.getOperation(); + if (operation.getRequestBody() != null || operation.getParameters() != null) { + ApiResponse badRequestResponse = new ApiResponse(); + badRequestResponse.description("BadRequest"); + apiResponses.put("400", badRequestResponse); + } + } String mediaTypeString; switch (typeNode.kind()) { case QUALIFIED_NAME_REFERENCE: @@ -795,11 +806,11 @@ private static void addResponse(ApiResponses apiResponses, Optional responses.forEach((key, value) -> { if (apiResponses.containsKey(key)) { ApiResponse res = apiResponses.get(key); - Content content = res.getContent(); - if (content == null) { - content = new Content(); - } if (value.getContent() != null) { + Content content = res.getContent(); + if (content == null) { + content = new Content(); + } String mediaType = value.getContent().keySet().iterator().next(); Schema newSchema = value.getContent().values().iterator().next().getSchema(); if (content.containsKey(mediaType)) { @@ -818,8 +829,8 @@ private static void addResponse(ApiResponses apiResponses, Optional Date: Fri, 13 Oct 2023 23:13:38 +0530 Subject: [PATCH 3/7] Update swagger test files --- .../openapi/generators/openapi/DataTypeTests.java | 2 -- .../advance/openapi/multiple_service_01.yaml | 2 ++ .../advance/openapi/multiple_service_02.yaml | 2 ++ .../advance/openapi/multiple_service_03.yaml | 2 ++ .../expected_gen/apidoc/path_param.yaml | 2 ++ .../expected_gen/apidoc/query_param.yaml | 2 ++ .../expected_gen/apidoc/record.yaml | 2 ++ .../expected_gen/apidoc/requestBody.yaml | 2 ++ .../expected_gen/apidoc/typeInclusion.yaml | 2 ++ .../expected_gen/data_type/enum.yaml | 4 ++++ .../expected_gen/data_type/enum_array_type.yaml | 2 ++ .../expected_gen/data_type/enum_with_value.yaml | 2 ++ .../expected_gen/data_type/nullable_type_def.yaml | 2 ++ .../expected_gen/data_type/tuple_type.yaml | 2 ++ .../expected_gen/data_type/type_def.yaml | 4 ++++ .../expected_gen/escape_identifier.yaml | 2 ++ .../expected_gen/json/nestedRecord.json | 3 +++ .../expected_gen/path_scenario03.yaml | 14 +++++++++++++- .../expected_gen/path_scenario04.yaml | 4 ++++ .../expected_gen/readonly.yaml | 4 ++++ .../expected_gen/record/typeInclusion.yaml | 2 ++ .../expected_gen/request_body/json_payload.yaml | 14 +++++++++++++- .../request_body/mime_with_record_payload.yaml | 13 +++++++++++++ .../request_body/multiple_payload.yaml | 13 +++++++++++++ .../expected_gen/request_body/nested_2record.yaml | 13 +++++++++++++ .../expected_gen/request_body/nested_array.yaml | 13 +++++++++++++ .../expected_gen/request_body/nested_record.yaml | 13 +++++++++++++ .../request_body/record_field_array.yaml | 13 +++++++++++++ .../expected_gen/request_body/record_payload.yaml | 13 +++++++++++++ .../expected_gen/response/cache_config_06.yaml | 2 ++ .../expected_gen/response/rs_scenario09.yaml | 6 ++++++ .../expected_gen/response/rs_scenario20.yaml | 2 ++ .../expected_gen/status_code/not_acceptable.yaml | 2 ++ 33 files changed, 176 insertions(+), 4 deletions(-) diff --git a/openapi-cli/src/test/java/io/ballerina/openapi/generators/openapi/DataTypeTests.java b/openapi-cli/src/test/java/io/ballerina/openapi/generators/openapi/DataTypeTests.java index 2adb42fab..b5d0caa4d 100644 --- a/openapi-cli/src/test/java/io/ballerina/openapi/generators/openapi/DataTypeTests.java +++ b/openapi-cli/src/test/java/io/ballerina/openapi/generators/openapi/DataTypeTests.java @@ -50,14 +50,12 @@ public void testForAllTypeDefinitions() throws IOException { @Test(description = "When the record field has type definitions with nullable") public void testForAllTypeDefinitionWithNullableValue() throws IOException { Path ballerinaFilePath = RES_DIR.resolve("data_type/nullable_type_def.bal"); - //Compare generated yaml file with expected yaml content TestUtils.compareWithGeneratedFile(ballerinaFilePath, "data_type/nullable_type_def.yaml"); } @Test(description = "test for tuple type scenarios") public void testForTupleType() throws IOException { Path ballerinaFilePath = RES_DIR.resolve("data_type/tuple_types.bal"); - //Compare generated yaml file with expected yaml content TestUtils.compareWithGeneratedFile(ballerinaFilePath, "data_type/tuple_type.yaml"); } diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/advance/openapi/multiple_service_01.yaml b/openapi-cli/src/test/resources/ballerina-to-openapi/advance/openapi/multiple_service_01.yaml index 04b0ed714..d1013a597 100644 --- a/openapi-cli/src/test/resources/ballerina-to-openapi/advance/openapi/multiple_service_01.yaml +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/advance/openapi/multiple_service_01.yaml @@ -20,4 +20,6 @@ paths: responses: "202": description: Accepted + "400": + description: BadRequest components: {} diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/advance/openapi/multiple_service_02.yaml b/openapi-cli/src/test/resources/ballerina-to-openapi/advance/openapi/multiple_service_02.yaml index 64b03b69f..42cf1986d 100644 --- a/openapi-cli/src/test/resources/ballerina-to-openapi/advance/openapi/multiple_service_02.yaml +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/advance/openapi/multiple_service_02.yaml @@ -20,4 +20,6 @@ paths: responses: "202": description: Accepted + "400": + description: BadRequest components: {} diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/advance/openapi/multiple_service_03.yaml b/openapi-cli/src/test/resources/ballerina-to-openapi/advance/openapi/multiple_service_03.yaml index 1afb02252..0246abab0 100644 --- a/openapi-cli/src/test/resources/ballerina-to-openapi/advance/openapi/multiple_service_03.yaml +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/advance/openapi/multiple_service_03.yaml @@ -20,4 +20,6 @@ paths: responses: "202": description: Accepted + "400": + description: BadRequest components: {} diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/apidoc/path_param.yaml b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/apidoc/path_param.yaml index c2e6618c2..40b0f2b03 100644 --- a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/apidoc/path_param.yaml +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/apidoc/path_param.yaml @@ -24,4 +24,6 @@ paths: responses: "202": description: Accepted + "400": + description: BadRequest components: {} diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/apidoc/query_param.yaml b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/apidoc/query_param.yaml index 7dc0c56c0..4032141cc 100644 --- a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/apidoc/query_param.yaml +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/apidoc/query_param.yaml @@ -36,4 +36,6 @@ paths: responses: "202": description: Accepted + "400": + description: BadRequest components: {} diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/apidoc/record.yaml b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/apidoc/record.yaml index c5436da83..8dfafdb44 100644 --- a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/apidoc/record.yaml +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/apidoc/record.yaml @@ -23,6 +23,8 @@ paths: responses: "202": description: Accepted + "400": + description: BadRequest components: schemas: ReserveRoom: diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/apidoc/requestBody.yaml b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/apidoc/requestBody.yaml index c5436da83..8dfafdb44 100644 --- a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/apidoc/requestBody.yaml +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/apidoc/requestBody.yaml @@ -23,6 +23,8 @@ paths: responses: "202": description: Accepted + "400": + description: BadRequest components: schemas: ReserveRoom: diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/apidoc/typeInclusion.yaml b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/apidoc/typeInclusion.yaml index 8d6c50401..a3610e89b 100644 --- a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/apidoc/typeInclusion.yaml +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/apidoc/typeInclusion.yaml @@ -21,6 +21,8 @@ paths: responses: "202": description: Accepted + "400": + description: BadRequest components: schemas: Link: diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/data_type/enum.yaml b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/data_type/enum.yaml index c08e915a1..ad068be5f 100644 --- a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/data_type/enum.yaml +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/data_type/enum.yaml @@ -23,6 +23,8 @@ paths: responses: "202": description: Accepted + "400": + description: BadRequest /getOrder: post: operationId: postGetorder @@ -34,6 +36,8 @@ paths: responses: "202": description: Accepted + "400": + description: BadRequest components: schemas: Link: diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/data_type/enum_array_type.yaml b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/data_type/enum_array_type.yaml index 7d46388bd..528d6ee1f 100644 --- a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/data_type/enum_array_type.yaml +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/data_type/enum_array_type.yaml @@ -23,6 +23,8 @@ paths: responses: "202": description: Accepted + "400": + description: BadRequest components: schemas: Link: diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/data_type/enum_with_value.yaml b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/data_type/enum_with_value.yaml index 649707787..4d3ebb060 100644 --- a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/data_type/enum_with_value.yaml +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/data_type/enum_with_value.yaml @@ -23,6 +23,8 @@ paths: responses: "202": description: Accepted + "400": + description: BadRequest components: schemas: Link: diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/data_type/nullable_type_def.yaml b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/data_type/nullable_type_def.yaml index 69b43b99d..033912d25 100644 --- a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/data_type/nullable_type_def.yaml +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/data_type/nullable_type_def.yaml @@ -23,6 +23,8 @@ paths: responses: "202": description: Accepted + "400": + description: BadRequest components: schemas: Salary: diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/data_type/tuple_type.yaml b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/data_type/tuple_type.yaml index 4c73b825e..b3d86631d 100644 --- a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/data_type/tuple_type.yaml +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/data_type/tuple_type.yaml @@ -21,6 +21,8 @@ paths: responses: "202": description: Accepted + "400": + description: BadRequest components: schemas: User: diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/data_type/type_def.yaml b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/data_type/type_def.yaml index 9a90a7b61..cc725d4d7 100644 --- a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/data_type/type_def.yaml +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/data_type/type_def.yaml @@ -23,6 +23,8 @@ paths: responses: "202": description: Accepted + "400": + description: BadRequest /reservation/{id}: get: operationId: getReservationId @@ -35,6 +37,8 @@ paths: responses: "202": description: Accepted + "400": + description: BadRequest components: schemas: PriceMap: diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/escape_identifier.yaml b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/escape_identifier.yaml index f52b498b4..c2e6addb0 100644 --- a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/escape_identifier.yaml +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/escape_identifier.yaml @@ -52,6 +52,8 @@ paths: responses: "202": description: Accepted + "400": + description: BadRequest components: schemas: Pet-Task: diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/json/nestedRecord.json b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/json/nestedRecord.json index fae643d56..ee2cce824 100644 --- a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/json/nestedRecord.json +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/json/nestedRecord.json @@ -31,6 +31,9 @@ "responses" : { "202" : { "description" : "Accepted" + }, + "400" : { + "description" : "BadRequest" } } } diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/path_scenario03.yaml b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/path_scenario03.yaml index 23a3a2e63..7a12d0318 100644 --- a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/path_scenario03.yaml +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/path_scenario03.yaml @@ -1,4 +1,14 @@ - +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: /: get: @@ -24,4 +34,6 @@ paths: responses: "202": description: Accepted + "400": + description: BadRequest components: {} diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/path_scenario04.yaml b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/path_scenario04.yaml index 987d24857..11696f1c4 100644 --- a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/path_scenario04.yaml +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/path_scenario04.yaml @@ -23,6 +23,8 @@ paths: responses: "202": description: Accepted + "400": + description: BadRequest post: operationId: post responses: @@ -46,4 +48,6 @@ paths: responses: "202": description: Accepted + "400": + description: BadRequest components: {} diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/readonly.yaml b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/readonly.yaml index 2f6b7fd4b..dd02de00b 100644 --- a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/readonly.yaml +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/readonly.yaml @@ -40,6 +40,10 @@ paths: schema: type: string responses: + "202": + description: Accepted + "400": + description: BadRequest "200": description: Ok content: diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/record/typeInclusion.yaml b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/record/typeInclusion.yaml index 5848bc863..2e0c01eff 100644 --- a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/record/typeInclusion.yaml +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/record/typeInclusion.yaml @@ -21,6 +21,8 @@ paths: responses: "202": description: Accepted + "400": + description: BadRequest components: schemas: Link: diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/request_body/json_payload.yaml b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/request_body/json_payload.yaml index 5b5361e17..0d3466549 100644 --- a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/request_body/json_payload.yaml +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/request_body/json_payload.yaml @@ -1,3 +1,14 @@ +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: /hi: post: @@ -9,5 +20,6 @@ paths: responses: "202": description: Accepted + "400": + description: BadRequest components: {} - diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/request_body/mime_with_record_payload.yaml b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/request_body/mime_with_record_payload.yaml index 655fb6b49..2b21461c7 100644 --- a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/request_body/mime_with_record_payload.yaml +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/request_body/mime_with_record_payload.yaml @@ -1,3 +1,14 @@ +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: /hi: post: @@ -10,6 +21,8 @@ paths: responses: "202": description: Accepted + "400": + description: BadRequest components: schemas: Pet: diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/request_body/multiple_payload.yaml b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/request_body/multiple_payload.yaml index 2de5acd84..b89bd81ba 100644 --- a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/request_body/multiple_payload.yaml +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/request_body/multiple_payload.yaml @@ -1,3 +1,14 @@ +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: /hi: post: @@ -11,4 +22,6 @@ paths: responses: "202": description: Accepted + "400": + description: BadRequest components: {} diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/request_body/nested_2record.yaml b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/request_body/nested_2record.yaml index 763a8edfb..8abcdab4c 100644 --- a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/request_body/nested_2record.yaml +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/request_body/nested_2record.yaml @@ -1,3 +1,14 @@ +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: /hi: post: @@ -10,6 +21,8 @@ paths: responses: "202": description: Accepted + "400": + description: BadRequest components: schemas: Tag: diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/request_body/nested_array.yaml b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/request_body/nested_array.yaml index bb686a16f..d314c6e45 100644 --- a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/request_body/nested_array.yaml +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/request_body/nested_array.yaml @@ -1,3 +1,14 @@ +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: /hi: post: @@ -10,6 +21,8 @@ paths: responses: "202": description: Accepted + "400": + description: BadRequest components: schemas: Pet: diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/request_body/nested_record.yaml b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/request_body/nested_record.yaml index beaa3bddb..80a37f845 100644 --- a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/request_body/nested_record.yaml +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/request_body/nested_record.yaml @@ -1,3 +1,14 @@ +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: /hi: post: @@ -10,6 +21,8 @@ paths: responses: "202": description: Accepted + "400": + description: BadRequest components: schemas: Dog: diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/request_body/record_field_array.yaml b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/request_body/record_field_array.yaml index af27e8b47..310e82985 100644 --- a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/request_body/record_field_array.yaml +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/request_body/record_field_array.yaml @@ -1,3 +1,14 @@ +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: /hi: post: @@ -10,6 +21,8 @@ paths: responses: "202": description: Accepted + "400": + description: BadRequest components: schemas: Dog: diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/request_body/record_payload.yaml b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/request_body/record_payload.yaml index 655fb6b49..2b21461c7 100644 --- a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/request_body/record_payload.yaml +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/request_body/record_payload.yaml @@ -1,3 +1,14 @@ +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: /hi: post: @@ -10,6 +21,8 @@ paths: responses: "202": description: Accepted + "400": + description: BadRequest components: schemas: Pet: diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/response/cache_config_06.yaml b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/response/cache_config_06.yaml index d8751ee2b..2d17c0d93 100644 --- a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/response/cache_config_06.yaml +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/response/cache_config_06.yaml @@ -58,6 +58,8 @@ paths: text/plain: schema: type: string + "202": + description: Accepted "500": description: Internal server error content: diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/response/rs_scenario09.yaml b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/response/rs_scenario09.yaml index 85d69033c..95763776e 100644 --- a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/response/rs_scenario09.yaml +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/response/rs_scenario09.yaml @@ -28,6 +28,8 @@ paths: responses: "202": description: Accepted + "400": + description: BadRequest put: operationId: putHi responses: @@ -59,6 +61,10 @@ paths: type: integer format: int64 responses: + "202": + description: Accepted + "400": + description: BadRequest "500": description: Internal server error content: diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/response/rs_scenario20.yaml b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/response/rs_scenario20.yaml index 6df8c5ba0..3f1b4f773 100644 --- a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/response/rs_scenario20.yaml +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/response/rs_scenario20.yaml @@ -20,6 +20,8 @@ paths: '*/*': schema: description: Any type of entity body + "202": + description: Accepted "500": description: Internal server error content: diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/status_code/not_acceptable.yaml b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/status_code/not_acceptable.yaml index ccf554b4b..7b4a878fd 100644 --- a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/status_code/not_acceptable.yaml +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/status_code/not_acceptable.yaml @@ -16,6 +16,8 @@ paths: responses: "406": description: NotAcceptable + "202": + description: Accepted "500": description: Internal server error content: From ca37508537949b84f8a95def98fafb1acaa9e863 Mon Sep 17 00:00:00 2001 From: lnash94 Date: Sat, 14 Oct 2023 07:56:17 +0530 Subject: [PATCH 4/7] Update swagger test files --- .../expected_gen/escape_identifier_02.yaml | 4 ++++ .../expected_gen/request_body/union_type.yaml | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/escape_identifier_02.yaml b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/escape_identifier_02.yaml index 464e8e273..f8ac76f1b 100644 --- a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/escape_identifier_02.yaml +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/escape_identifier_02.yaml @@ -97,6 +97,8 @@ paths: responses: "202": description: Accepted + "400": + description: BadRequest put: operationId: putพิมพ์ชื่อ parameters: @@ -108,6 +110,8 @@ paths: responses: "202": description: Accepted + "400": + description: BadRequest components: schemas: ErrorPayload: diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/request_body/union_type.yaml b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/request_body/union_type.yaml index 72172bab0..c425d1324 100644 --- a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/request_body/union_type.yaml +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/request_body/union_type.yaml @@ -21,6 +21,8 @@ paths: responses: "202": description: Accepted + "400": + description: BadRequest /path1: post: operationId: postPath1 @@ -40,6 +42,8 @@ paths: responses: "202": description: Accepted + "400": + description: BadRequest /path3: post: operationId: postPath3 @@ -55,6 +59,8 @@ paths: responses: "202": description: Accepted + "400": + description: BadRequest /path4: post: operationId: postPath4 @@ -71,6 +77,8 @@ paths: responses: "202": description: Accepted + "400": + description: BadRequest /path5: post: operationId: postPath5 @@ -85,6 +93,8 @@ paths: responses: "202": description: Accepted + "400": + description: BadRequest /path6: post: operationId: postPath6 @@ -100,6 +110,8 @@ paths: responses: "202": description: Accepted + "400": + description: BadRequest /path7: post: operationId: postPath7 @@ -117,6 +129,8 @@ paths: responses: "202": description: Accepted + "400": + description: BadRequest /path8: post: operationId: postPath8 @@ -134,6 +148,8 @@ paths: responses: "202": description: Accepted + "400": + description: BadRequest /path9: post: operationId: postPath9 @@ -152,6 +168,8 @@ paths: responses: "202": description: Accepted + "400": + description: BadRequest components: schemas: ABC: From 0847854045f903dbe2ad6814dace35a4414f4ff1 Mon Sep 17 00:00:00 2001 From: lnash94 Date: Sun, 15 Oct 2023 21:16:42 +0530 Subject: [PATCH 5/7] Add negative tests scenarios. --- .../openapi/converter/Constants.java | 5 +- .../service/OpenAPIResponseMapper.java | 20 +++--- .../openapi/NegativeResponseTests.java | 66 +++++++++++++++++++ .../response/nil_union_return_type.yaml | 21 ++++-- .../response/negative/nil_return_type.bal | 11 ++++ .../response/nil_return_type.bal | 4 -- .../response/nil_union_return_type.bal | 15 +++-- openapi-cli/src/test/resources/testng.xml | 1 + 8 files changed, 117 insertions(+), 26 deletions(-) create mode 100644 openapi-cli/src/test/java/io/ballerina/openapi/generators/openapi/NegativeResponseTests.java create mode 100644 openapi-cli/src/test/resources/ballerina-to-openapi/response/negative/nil_return_type.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 310ccc379..d1a2a97c7 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 @@ -238,5 +238,8 @@ public String toString() { public static final String YML_EXTENSION = ".yml"; public static final String PLUS = "+"; public static final String UNDERSCORE = "_"; - + public static final String HTTP_202 = "202"; + public static final String HTTP_400 = "400"; + public static final String ACCEPTED = "Accepted"; + public static final String BAD_REQUEST = "BadRequest"; } 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 f6dccf0ad..c10a14045 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 @@ -93,7 +93,9 @@ import static io.ballerina.compiler.syntax.tree.SyntaxKind.RECORD_FIELD; import static io.ballerina.compiler.syntax.tree.SyntaxKind.SIMPLE_NAME_REFERENCE; import static io.ballerina.compiler.syntax.tree.SyntaxKind.TYPE_REFERENCE; +import static io.ballerina.openapi.converter.Constants.ACCEPTED; import static io.ballerina.openapi.converter.Constants.APPLICATION_PREFIX; +import static io.ballerina.openapi.converter.Constants.BAD_REQUEST; 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; @@ -106,7 +108,9 @@ import static io.ballerina.openapi.converter.Constants.HTTP_200_DESCRIPTION; import static io.ballerina.openapi.converter.Constants.HTTP_201; import static io.ballerina.openapi.converter.Constants.HTTP_201_DESCRIPTION; +import static io.ballerina.openapi.converter.Constants.HTTP_202; import static io.ballerina.openapi.converter.Constants.HTTP_204; +import static io.ballerina.openapi.converter.Constants.HTTP_400; import static io.ballerina.openapi.converter.Constants.HTTP_500; import static io.ballerina.openapi.converter.Constants.HTTP_500_DESCRIPTION; import static io.ballerina.openapi.converter.Constants.HTTP_CODES; @@ -198,12 +202,12 @@ public void getResourceOutput(FunctionDefinitionNode resource, OperationAdaptor } else { // When the return type is not mention in the resource function. ApiResponse apiResponse = new ApiResponse(); - apiResponse.description("Accepted"); - apiResponses.put("202", apiResponse); + apiResponse.description(ACCEPTED); + apiResponses.put(HTTP_202, apiResponse); if (operation.getRequestBody() != null || operation.getParameters() != null) { ApiResponse badRequestResponse = new ApiResponse(); - badRequestResponse.description("BadRequest"); - apiResponses.put("400", badRequestResponse); + badRequestResponse.description(BAD_REQUEST); + apiResponses.put(HTTP_400, badRequestResponse); } } operation.setResponses(apiResponses); @@ -441,13 +445,13 @@ private Optional getAPIResponses(OperationAdaptor operationAdaptor String description = httpMethod.equals(POST) ? HTTP_201_DESCRIPTION : HTTP_200_DESCRIPTION; if (typeNode.parent().kind() == SyntaxKind.OPTIONAL_TYPE_DESC) { ApiResponse acceptedRequestResponse = new ApiResponse(); - acceptedRequestResponse.description("Accepted"); - apiResponses.put("202", acceptedRequestResponse); + acceptedRequestResponse.description(ACCEPTED); + apiResponses.put(HTTP_202, acceptedRequestResponse); Operation operation = operationAdaptor.getOperation(); if (operation.getRequestBody() != null || operation.getParameters() != null) { ApiResponse badRequestResponse = new ApiResponse(); - badRequestResponse.description("BadRequest"); - apiResponses.put("400", badRequestResponse); + badRequestResponse.description(BAD_REQUEST); + apiResponses.put(HTTP_400, badRequestResponse); } } String mediaTypeString; diff --git a/openapi-cli/src/test/java/io/ballerina/openapi/generators/openapi/NegativeResponseTests.java b/openapi-cli/src/test/java/io/ballerina/openapi/generators/openapi/NegativeResponseTests.java new file mode 100644 index 000000000..ceea47c6b --- /dev/null +++ b/openapi-cli/src/test/java/io/ballerina/openapi/generators/openapi/NegativeResponseTests.java @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.org). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package io.ballerina.openapi.generators.openapi; + +import io.ballerina.openapi.cmd.OASContractGenerator; +import org.testng.Assert; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.AfterTest; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +/** + * This test class is covering the negative unit tests for return type scenarios. + */ +public class NegativeResponseTests { + private static final Path RES_DIR = Paths.get("src/test/resources/ballerina-to-openapi").toAbsolutePath(); + private Path tempDir; + + @BeforeMethod + public void setup() throws IOException { + this.tempDir = Files.createTempDirectory("bal-to-openapi-test-out-" + System.nanoTime()); + } + + @Test(description = "When the resource has nil type return") + public void testNilReturnType() { + Path ballerinaFilePath = RES_DIR.resolve("response/negative/nil_return_type.bal"); + OASContractGenerator openApiConverterUtils = new OASContractGenerator(); + openApiConverterUtils.generateOAS3DefinitionsAllService(ballerinaFilePath, this.tempDir, null + , false); + //TODO: Uncomment after merging this PR https://github.com/ballerina-platform/openapi-tools/pull/1370. +// Assert.assertFalse(openApiConverterUtils.getErrors().isEmpty()); + Assert.assertFalse(Files.exists(tempDir.resolve("payloadV_openapi.yaml"))); + } + + + @AfterMethod + public void cleanUp() { + TestUtils.deleteDirectory(this.tempDir); + } + + @AfterTest + public void clean() { + System.setErr(null); + System.setOut(null); + } +} diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/response/nil_union_return_type.yaml b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/response/nil_union_return_type.yaml index 7f0586d4d..73850c720 100644 --- a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/response/nil_union_return_type.yaml +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/response/nil_union_return_type.yaml @@ -12,7 +12,7 @@ servers: paths: /path_get_error: get: - summary: for `get` method with nil return type and no error status code + summary: for `get` method with nil return type and error operationId: getPathGetError responses: "202": @@ -25,7 +25,7 @@ paths: $ref: '#/components/schemas/ErrorPayload' /path_get: get: - summary: for `get` method with nil return type and no error status code + summary: for `get` method with nil return type and success status code operationId: getPathGet responses: "202": @@ -38,7 +38,8 @@ paths: type: string /path_get_query_param: get: - summary: for `get` method with nil return type and no error status code + summary: for `get` method with nil return type and success status code with + query param operationId: getPathGetQueryParam parameters: - name: id @@ -59,7 +60,7 @@ paths: type: string /path_post: post: - summary: for `post` method with 201 payload with optional error status code + summary: for `post` method with 201 payload with error status code and nil type operationId: postPathPost responses: "201": @@ -74,7 +75,7 @@ paths: description: NotFound /path: get: - summary: for union return type with error status code + summary: for union return type with error status code and nil type operationId: getPath responses: "200": @@ -88,8 +89,8 @@ paths: "404": description: NotFound post: - summary: "with same error status code (202): by default for ? and explicitly\ - \ mentioned as return type" + summary: "resource has 3 status codes that explicitly returns (202, 404, 400)\ + \ while ? type returns implicitly 400, 202" operationId: postPath parameters: - name: id @@ -110,6 +111,8 @@ paths: type: string /path_with_query: get: + summary: "resource has 4 status codes that explicitly returns (200, 404) while\ + \ ? type returns implicitly 400, 202" operationId: getPathWithQuery parameters: - name: id @@ -132,6 +135,8 @@ paths: description: NotFound /path_with_path/{id}: post: + summary: "resource has 3 status codes that explicitly returns (404) while ?\ + \ type returns implicitly 400, 202" operationId: postPathWithPathId parameters: - name: id @@ -148,6 +153,8 @@ paths: description: NotFound /path_with_headers: post: + summary: "method has 3 status codes that explicitly returns (404) while ? type\ + \ returns implicitly 400, 202" operationId: postPathWithHeaders parameters: - name: header diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/response/negative/nil_return_type.bal b/openapi-cli/src/test/resources/ballerina-to-openapi/response/negative/nil_return_type.bal new file mode 100644 index 000000000..50cd00198 --- /dev/null +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/response/negative/nil_return_type.bal @@ -0,0 +1,11 @@ +import ballerina/http; + +service /payloadV on new http:Listener(9090) { + + resource function get path_with_request_body(@http:Payload string payload) { + }; + + resource function head path_with_request_body(@http:Payload string payload) { + }; + +} diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/response/nil_return_type.bal b/openapi-cli/src/test/resources/ballerina-to-openapi/response/nil_return_type.bal index 7bc10da83..eb9a1bf03 100644 --- a/openapi-cli/src/test/resources/ballerina-to-openapi/response/nil_return_type.bal +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/response/nil_return_type.bal @@ -9,8 +9,6 @@ service /payloadV on new http:Listener(9090) { }; resource function get path_with_headers(@http:Header string header) { }; - // resource function get path_with_request_body(@http:Payload string payload) { - // }; resource function post path1() { }; @@ -64,8 +62,6 @@ service /payloadV on new http:Listener(9090) { }; resource function head path_with_headers(@http:Header string header) { }; - // resource function head path_with_request_body(@http:Payload string payload) { - // }; resource function option path1() { }; diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/response/nil_union_return_type.bal b/openapi-cli/src/test/resources/ballerina-to-openapi/response/nil_union_return_type.bal index 6b1e06078..e45f14226 100644 --- a/openapi-cli/src/test/resources/ballerina-to-openapi/response/nil_union_return_type.bal +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/response/nil_union_return_type.bal @@ -12,36 +12,39 @@ type Links record {| service /payloadV on new http:Listener(9090) { - # for `get` method with nil return type and no error status code + # for `get` method with nil return type and error resource function get path_get_error() returns error? { }; - # for `get` method with nil return type and no error status code + # for `get` method with nil return type and success status code resource function get path_get() returns string? { }; - # for `get` method with nil return type and no error status code + # for `get` method with nil return type and success status code with query param resource function get path_get_query_param(string id) returns string? { }; - # for `post` method with 201 payload with optional error status code + # for `post` method with 201 payload with error status code and nil type resource function post path_post() returns Links|http:NotFound? { }; - # for union return type with error status code + # for union return type with error status code and nil type resource function get path() returns string|http:NotFound? { }; - # with same error status code (202): by default for ? and explicitly mentioned as return type + # resource has 3 status codes that explicitly returns (202, 404, 400) while ? type returns implicitly 400, 202 resource function post path(string id) returns http:Accepted|http:NotFound|BadRequestRecord? { }; + # resource has 4 status codes that explicitly returns (200, 404) while ? type returns implicitly 400, 202 resource function get path_with_query(string id) returns string|http:NotFound? { }; + # resource has 3 status codes that explicitly returns (404) while ? type returns implicitly 400, 202 resource function post path_with_path/[string id]() returns http:NotFound? { }; + # method has 3 status codes that explicitly returns (404) while ? type returns implicitly 400, 202 resource function post path_with_headers(@http:Header string header) returns http:NotFound? { }; diff --git a/openapi-cli/src/test/resources/testng.xml b/openapi-cli/src/test/resources/testng.xml index 8c4eaeafb..48633fc0b 100644 --- a/openapi-cli/src/test/resources/testng.xml +++ b/openapi-cli/src/test/resources/testng.xml @@ -104,6 +104,7 @@ under the License. + From 2a8fed47060cd959236db61d50efe2998dc0f195 Mon Sep 17 00:00:00 2001 From: TharmiganK Date: Tue, 17 Oct 2023 10:58:16 +0530 Subject: [PATCH 6/7] Add support for array payload from external module --- .../service/OpenAPIRequestBodyMapper.java | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/openapi-bal-service/src/main/java/io/ballerina/openapi/converter/service/OpenAPIRequestBodyMapper.java b/openapi-bal-service/src/main/java/io/ballerina/openapi/converter/service/OpenAPIRequestBodyMapper.java index 9308f66e3..8a278cbe3 100644 --- a/openapi-bal-service/src/main/java/io/ballerina/openapi/converter/service/OpenAPIRequestBodyMapper.java +++ b/openapi-bal-service/src/main/java/io/ballerina/openapi/converter/service/OpenAPIRequestBodyMapper.java @@ -283,8 +283,8 @@ private String getMediaTypeForSyntaxKind(TypeDescriptorNode payloadNode) { return customMediaPrefix == null ? MediaType.APPLICATION_OCTET_STREAM : APPLICATION_PREFIX + customMediaPrefix + OCTECT_STREAM_POSTFIX; case SIMPLE_NAME_REFERENCE: - SimpleNameReferenceNode record = (SimpleNameReferenceNode) payloadNode; - TypeSymbol typeSymbol = getReferenceTypeSymbol(semanticModel.symbol(record)); + case QUALIFIED_NAME_REFERENCE: + TypeSymbol typeSymbol = getReferenceTypeSymbol(semanticModel.symbol(payloadNode)); if (typeSymbol instanceof TypeReferenceTypeSymbol) { typeSymbol = ((TypeReferenceTypeSymbol) typeSymbol).typeDescriptor(); } @@ -346,16 +346,21 @@ private void handleArrayTypePayload(Map schema, ArrayTypeDescrip TypeDescriptorNode typeDescriptorNode = arrayNode.memberTypeDesc(); // Nested array not allowed io.swagger.v3.oas.models.media.MediaType media = new io.swagger.v3.oas.models.media.MediaType(); - if (typeDescriptorNode.kind().equals(SyntaxKind.SIMPLE_NAME_REFERENCE)) { + if (typeDescriptorNode.kind().equals(SyntaxKind.SIMPLE_NAME_REFERENCE) || + typeDescriptorNode.kind().equals(SyntaxKind.QUALIFIED_NAME_REFERENCE)) { //handle record for components - SimpleNameReferenceNode referenceNode = (SimpleNameReferenceNode) typeDescriptorNode; - TypeSymbol typeSymbol = getReferenceTypeSymbol(semanticModel.symbol(referenceNode)); + TypeSymbol typeSymbol = getReferenceTypeSymbol(semanticModel.symbol(typeDescriptorNode)); OpenAPIComponentMapper componentMapper = new OpenAPIComponentMapper(components); componentMapper.createComponentSchema(schema, typeSymbol); diagnostics.addAll(componentMapper.getDiagnostics()); Schema itemSchema = new Schema(); - arraySchema.setItems(itemSchema.$ref(ConverterCommonUtils.unescapeIdentifier( - referenceNode.name().text().trim()))); + String referenceName; + if (typeDescriptorNode.kind().equals(SyntaxKind.SIMPLE_NAME_REFERENCE)) { + referenceName = ((SimpleNameReferenceNode) typeDescriptorNode).name().toString().trim(); + } else { + referenceName = ((QualifiedNameReferenceNode) typeDescriptorNode).identifier().text(); + } + arraySchema.setItems(itemSchema.$ref(ConverterCommonUtils.unescapeIdentifier(referenceName))); media.setSchema(arraySchema); } else if (typeDescriptorNode.kind() == SyntaxKind.BYTE_TYPE_DESC) { StringSchema byteSchema = new StringSchema(); From 81a4e3a8c11f2171414313ac753e8ee18ed933de Mon Sep 17 00:00:00 2001 From: TharmiganK Date: Tue, 17 Oct 2023 10:58:36 +0530 Subject: [PATCH 7/7] Add test case for array payload from external module --- .../service/snowpeak_request_body.bal | 9 ++++++ .../snowpeak_request_body_ref.yaml | 32 +++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/ballerina-project/service/snowpeak_request_body.bal b/openapi-cli/src/test/resources/ballerina-to-openapi/ballerina-project/service/snowpeak_request_body.bal index 63440d66d..30c48aea6 100644 --- a/openapi-cli/src/test/resources/ballerina-to-openapi/ballerina-project/service/snowpeak_request_body.bal +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/ballerina-project/service/snowpeak_request_body.bal @@ -31,6 +31,15 @@ service /payloadV on new http:Listener(9090) { return created; } + # Represents Snowpeak reservations resource + # + # + reservations - Array representation of Reservations + # + return - `ReservationCreated` or ReservationConflict representation + resource function post reservations(@http:Payload rep:Reservation[] reservations) + returns @http:Cache{} http:Created|rep:ReservationConflict { + return http:CREATED; + } + # Represents Snowpeak payment resource # # + id - Unique identification of payment diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/snowpeak_request_body_ref.yaml b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/snowpeak_request_body_ref.yaml index 22fdfebfd..ad6cfdf69 100644 --- a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/snowpeak_request_body_ref.yaml +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/snowpeak_request_body_ref.yaml @@ -44,6 +44,38 @@ paths: text/plain: schema: type: string + /reservations: + post: + summary: Represents Snowpeak reservations resource + operationId: postReservations + requestBody: + description: Array representation of Reservations + content: + application/json: + schema: + type: array + items: + $ref: '#/components/schemas/Reservation' + responses: + "201": + description: Created + headers: + Cache-Control: + schema: + type: string + default: "must-revalidate,public,max-age=3600" + ETag: + schema: + type: string + Last-Modified: + schema: + type: string + "409": + description: Conflict + content: + text/plain: + schema: + type: string /payment/{id}: post: summary: Represents Snowpeak payment resource