Skip to content

Commit

Permalink
Refactor the function code
Browse files Browse the repository at this point in the history
  • Loading branch information
lnash94 committed Nov 8, 2023
1 parent d5d64e7 commit ed1ce59
Show file tree
Hide file tree
Showing 29 changed files with 254 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,20 @@ public Object[][] dataProviderForFunctionBody() {
"http:Responseresponse=check self.clientEp-> head(resourcePath, httpHeaders);returnresponse;}"},
{"diagnostic_files/operation_delete.yaml", "/pets/{petId}", "{string resourcePath = " +
"string `/pets/${getEncodedUri(petId)}`;" +
"returncheck self.clientEp-> delete(resourcePath);}"},
"return self.clientEp-> delete(resourcePath);}"},
{"diagnostic_files/json_payload.yaml", "/pets", "{string resourcePath = string `/pets`;" +
"http:Request request = new; request.setPayload(payload, \"application/json\"); " +
"returncheck self.clientEp->post(resourcePath, request);}"},
"return self.clientEp->post(resourcePath, request);}"},
{"diagnostic_files/xml_payload.yaml", "/pets", "{string resourcePath = string `/pets`; " +
"http:Request request = new;" +
"request.setPayload(payload, \"application/xml\"); " +
"return check self.clientEp->post(resourcePath, request);}"},
"return self.clientEp->post(resourcePath, request);}"},
{"diagnostic_files/xml_payload_with_ref.yaml", "/pets", "{string resourcePath = string `/pets`;" +
"http:Request request = new;" +
"json jsonBody = payload.toJson();" +
"xml? xmlBody = check xmldata:fromJson(jsonBody);" +
"request.setPayload(xmlBody, \"application/xml\");" +
"returncheck self.clientEp->post(resourcePath, request);}"},
"return self.clientEp->post(resourcePath, request);}"},
{"client/swagger/response_type_order.yaml", "/pet/{petId}",
"{string resourcePath = string `/pet/${getEncodedUri(petId)}`;" +
"Pet response = check self.clientEp->get(resourcePath);" +
Expand All @@ -115,11 +115,11 @@ public Object[][] dataProviderForFunctionBody() {
"return response;}"},
{"client/swagger/pdf_payload.yaml", "/pets", "{string resourcePath = string `/pets`;" +
"// TODO: Update the request as needed;\n" +
"returncheck self.clientEp->post(resourcePath, request);}"},
" return self.clientEp->post(resourcePath, request);}"},
{"client/swagger/image_payload.yaml", "/pets", "{string resourcePath = string `/pets`;" +
"http:Request request = new;" +
"request.setPayload(payload, \"image/png\");" +
"returncheck self.clientEp->post(resourcePath, request);}"},
" return self.clientEp->post(resourcePath, request);}"},
{"client/swagger/multipart_formdata_custom.yaml", "/pets", "{string resourcePath = string `/pets`;\n" +
"http:Request request = new;\n" +
"map<Encoding> encodingMap = {\"profileImage\": {contentType: \"image/png\", headers: " +
Expand All @@ -128,7 +128,7 @@ public Object[][] dataProviderForFunctionBody() {
"{contentType:\"text/plain\"}};\n" +
"mime:Entity[] bodyParts = check createBodyParts(payload, encodingMap);\n" +
"request.setBodyParts(bodyParts);\n" +
"returncheck self.clientEp->post(resourcePath, request);\n}"},
" return self.clientEp->post(resourcePath, request);\n}"},
{"client/swagger/empty_object_responnse.yaml", "/pets", "{string resourcePath = string `/pets`;\n" +
" // TODO: Update the request as needed;\n" +
" json response = check self.clientEp->post(resourcePath, request);\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@

package io.ballerina.openapi.generators.client;

import io.ballerina.compiler.syntax.tree.SyntaxTree;
import io.ballerina.openapi.core.exception.BallerinaOpenApiException;
import io.ballerina.openapi.core.generators.client.BallerinaClientGenerator;
import io.ballerina.openapi.core.generators.client.FunctionReturnTypeGenerator;
import io.ballerina.openapi.core.generators.client.model.OASClientConfig;
import io.ballerina.openapi.core.generators.schema.BallerinaTypesGenerator;
import io.ballerina.openapi.core.model.Filter;
import io.swagger.v3.oas.models.OpenAPI;
import org.testng.Assert;
import org.testng.annotations.Test;
Expand All @@ -29,7 +33,9 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import static io.ballerina.openapi.generators.common.TestUtils.compareGeneratedSyntaxTreeWithExpectedSyntaxTree;
import static io.ballerina.openapi.generators.common.TestUtils.getOpenAPI;

/**
Expand Down Expand Up @@ -132,4 +138,21 @@ public void getReturnTypeForAdditionalPropertySchema() throws IOException, Balle
true);
Assert.assertEquals(returnType, "json|error");
}

@Test(description = "Tests for the response without content type")
public void getReturnTypeForNoContentType() throws IOException, BallerinaOpenApiException {
OpenAPI openAPI = getOpenAPI(RES_DIR.resolve("swagger/return_type" +
"/no_content_type.yaml"));
Path expectedPath = RES_DIR.resolve("ballerina/return/no_content_type.bal");

List<String> list1 = new ArrayList<>();
Filter filter = new Filter(list1, list1);
OASClientConfig.Builder clientMetaDataBuilder = new OASClientConfig.Builder();
OASClientConfig oasClientConfig = clientMetaDataBuilder
.withFilters(filter)
.withOpenAPI(openAPI).build();
BallerinaClientGenerator ballerinaClientGenerator = new BallerinaClientGenerator(oasClientConfig);
SyntaxTree syntaxTree = ballerinaClientGenerator.generateSyntaxTree();
compareGeneratedSyntaxTreeWithExpectedSyntaxTree(expectedPath, syntaxTree);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ public isolated client class Client {
http:Request request = new;
json jsonBody = payload.toJson();
request.setPayload(jsonBody, "application/json");
return check self.clientEp->post(resourcePath, request);
return self.clientEp->post(resourcePath, request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ public isolated client class Client {
remote isolated function createPet(http:Request request) returns error? {
string resourcePath = string `/pets`;
// TODO: Update the request as needed;
return check self.clientEp->post(resourcePath, request);
return self.clientEp->post(resourcePath, request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public isolated client class Client {
string resourcePath = string `/pets`;
http:Request request = new;
request.setPayload(payload, "application/octet-stream");
return check self.clientEp->post(resourcePath, request);
return self.clientEp->post(resourcePath, request);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public isolated client class Client {
http:Request request = new;
string encodedRequestBody = payload.toBase64();
request.setPayload(encodedRequestBody, "application/octet-stream");
return check self.clientEp->post(resourcePath, request);
return self.clientEp->post(resourcePath, request);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ public isolated client class Client {
# + order_id - Order ID
# + risk_id - Order Risk ID
# + return - Status OK
remote isolated function delete_order_risk(string order_id, string risk_id) returns http:Response|error {
remote isolated function delete_order_risk(string order_id, string risk_id) returns error? {
string resourcePath = string `/admin/api/2021-10/orders/${getEncodedUri(order_id)}/risks/${getEncodedUri(risk_id)}.json`;
http:Response response = check self.clientEp-> delete(resourcePath);
return response;
return self.clientEp-> delete(resourcePath);
}
# Delete with request body.
#
Expand All @@ -51,7 +50,7 @@ public isolated client class Client {
string resourcePath = string `/request-body`;
http:Request request = new;
request.setPayload(payload, "application/json");
return check self.clientEp->delete(resourcePath, request);
return self.clientEp->delete(resourcePath, request);
}
# Delete with header.
#
Expand All @@ -61,8 +60,7 @@ public isolated client class Client {
string resourcePath = string `/header`;
map<any> headerValues = {"X-Request-ID": xRequestId};
map<string|string[]> httpHeaders = getMapForHeaders(headerValues);
return check self.clientEp->delete(resourcePath, headers = httpHeaders);
return response;
return self.clientEp->delete(resourcePath, headers = httpHeaders);
}
# Delete with header and request body.
#
Expand All @@ -74,6 +72,6 @@ public isolated client class Client {
map<string|string[]> httpHeaders = getMapForHeaders(headerValues);
http:Request request = new;
request.setPayload(payload, "application/json");
return check self.clientEp->delete(resourcePath, request, httpHeaders);
return self.clientEp->delete(resourcePath, request, httpHeaders);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ public isolated client class Client {
remote isolated function createPet(http:Request request) returns error? {
string resourcePath = string `/pets`;
// TODO: Update the request as needed;
return check self.clientEp->post(resourcePath, request);
return self.clientEp->post(resourcePath, request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public isolated client class Client {
http:Request request = new;
mime:Entity[] bodyParts = check createBodyParts(payload);
request.setBodyParts(bodyParts);
return check self.clientEp->post(resourcePath, request);
return self.clientEp->post(resourcePath, request);
}

# Create an user
Expand All @@ -56,6 +56,6 @@ public isolated client class Client {
http:Request request = new;
mime:Entity[] bodyParts = check createBodyParts(payload);
request.setBodyParts(bodyParts);
return check self.clientEp->post(resourcePath, request);
return self.clientEp->post(resourcePath, request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ public isolated client class Client {
remote isolated function createPet(http:Request request) returns error? {
string resourcePath = string `/pets`;
// TODO: Update the request as needed;
return check self.clientEp->post(resourcePath, request);
return self.clientEp->post(resourcePath, request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public isolated client class Client {
string resourcePath = string `/user`;
http:Request request = new;
request.setPayload(payload, "application/octet-stream");
return check self.clientEp->post(resourcePath, request);
return self.clientEp->post(resourcePath, request);
}
# Creates a new payment.
#
Expand All @@ -52,6 +52,6 @@ public isolated client class Client {
string resourcePath = string `/payment`;
http:Request request = new;
request.setPayload(payload, "application/octet-stream");
return check self.clientEp->post(resourcePath, request);
return self.clientEp->post(resourcePath, request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public isolated client class Client {
json jsonBody = payload.toJson();
xml? xmlBody = check xmldata:fromJson(jsonBody);
request.setPayload(xmlBody, "application/xml");
return check self.clientEp->put(resourcePath, request);
return self.clientEp->put(resourcePath, request);
}
# Request Body has nested allOf.
#
Expand All @@ -55,7 +55,7 @@ public isolated client class Client {
http:Request request = new;
json jsonBody = payload.toJson();
request.setPayload(jsonBody, "application/json");
return check self.clientEp->post(resourcePath, request);
return self.clientEp->post(resourcePath, request);
}
# Request Body has Array type AllOf.
#
Expand All @@ -66,6 +66,6 @@ public isolated client class Client {
json jsonBody = payload.toJson();
xml? xmlBody = check xmldata:fromJson(jsonBody);
request.setPayload(xmlBody, "application/xml");
return check self.clientEp->post(resourcePath, request);
return self.clientEp->post(resourcePath, request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ public isolated client class Client {
http:Request request = new;
json jsonBody = payload.toJson();
request.setPayload(jsonBody, "application/json");
return check self.clientEp->put(resourcePath, request);
return self.clientEp->put(resourcePath, request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public isolated client class Client {
http:Request request = new;
json jsonBody = payload.toJson();
request.setPayload(jsonBody, "application/json");
return check self.clientEp->put(resourcePath, request);
return self.clientEp->put(resourcePath, request);
}
# 01 Request body with reference.
#
Expand All @@ -54,7 +54,7 @@ public isolated client class Client {
http:Request request = new;
json jsonBody = payload.toJson();
request.setPayload(jsonBody, "application/json");
return check self.clientEp->post(resourcePath, request);
return self.clientEp->post(resourcePath, request);
}
# 04 Example for rb has inline requestbody.
#
Expand All @@ -65,7 +65,7 @@ public isolated client class Client {
http:Request request = new;
json jsonBody = payload.toJson();
request.setPayload(jsonBody, "application/json");
return check self.clientEp->put(resourcePath, request);
return self.clientEp->put(resourcePath, request);
}
# 03 Request body with record reference.
#
Expand All @@ -75,7 +75,7 @@ public isolated client class Client {
http:Request request = new;
json jsonBody = payload.toJson();
request.setPayload(jsonBody, "application/json");
return check self.clientEp->post(resourcePath, request);
return self.clientEp->post(resourcePath, request);
}
# 06 Example for rb has array inline requestbody.
#
Expand All @@ -86,7 +86,7 @@ public isolated client class Client {
json jsonBody = payload.toJson();
xml? xmlBody = check xmldata:fromJson(jsonBody);
request.setPayload(xmlBody, "application/xml");
return check self.clientEp->put(resourcePath, request);
return self.clientEp->put(resourcePath, request);
}
# 05 Example for rb has array inline requestbody.
#
Expand All @@ -97,7 +97,7 @@ public isolated client class Client {
json jsonBody = payload.toJson();
xml? xmlBody = check xmldata:fromJson(jsonBody);
request.setPayload(xmlBody, "application/xml");
return check self.clientEp->post(resourcePath, request);
return self.clientEp->post(resourcePath, request);
}
# 07 Example for rb has array inline requestbody.
#
Expand All @@ -108,6 +108,6 @@ public isolated client class Client {
json jsonBody = payload.toJson();
xml? xmlBody = check xmldata:fromJson(jsonBody);
request.setPayload(xmlBody, "application/xml");
return check self.clientEp->post(resourcePath, request);
return self.clientEp->post(resourcePath, request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ public isolated client class Client {
http:Request request = new;
json jsonBody = payload.toJson();
request.setPayload(jsonBody, "application/json");
return check self.clientEp->put(resourcePath, request);
return self.clientEp->put(resourcePath, request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@ public isolated client class Client {
http:Request request = new;
json jsonBody = payload.toJson();
request.setPayload(jsonBody, "application/json");
return check self.clientEp->put(resourcePath, request);
return self.clientEp->put(resourcePath, request);
}
# Request body has non-standard media type application/zip with object content without properties
#
# + return - Ok
resource isolated function post greeting03(http:Request request) returns error? {
string resourcePath = string `/greeting03`;
// TODO: Update the request as needed;
return check self.clientEp->post(resourcePath, request);
return self.clientEp->post(resourcePath, request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ public isolated client class Client {
http:Request request = new;
json jsonBody = payload.toJson();
request.setPayload(jsonBody, "application/json");
return check self.clientEp->post(resourcePath, request);
return self.clientEp->post(resourcePath, request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public isolated client class Client {
http:Request request = new;
json jsonBody = payload.toJson();
request.setPayload(jsonBody, "application/json");
return check self.clientEp->post(resourcePath, request);
return self.clientEp->post(resourcePath, request);
}
# Create a pet
#
Expand All @@ -54,6 +54,6 @@ public isolated client class Client {
http:Request request = new;
json jsonBody = payload.toJson();
request.setPayload(jsonBody, "application/json");
return check self.clientEp->post(resourcePath, request);
return self.clientEp->post(resourcePath, request);
}
}
Loading

0 comments on commit ed1ce59

Please sign in to comment.