Skip to content

Commit

Permalink
Add header, query util functions
Browse files Browse the repository at this point in the history
  • Loading branch information
TharmiganK committed Sep 23, 2024
1 parent 8d8c554 commit d8df945
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 41 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ stdlibJwtVersion=2.13.0
stdlibOAuth2Version=2.12.0

# Stdlib Level 05
stdlibHttpVersion=2.12.1-20240918-130700-9906dbe
stdlibHttpVersion=2.12.1-20240922-195800-82c5e76

# Stdlib Level 06
stdlibGrpcVersion=1.12.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@
*/
public class BallerinaUtilGenerator {

private boolean headersFound = false;
private boolean pathParametersFound = false;
private boolean queryParamsFound = false;
private boolean requestBodyEncodingFound = false;
Expand All @@ -136,7 +135,6 @@ public class BallerinaUtilGenerator {
private static final String GET_ENCODED_URI = "getEncodedUri";
private static final String GET_ORIGINAL_KEY = "getOriginalKey";
private static final String GET_PATH_FOR_QUERY_PARAM = "getPathForQueryParam";
private static final String GET_MAP_FOR_HEADERS = "getMapForHeaders";
private static final String GET_SERIALIZED_RECORD_ARRAY = "getSerializedRecordArray";
private static final String CREATE_MULTIPART_BODY_PARTS = "createBodyParts";
private static final String GET_VALIDATED_RESPONSE_FOR_DEFAULT_MAPPING = "getValidatedResponseForDefaultMapping";
Expand All @@ -151,16 +149,6 @@ public void setQueryParamsFound(boolean flag) {
this.queryParamsFound = flag;
}

/**
* Set `headersFound` flag to `true` when at least one header found.
*
* @param flag Function will be called only in the occasions where flag needs to be set to `true`
*/
public void setHeadersFound(boolean flag) {

this.headersFound = flag;
}

/**
* Set `pathParametersFound` flag to `true` when at least one path parameter found.
*
Expand Down Expand Up @@ -260,9 +248,6 @@ private Set<String> getFunctionNameList() {
GET_SERIALIZED_RECORD_ARRAY
));
}
if (headersFound) {
functionNameList.add(GET_MAP_FOR_HEADERS);
}
if (pathParametersFound) {
functionNameList.add(GET_ENCODED_URI);
}
Expand All @@ -288,7 +273,7 @@ private List<ImportDeclarationNode> generateImports(Set<String> functionNameList
ImportDeclarationNode importMime = GeneratorUtils.getImportDeclarationNode(BALLERINA, MIME);
imports.add(importMime);
}
if (defaultStatusCodeResponseBindingFound) {
if (defaultStatusCodeResponseBindingFound || queryParamsFound) {
ImportDeclarationNode importForHttp = GeneratorUtils.getImportDeclarationNode(BALLERINA, HTTP);
imports.add(importForHttp);
}
Expand All @@ -301,7 +286,7 @@ private List<ImportDeclarationNode> generateImports(Set<String> functionNameList
* @param memberDeclarationNodes {@link ModuleMemberDeclarationNode}
*/
private void getUtilTypeDeclarationNodes(List<ModuleMemberDeclarationNode> memberDeclarationNodes) {
if (requestBodyEncodingFound || queryParamsFound || headersFound || requestBodyMultipartFormDatafound) {
if (requestBodyEncodingFound || queryParamsFound || requestBodyMultipartFormDatafound) {
memberDeclarationNodes.add(getSimpleBasicTypeDefinitionNode());
}
if (requestBodyEncodingFound || queryParamsFound || requestBodyMultipartFormDatafound) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public class FunctionBodyGeneratorImp implements FunctionBodyGenerator {

public static final String MAP_ANYDATA = "map<anydata> ";
public static final String MAP_STRING_STRING_ARRAY = "map<string|string[]> ";
public static final String GET_MAP_FOR_HEADERS = " = getMapForHeaders(";
public static final String GET_MAP_FOR_HEADERS = " = http:getHeaderMap(";
private final List<ImportDeclarationNode> imports;
private final String path;
protected final Map.Entry<PathItem.HttpMethod, Operation> operation;
Expand Down Expand Up @@ -286,11 +286,9 @@ public void handleQueryParamsAndHeaders(List<Parameter> queryParameters, List<St
addApiKeysToMap(HEADER_VALUES, headerApiKeyNameList, statementsList);
statementsList.add(GeneratorUtils.getSimpleExpressionStatementNode(
MAP_STRING_STRING_ARRAY + HTTP_HEADERS + GET_MAP_FOR_HEADERS + HEADER_VALUES + ")"));
ballerinaUtilGenerator.setHeadersFound(true);
} else if (!hasDefaultHeaders) {
statementsList.add(GeneratorUtils.getSimpleExpressionStatementNode(
MAP_STRING_STRING_ARRAY + HTTP_HEADERS + GET_MAP_FOR_HEADERS + headersParamName + ")"));
ballerinaUtilGenerator.setHeadersFound(true);
}
}
}
Expand Down Expand Up @@ -355,7 +353,6 @@ private void addUpdatedPathAndHeaders(List<StatementNode> statementsList, List<S
if ((hasHeaders || !headerApiKeyNameList.isEmpty()) && !hasDefaultHeaders) {
statementsList.add(GeneratorUtils.getSimpleExpressionStatementNode(
MAP_STRING_STRING_ARRAY + HTTP_HEADERS + GET_MAP_FOR_HEADERS + headerVarName + ")"));
ballerinaUtilGenerator.setHeadersFound(true);
}
}

Expand Down
23 changes: 4 additions & 19 deletions openapi-core/src/main/resources/templates/utils_openapi.bal
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,13 @@ isolated function getEncodedUri(anydata value) returns string {
# + encodingMap - Details on serialization mechanism
# + return - Returns generated Path or error at failure of client initialization
isolated function getPathForQueryParam(map<anydata> queryParam, map<Encoding> encodingMap = {}) returns string|error {
map<anydata> queriesMap = http:getQueryMap(queryParam);
string[] param = [];
if queryParam.length() > 0 {
if queriesMap.length() > 0 {
param.push("?");
foreach var [key, value] in queryParam.entries() {
foreach var [key, value] in queriesMap.entries() {
if value is () {
_ = queryParam.remove(key);
_ = queriesMap.remove(key);
continue;
}
Encoding encodingData = encodingMap.hasKey(key) ? encodingMap.get(key) : defaultEncoding;
Expand All @@ -230,22 +231,6 @@ isolated function getPathForQueryParam(map<anydata> queryParam, map<Encoding> en
return restOfPath;
}

# Generate header map for given header values.
#
# + headerParam - Headers map
# + return - Returns generated map or error at failure of client initialization
isolated function getMapForHeaders(map<anydata> headerParam) returns map<string|string[]> {
map<string|string[]> headerMap = {};
foreach var [key, value] in headerParam.entries() {
if value is SimpleBasicType[] {
headerMap[key] = from SimpleBasicType data in value select data.toString();
} else {
headerMap[key] = value.toString();
}
}
return headerMap;
}

isolated function createBodyParts(record {|anydata...;|} anyRecord, map<Encoding> encodingMap = {}) returns mime:Entity[]|error {
mime:Entity[] entities = [];
foreach [string, anydata] [key, value] in anyRecord.entries() {
Expand Down

0 comments on commit d8df945

Please sign in to comment.