Skip to content

Commit

Permalink
Add missing response mapper tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TharmiganK committed Jan 24, 2024
1 parent aa1b4a0 commit 86678fa
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import io.ballerina.openapi.service.mapper.utils.MediaTypeUtils;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.headers.Header;
import io.swagger.v3.oas.models.media.ComposedSchema;
import io.swagger.v3.oas.models.media.Content;
import io.swagger.v3.oas.models.media.MediaType;
import io.swagger.v3.oas.models.media.Schema;
Expand Down Expand Up @@ -221,38 +220,18 @@ public void addApiResponse(ApiResponse apiResponse, String statusCode) {
}

private void updateExistingApiResponse(ApiResponse apiResponse, String statusCode) {
ApiResponse res = apiResponses.get(statusCode);
if (apiResponse.getContent() != null) {
Content content = res.getContent();
if (content == null) {
content = new Content();
}

updateApiResponseContentWithMediaType(apiResponse, content);
res.setContent(content);
}
apiResponses.put(statusCode, res);
}

private static void updateApiResponseContentWithMediaType(ApiResponse apiResponse, Content content) {
String mediaType = apiResponse.getContent().keySet().iterator().next();
Schema newSchema = apiResponse.getContent().values().iterator().next().getSchema();
if (content.containsKey(mediaType)) {
Schema<?> schema = content.get(mediaType).getSchema();
if (schema instanceof ComposedSchema && schema.getOneOf() != null) {
schema.getOneOf().add(newSchema);
content.put(mediaType, new MediaType().schema(schema));
ApiResponse existingRes = apiResponses.get(statusCode);
Content newContent = apiResponse.getContent();
if (Objects.nonNull(newContent)) {
Content content = existingRes.getContent();
if (Objects.isNull(content)) {
content = newContent;
} else {
ComposedSchema composedSchema = new ComposedSchema();
composedSchema.addOneOfItem(schema);
composedSchema.addOneOfItem(newSchema);
MediaType updatedMediaContent =
new MediaType().schema(composedSchema);
content.put(mediaType, updatedMediaContent);
content.put(newContent.keySet().iterator().next(), newContent.values().iterator().next());
}
} else {
content.put(mediaType, apiResponse.getContent().values().iterator().next());
existingRes.setContent(content);
}
apiResponses.put(statusCode, existingRes);
}

private void updateApiResponseContentWithMediaType(ApiResponse apiResponse, String mediaType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,78 @@ paths:
text/plain:
schema:
type: string
/hello:
get:
operationId: getHello
parameters:
- name: name
in: query
required: true
schema:
$ref: '#/components/schemas/String'
responses:
"200":
description: Ok
content:
text/plain:
schema:
$ref: '#/components/schemas/String'
"400":
description: BadRequest
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorPayload'
/howdy:
get:
operationId: getHowdy
parameters:
- name: name
in: query
required: true
schema:
oneOf:
- $ref: '#/components/schemas/String'
- type: string
responses:
"200":
description: Ok
content:
text/plain:
schema:
oneOf:
- $ref: '#/components/schemas/String'
- type: string
"400":
description: BadRequest
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorPayload'
components:
schemas:
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
String:
type: string
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,18 @@ import ballerina/http;

listener http:Listener helloEp = new (9090);

type String string;

service /payloadV on helloEp {
resource function post hi() returns string {
return "ok";
}

resource function get hello(String name) returns String {
return "Hello, " + name;
}

resource function get howdy(String|string name) returns String|string {
return "Hello, " + name;
}
}

0 comments on commit 86678fa

Please sign in to comment.