Skip to content

Commit

Permalink
Merge pull request #1768 from ballerina-platform/fix-header-query-gen
Browse files Browse the repository at this point in the history
Fix the sanitization logic in the auto-generated names
  • Loading branch information
TharmiganK authored Sep 11, 2024
2 parents c278557 + 9a0e2de commit 2ab7b5a
Show file tree
Hide file tree
Showing 40 changed files with 1,276 additions and 620 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,58 @@ public void testDefaultValueGenerationWithServiceContract() {
}
}

@Test
public void testDefaultHeadersQueriesParameterNameSanitization() {
String definitionPath = RES_DIR.resolve("default_headers_queries_parameters.yaml").toString();
BallerinaCodeGenerator generator = new BallerinaCodeGenerator();
try {
String expectedClientContent = getStringFromGivenBalFile(
expectedDirPath, "default_headers_queries_parameters.bal");
generator.generateClient(definitionPath, resourcePath.toString(), filter,
new ClientGeneratorOptions(false, true, false, false,
true, false));
if (Files.exists(resourcePath.resolve("client.bal"))) {
String generatedClient = getStringFromGivenBalFile(resourcePath, "client.bal");
generatedClient = (generatedClient.trim()).replaceAll("\\s+", "");
expectedClientContent = (expectedClientContent.trim()).replaceAll("\\s+", "");
Assert.assertTrue(generatedClient.contains(expectedClientContent));
} else {
Assert.fail("Client was not generated");
}
} catch (IOException | BallerinaOpenApiException |
OASTypeGenException | FormatterException e) {
Assert.fail("Error while generating the client: " + e.getMessage());
} finally {
deleteGeneratedFiles("client.bal");
}
}

@Test
public void testDefaultHeadersNameConflictWithQuery() {
String definitionPath = RES_DIR.resolve("default_headers_conflict_with_query.yaml").toString();
BallerinaCodeGenerator generator = new BallerinaCodeGenerator();
try {
String expectedClientContent = getStringFromGivenBalFile(
expectedDirPath, "default_headers_conflict_with_query.bal");
generator.generateClient(definitionPath, resourcePath.toString(), filter,
new ClientGeneratorOptions(false, true, false, false,
true, false));
if (Files.exists(resourcePath.resolve("client.bal"))) {
String generatedClient = getStringFromGivenBalFile(resourcePath, "client.bal");
generatedClient = (generatedClient.trim()).replaceAll("\\s+", "");
expectedClientContent = (expectedClientContent.trim()).replaceAll("\\s+", "");
Assert.assertTrue(generatedClient.contains(expectedClientContent));
} else {
Assert.fail("Client was not generated");
}
} catch (IOException | BallerinaOpenApiException |
OASTypeGenException | FormatterException e) {
Assert.fail("Error while generating the client: " + e.getMessage());
} finally {
deleteGeneratedFiles("client.bal");
}
}

private String getStringFromGivenBalFile(Path expectedServiceFile, String s) throws IOException {

Stream<String> expectedServiceLines = Files.lines(expectedServiceFile.resolve(s));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.util.Optional;
import java.util.Set;

import static io.ballerina.openapi.core.generators.common.GeneratorConstants.HEADERS;
import static io.ballerina.openapi.generators.common.GeneratorTestUtils.getOpenAPI;

/**
Expand All @@ -67,7 +68,7 @@ public void getFunctionBodyNodes(String yamlFile, String path, boolean hasHeader
TypeHandler.createInstance(openapi, false);
FunctionBodyGeneratorImp functionBodyGeneratorImp = new FunctionBodyGeneratorImp(path, operation, openapi,
new AuthConfigGeneratorImp(false, false),
new BallerinaUtilGenerator(), new ArrayList<>(), hasHeaders, hasDefaultHeaders, hasQueries);
new BallerinaUtilGenerator(), new ArrayList<>(), hasHeaders, hasDefaultHeaders, hasQueries, HEADERS);
Optional<FunctionBodyNode> bodyNode = functionBodyGeneratorImp.getFunctionBodyNode();
content = content.trim().replaceAll("\n", "").replaceAll("\\s+", "");
String bodyNodeContent = bodyNode.get().toString().trim().replaceAll("\n", "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void testFunctionNameGeneration(String operationId, String expectedFuncti
public Object[][] dataProvider() {
return new Object[][]{
{"get-pet-name", "getPetName"},
{"GET_Add_permission", "gET_Add_permission"},
{"GET_Add_Permission", "gETAddPermission"},
{"ListBankAccount", "listBankAccount"},
{"chat.media.download", "chatMediaDownload"}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
openapi: 3.0.1
info:
title: PayloadV
version: 0.0.0
servers:
- url: "http://{server}:{port}/payloadV"
variables:
server:
default: localhost
port:
default: "8080"
paths:
/albums/{id}:
get:
operationId: getAlbumsId
parameters:
- name: id
in: path
required: true
schema:
type: integer
format: int64
- name: headers
in: query
schema:
type: string
responses:
"200":
description: Ok
content:
application/json:
schema:
type: object
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
openapi: 3.0.1
info:
title: PayloadV
version: 0.0.0
servers:
- url: "http://{server}:{port}/payloadV"
variables:
server:
default: localhost
port:
default: "8080"
paths:
/albums/{id}:
get:
operationId: Get_Albums_Id
parameters:
- name: id
in: path
required: true
schema:
type: integer
format: int64
- name: albumId
in: query
required: false
style: form
explode: true
schema:
type: string
- name: q2
in: query
required: false
style: form
explode: true
schema:
type: integer
format: int64
- name: X-HEADER
in: header
required: false
style: simple
explode: false
schema:
type: string
responses:
"200":
description: Ok
content:
application/json:
schema:
type: object
Loading

0 comments on commit 2ab7b5a

Please sign in to comment.