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

Fix the sanitization logic in the auto-generated names #1768

Merged
merged 15 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
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 @@ -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
Loading