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

Add Ballerina type/field/parameter name generation support from Ballerina name extension #1772

Merged
merged 11 commits into from
Sep 24, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,82 @@ public void testDefaultHeadersNameConflictWithQuery() {
}
}

@Test
public void testBallerinaNameExtensionInClient() {
String definitionPath = RES_DIR.resolve("bal_name_ext_sanitized.yaml").toString();
BallerinaCodeGenerator generator = new BallerinaCodeGenerator();
try {
String expectedClientContent = getStringFromGivenBalFile(expectedDirPath, "bal_name_ext_client.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 testBallerinaNameExtensionWithSanitization() {
String definitionPath = RES_DIR.resolve("bal_name_ext.yaml").toString();
BallerinaCodeGenerator generator = new BallerinaCodeGenerator();
try {
String expectedClientContent = getStringFromGivenBalFile(expectedDirPath, "bal_name_ext_client.bal");
generator.generateClient(definitionPath, resourcePath.toString(), filter,
new ClientGeneratorOptions(false, true, false, false,
true, true));
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 testBallerinaNameExtensionInService() {
String definitionPath = RES_DIR.resolve("bal_name_ext_sanitized.yaml").toString();
BallerinaCodeGenerator generator = new BallerinaCodeGenerator();
try {
String serviceName = "bal_name_ext";
String expectedServiceContractContent = getStringFromGivenBalFile(
expectedDirPath, "bal_name_ext_service_contract.bal");
ServiceGeneratorOptions options = new ServiceGeneratorOptions(false, false,
true, false, true, false);
generator.generateService(definitionPath, serviceName, resourcePath.toString(), filter, options);
if (Files.exists(resourcePath.resolve("bal_name_ext_service.bal"))) {
String generatedServiceContract = getStringFromGivenBalFile(resourcePath, "bal_name_ext_service.bal");
generatedServiceContract = (generatedServiceContract.trim()).replaceAll("\\s+", "");
expectedServiceContractContent = (expectedServiceContractContent.trim()).replaceAll("\\s+", "");
Assert.assertTrue(generatedServiceContract.contains(expectedServiceContractContent));
} else {
Assert.fail("Service contract was not generated");
}
} catch (IOException | BallerinaOpenApiException | FormatterException e) {
Assert.fail("Error while generating the service contract: " + e.getMessage());
} finally {
deleteGeneratedFiles("bal_name_ext_sanitized_service.bal");
}
}

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

Stream<String> expectedServiceLines = Files.lines(expectedServiceFile.resolve(s));
Expand Down
189 changes: 189 additions & 0 deletions openapi-cli/src/test/resources/bal_name_ext.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
openapi: 3.0.1
info:
title: Api V1
version: 0.0.0
servers:
- url: "http://{server}:{port}/api/v1"
variables:
server:
default: localhost
port:
default: "8080"
paths:
/albums:
get:
tags:
- albums
operationId: getAlbums
parameters:
- name: _artists_
in: query
schema:
type: array
items:
type: string
default: []
- name: X-API-VERSION
in: header
schema:
type: string
default: v1
responses:
"200":
description: Ok
content:
application/json:
schema:
type: array
items:
$ref: "#/components/schemas/album"
"400":
description: BadRequest
content:
application/json:
schema:
$ref: "#/components/schemas/ErrorPayload"
post:
tags:
- albums
operationId: postAlbum
requestBody:
content:
application/json:
schema:
$ref: "#/components/schemas/album"
required: true
responses:
"201":
description: Created
content:
application/json:
schema:
$ref: "#/components/schemas/album"
"400":
description: BadRequest
content:
application/json:
schema:
$ref: "#/components/schemas/ErrorPayload"
/albums/{_id}:
get:
tags:
- albums
operationId: getAlbumById
parameters:
- name: _id
in: path
required: true
schema:
type: string
responses:
"200":
description: Ok
content:
application/json:
schema:
$ref: "#/components/schemas/album"
"404":
description: NotFound
content:
application/json:
schema:
$ref: "#/components/schemas/message"
"400":
description: BadRequest
content:
application/json:
schema:
$ref: "#/components/schemas/ErrorPayload"
/albums/{id}/artist:
get:
tags:
- artists
operationId: getArtistByAlbum
parameters:
- name: id
in: path
required: true
schema:
type: string
responses:
"200":
description: Ok
content:
application/json:
schema:
$ref: "#/components/schemas/album_aRTIST"
"400":
description: BadRequest
content:
application/json:
schema:
$ref: "#/components/schemas/ErrorPayload"
components:
schemas:
ErrorPayload:
required:
- message
- method
- path
- reason
- status
- timestamp
type: object
properties:
timestamp:
type: string
status:
type: integer
format: int64
reason:
type: string
message:
type: string
path:
type: string
method:
type: string
album:
required:
- _id
- artist
- title
type: object
properties:
_id:
type: string
title:
type: string
artist:
type: string
additionalProperties: false
album_aRTIST:
required:
- albums
- id
- name
type: object
properties:
id:
type: string
name:
type: string
albums:
type: array
items:
$ref: "#/components/schemas/album"
additionalProperties: false
message:
required:
- code
- message
type: object
properties:
message:
type: string
code:
type: integer
format: int64
additionalProperties: false
Loading
Loading