From 80043d53db76cd22dfc4e1edfb59c06ee57c785d Mon Sep 17 00:00:00 2001 From: TharmiganK Date: Thu, 8 Feb 2024 16:01:09 +0530 Subject: [PATCH] Add additional test case --- .../generators/openapi/ResponseTests.java | 10 + .../response/status_code_errors_03.yaml | 173 ++++++++++++++++++ .../response/status_code_errors_03.bal | 65 +++++++ 3 files changed, 248 insertions(+) create mode 100644 openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/response/status_code_errors_03.yaml create mode 100644 openapi-cli/src/test/resources/ballerina-to-openapi/response/status_code_errors_03.bal 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 70b9789fe..6759a81ac 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 @@ -413,6 +413,16 @@ public void testHttpStatusCodeErrorReturnType2() throws IOException { compareWithGeneratedFile(ballerinaFilePath, "response/status_code_errors_02.yaml"); } + @Test(description = "When the resource has http status code error return with different detail types") + public void testHttpStatusCodeErrorReturnType3() throws IOException { + Path ballerinaFilePath = RES_DIR.resolve("response/status_code_errors_03.bal"); + OASContractGenerator openApiConverterUtils = new OASContractGenerator(); + openApiConverterUtils.generateOAS3DefinitionsAllService(ballerinaFilePath, this.tempDir, null + , false); + Assert.assertTrue(openApiConverterUtils.getErrors().isEmpty()); + compareWithGeneratedFile(ballerinaFilePath, "response/status_code_errors_03.yaml"); + } + @AfterMethod public void cleanUp() { TestUtils.deleteDirectory(this.tempDir); diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/response/status_code_errors_03.yaml b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/response/status_code_errors_03.yaml new file mode 100644 index 000000000..e5dc5ace8 --- /dev/null +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/expected_gen/response/status_code_errors_03.yaml @@ -0,0 +1,173 @@ +openapi: 3.0.1 +info: + title: PayloadV + version: 0.0.0 +servers: + - url: "{server}:{port}/payloadV" + variables: + server: + default: http://localhost + port: + default: "9000" +paths: + /users/{id}: + get: + operationId: getUsersId + parameters: + - name: id + in: path + required: true + schema: + type: integer + format: int64 + responses: + "200": + description: Ok + content: + application/json: + schema: + $ref: '#/components/schemas/User' + "202": + description: Accepted + "404": + description: NotFound + content: + application/json: + schema: + $ref: '#/components/schemas/UserApiErrorInfo' + "400": + description: BadRequest + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorPayload' + /users: + post: + operationId: postUsers + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/UserWithoutId' + required: true + responses: + "201": + description: Created + content: + application/json: + schema: + $ref: '#/components/schemas/User' + "400": + description: BadRequest + content: + application/json: + schema: + oneOf: + - $ref: '#/components/schemas/UserApiErrorInfo' + - $ref: '#/components/schemas/ErrorPayload' + "202": + description: Accepted + "409": + description: Conflict + content: + application/json: + schema: + $ref: '#/components/schemas/UserApiErrorInfo' + /test1: + get: + operationId: getTest1 + responses: + default: + description: Any Response + content: + '*/*': + schema: + description: Any type of entity body + "202": + description: Accepted + /test2: + get: + operationId: getTest2 + responses: + default: + description: Any Response + content: + '*/*': + schema: + description: Any type of entity body + application/json: + schema: + $ref: '#/components/schemas/DefaultErrorInfo' + "202": + description: Accepted +components: + schemas: + DefaultErrorInfo: + required: + - message + - timeStamp + type: object + properties: + timeStamp: + type: string + message: + type: string + additionalProperties: false + ErrorPayload: + required: + - message + - method + - path + - reason + - status + - timestamp + type: object + properties: + timestamp: + type: string + status: + type: integer + format: int64 + reason: + type: string + message: + type: string + path: + type: string + method: + type: string + User: + type: object + allOf: + - $ref: '#/components/schemas/UserWithoutId' + - required: + - id + type: object + properties: + id: + type: integer + format: int64 + additionalProperties: false + UserApiErrorInfo: + type: object + allOf: + - $ref: '#/components/schemas/DefaultErrorInfo' + - required: + - userId + type: object + properties: + userId: + type: string + additionalProperties: false + UserWithoutId: + required: + - age + - name + type: object + properties: + name: + type: string + age: + type: integer + format: int64 + additionalProperties: false diff --git a/openapi-cli/src/test/resources/ballerina-to-openapi/response/status_code_errors_03.bal b/openapi-cli/src/test/resources/ballerina-to-openapi/response/status_code_errors_03.bal new file mode 100644 index 000000000..0aaadf594 --- /dev/null +++ b/openapi-cli/src/test/resources/ballerina-to-openapi/response/status_code_errors_03.bal @@ -0,0 +1,65 @@ +import ballerina/http; +import ballerina/http.httpscerr; + +type Error distinct error; + +type DefaultErrorInfo record {| + string timeStamp; + string message; +|}; + +type DefaultErrorDetails record {| + *httpscerr:ErrorDetail; + DefaultErrorInfo body; +|}; + +type UserApiErrorInfo record {| + *DefaultErrorInfo; + string userId; +|}; + +type UserApiErrorDetails record {| + *httpscerr:ErrorDetail; + UserApiErrorInfo body; +|}; + +type UserNotFoundError Error & httpscerr:NotFoundError & error; + +type UserNameAlreadyExistError Error & httpscerr:ConflictError & error; + +type BadUserError Error & httpscerr:BadRequestError & error; + +type DefaultError Error & httpscerr:DefaultStatusCodeError & error; + +type User record {| + readonly int id; + *UserWithoutId; +|}; + +type UserWithoutId record {| + string name; + int age; +|}; + +service /payloadV on new http:Listener(9000) { + + resource function get users/[int id]() returns User|UserNotFoundError? { + return; + } + + resource function post users(@http:Payload readonly & UserWithoutId user) + returns User|UserNameAlreadyExistError|BadUserError? { + return; + } + + resource function get test1() returns http:Response|httpscerr:DefaultStatusCodeError? { + return; + } + + resource function get test2() returns http:Response|DefaultError? { + return; + } + + resource function 'default [string... path]() returns httpscerr:NotFoundError? { + } +}