Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[master] Fix bug when return type is byte[] in OpenAPI spec generation #1469

Merged
merged 3 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -621,6 +623,7 @@ private Optional<ApiResponses> 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;
aneeshafedo marked this conversation as resolved.
Show resolved Hide resolved
Schema<?> openApiSchema = ConverterCommonUtils.getOpenApiSchema(type02);
Optional<String> mimeType = convertBallerinaMIMEToOASMIMETypes(type02, customMediaPrefix);
if (mimeType.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
@@ -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
NipunaRanasinghe marked this conversation as resolved.
Show resolved Hide resolved
"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
Original file line number Diff line number Diff line change
@@ -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("");
}
}
Loading