Skip to content

Commit

Permalink
Add additional test case
Browse files Browse the repository at this point in the history
  • Loading branch information
TharmiganK committed Feb 8, 2024
1 parent 43557d1 commit 80043d5
Show file tree
Hide file tree
Showing 3 changed files with 248 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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<UserApiErrorDetails>;

type UserNameAlreadyExistError Error & httpscerr:ConflictError & error<UserApiErrorDetails>;

type BadUserError Error & httpscerr:BadRequestError & error<UserApiErrorDetails>;

type DefaultError Error & httpscerr:DefaultStatusCodeError & error<DefaultErrorDetails>;

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? {
}
}

0 comments on commit 80043d5

Please sign in to comment.