-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1799 from SachinAkash01/service-object-type-name
Add Support for Parameterizing the Service Object Type Name
- Loading branch information
Showing
16 changed files
with
679 additions
and
1 deletion.
There are no files selected for viewing
151 changes: 151 additions & 0 deletions
151
openapi-cli/src/test/java/io/ballerina/openapi/generators/service/ServiceContractTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
/* | ||
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com). | ||
* | ||
* WSO2 LLC. licenses this file to you under the Apache License, | ||
* Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package io.ballerina.openapi.generators.service; | ||
|
||
import io.ballerina.compiler.syntax.tree.SyntaxTree; | ||
import io.ballerina.openapi.core.generators.common.GeneratorUtils; | ||
import io.ballerina.openapi.core.generators.common.TypeHandler; | ||
import io.ballerina.openapi.core.generators.common.exception.BallerinaOpenApiException; | ||
import io.ballerina.openapi.core.generators.common.model.Filter; | ||
import io.ballerina.openapi.core.generators.service.ServiceContractGenerator; | ||
import io.ballerina.openapi.core.generators.service.ServiceDeclarationGenerator; | ||
import io.ballerina.openapi.core.generators.service.model.OASServiceMetadata; | ||
import io.swagger.v3.oas.models.OpenAPI; | ||
import org.testng.annotations.Test; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class ServiceContractTests { | ||
private static final Path RES_DIR = Paths.get("src/test/resources/generators/service"); | ||
List<String> list1 = new ArrayList<>(); | ||
List<String> list2 = new ArrayList<>(); | ||
Filter filter = new Filter(list1, list2); | ||
SyntaxTree syntaxTree; | ||
|
||
@Test(description = "Test default service object type name") | ||
public void testDefaultServiceTypeNameInGeneratedService() throws IOException, BallerinaOpenApiException { | ||
Path definitionPath = RES_DIR.resolve("swagger/service_type/default_service_type_name.yaml"); | ||
OpenAPI openAPI = GeneratorUtils.getOpenAPIFromOpenAPIV3Parser(definitionPath); | ||
OASServiceMetadata oasServiceMetadata = new OASServiceMetadata.Builder() | ||
.withOpenAPI(openAPI) | ||
.withFilters(filter) | ||
.build(); | ||
TypeHandler.createInstance(openAPI, false); | ||
ServiceDeclarationGenerator ballerinaServiceGenerator = new ServiceDeclarationGenerator(oasServiceMetadata); | ||
ServiceContractGenerator serviceContractGenerator = new ServiceContractGenerator(oasServiceMetadata, | ||
ballerinaServiceGenerator.getFunctionsList()); | ||
syntaxTree = serviceContractGenerator.generateSyntaxTree(); | ||
CommonTestFunctions.compareGeneratedSyntaxTreewithExpectedSyntaxTree( | ||
"service_type/default_service_type_name.bal", syntaxTree); | ||
} | ||
|
||
@Test(description = "Test custom service object type name") | ||
public void testCustomServiceTypeNameInGeneratedService() throws IOException, BallerinaOpenApiException { | ||
Path definitionPath = RES_DIR.resolve("swagger/service_type/custom_service_type_name.yaml"); | ||
OpenAPI openAPI = GeneratorUtils.getOpenAPIFromOpenAPIV3Parser(definitionPath); | ||
OASServiceMetadata oasServiceMetadata = new OASServiceMetadata.Builder() | ||
.withOpenAPI(openAPI) | ||
.withFilters(filter) | ||
.withServiceObjectTypeName("CustomServiceObjectTypeName") | ||
.build(); | ||
TypeHandler.createInstance(openAPI, false); | ||
ServiceDeclarationGenerator ballerinaServiceGenerator = new ServiceDeclarationGenerator(oasServiceMetadata); | ||
ServiceContractGenerator serviceContractGenerator = new ServiceContractGenerator(oasServiceMetadata, | ||
ballerinaServiceGenerator.getFunctionsList()); | ||
syntaxTree = serviceContractGenerator.generateSyntaxTree(); | ||
CommonTestFunctions.compareGeneratedSyntaxTreewithExpectedSyntaxTree( | ||
"service_type/custom_service_type_name.bal", syntaxTree); | ||
} | ||
|
||
@Test(description = "Test custom service object type name with special characters") | ||
public void testCustomServiceTypeNameWithSpecialCharacters() throws IOException, BallerinaOpenApiException { | ||
Path definitionPath = RES_DIR.resolve("swagger/service_type/custom_name_with_special_characters.yaml"); | ||
OpenAPI openAPI = GeneratorUtils.getOpenAPIFromOpenAPIV3Parser(definitionPath); | ||
OASServiceMetadata oasServiceMetadata = new OASServiceMetadata.Builder() | ||
.withOpenAPI(openAPI) | ||
.withFilters(filter) | ||
.withServiceObjectTypeName("CustomServiceObject-TypeName1") | ||
.build(); | ||
TypeHandler.createInstance(openAPI, false); | ||
ServiceDeclarationGenerator ballerinaServiceGenerator = new ServiceDeclarationGenerator(oasServiceMetadata); | ||
ServiceContractGenerator serviceContractGenerator = new ServiceContractGenerator(oasServiceMetadata, | ||
ballerinaServiceGenerator.getFunctionsList()); | ||
syntaxTree = serviceContractGenerator.generateSyntaxTree(); | ||
CommonTestFunctions.compareGeneratedSyntaxTreewithExpectedSyntaxTree( | ||
"service_type/custom_name_with_special_characters.bal", syntaxTree); | ||
} | ||
|
||
@Test(description = "Test custom service object type name with a whitespace") | ||
public void testCustomServiceTypeNameWithWhitespace() throws IOException, BallerinaOpenApiException { | ||
Path definitionPath = RES_DIR.resolve("swagger/service_type/custom_name_with_whitespace.yaml"); | ||
OpenAPI openAPI = GeneratorUtils.getOpenAPIFromOpenAPIV3Parser(definitionPath); | ||
OASServiceMetadata oasServiceMetadata = new OASServiceMetadata.Builder() | ||
.withOpenAPI(openAPI) | ||
.withFilters(filter) | ||
.withServiceObjectTypeName("CustomServiceObject TypeName2") | ||
.build(); | ||
TypeHandler.createInstance(openAPI, false); | ||
ServiceDeclarationGenerator ballerinaServiceGenerator = new ServiceDeclarationGenerator(oasServiceMetadata); | ||
ServiceContractGenerator serviceContractGenerator = new ServiceContractGenerator(oasServiceMetadata, | ||
ballerinaServiceGenerator.getFunctionsList()); | ||
syntaxTree = serviceContractGenerator.generateSyntaxTree(); | ||
CommonTestFunctions.compareGeneratedSyntaxTreewithExpectedSyntaxTree( | ||
"service_type/custom_name_with_whitespace.bal", syntaxTree); | ||
} | ||
|
||
@Test(description = "Test custom service object type name with an empty value") | ||
public void testCustomServiceTypeNameWithEmptyValue() throws IOException, BallerinaOpenApiException { | ||
Path definitionPath = RES_DIR.resolve("swagger/service_type/custom_name_with_empty_value.yaml"); | ||
OpenAPI openAPI = GeneratorUtils.getOpenAPIFromOpenAPIV3Parser(definitionPath); | ||
OASServiceMetadata oasServiceMetadata = new OASServiceMetadata.Builder() | ||
.withOpenAPI(openAPI) | ||
.withFilters(filter) | ||
.withServiceObjectTypeName("") | ||
.build(); | ||
TypeHandler.createInstance(openAPI, false); | ||
ServiceDeclarationGenerator ballerinaServiceGenerator = new ServiceDeclarationGenerator(oasServiceMetadata); | ||
ServiceContractGenerator serviceContractGenerator = new ServiceContractGenerator(oasServiceMetadata, | ||
ballerinaServiceGenerator.getFunctionsList()); | ||
syntaxTree = serviceContractGenerator.generateSyntaxTree(); | ||
CommonTestFunctions.compareGeneratedSyntaxTreewithExpectedSyntaxTree( | ||
"service_type/custom_name_with_empty_value.bal", syntaxTree); | ||
} | ||
|
||
@Test(description = "Test custom service object type name with only whitespace") | ||
public void testCustomServiceTypeNameWithOnlyWhitespace() throws IOException, BallerinaOpenApiException { | ||
Path definitionPath = RES_DIR.resolve("swagger/service_type/custom_name_only_whitespace.yaml"); | ||
OpenAPI openAPI = GeneratorUtils.getOpenAPIFromOpenAPIV3Parser(definitionPath); | ||
OASServiceMetadata oasServiceMetadata = new OASServiceMetadata.Builder() | ||
.withOpenAPI(openAPI) | ||
.withFilters(filter) | ||
.withServiceObjectTypeName(" ") | ||
.build(); | ||
TypeHandler.createInstance(openAPI, false); | ||
ServiceDeclarationGenerator ballerinaServiceGenerator = new ServiceDeclarationGenerator(oasServiceMetadata); | ||
ServiceContractGenerator serviceContractGenerator = new ServiceContractGenerator(oasServiceMetadata, | ||
ballerinaServiceGenerator.getFunctionsList()); | ||
syntaxTree = serviceContractGenerator.generateSyntaxTree(); | ||
CommonTestFunctions.compareGeneratedSyntaxTreewithExpectedSyntaxTree( | ||
"service_type/custom_name_only_whitespace.bal", syntaxTree); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
.../test/resources/generators/service/ballerina/service_type/custom_name_only_whitespace.bal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import ballerina/http; | ||
|
||
@http:ServiceConfig { basePath: "/v1" } | ||
type OASServiceType service object { | ||
*http:ServiceContract; | ||
resource function post pets() returns InlineResponse400BadRequest; | ||
}; |
7 changes: 7 additions & 0 deletions
7
...test/resources/generators/service/ballerina/service_type/custom_name_with_empty_value.bal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import ballerina/http; | ||
|
||
@http:ServiceConfig { basePath: "/v1" } | ||
type OASServiceType service object { | ||
*http:ServiceContract; | ||
resource function post pets() returns InlineResponse400BadRequest; | ||
}; |
7 changes: 7 additions & 0 deletions
7
...sources/generators/service/ballerina/service_type/custom_name_with_special_characters.bal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import ballerina/http; | ||
|
||
@http:ServiceConfig { basePath: "/v1" } | ||
type CustomServiceObject\-TypeName1 service object { | ||
*http:ServiceContract; | ||
resource function post pets() returns InlineResponse400BadRequest; | ||
}; |
7 changes: 7 additions & 0 deletions
7
.../test/resources/generators/service/ballerina/service_type/custom_name_with_whitespace.bal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import ballerina/http; | ||
|
||
@http:ServiceConfig { basePath: "/v1" } | ||
type CustomServiceObject\ TypeName2 service object { | ||
*http:ServiceContract; | ||
resource function post pets() returns InlineResponse400BadRequest; | ||
}; |
7 changes: 7 additions & 0 deletions
7
...src/test/resources/generators/service/ballerina/service_type/custom_service_type_name.bal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import ballerina/http; | ||
|
||
@http:ServiceConfig { basePath: "/v1" } | ||
type CustomServiceObjectTypeName service object { | ||
*http:ServiceContract; | ||
resource function post pets() returns InlineResponse400BadRequest; | ||
}; |
7 changes: 7 additions & 0 deletions
7
...rc/test/resources/generators/service/ballerina/service_type/default_service_type_name.bal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import ballerina/http; | ||
|
||
@http:ServiceConfig { basePath: "/v1" } | ||
type OASServiceType service object { | ||
*http:ServiceContract; | ||
resource function post pets() returns anydata; | ||
}; |
78 changes: 78 additions & 0 deletions
78
...c/test/resources/generators/service/swagger/service_type/custom_name_only_whitespace.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
openapi: 3.0.0 | ||
info: | ||
title: refComponent | ||
description: refComponent | ||
version: 1.0.0 | ||
servers: | ||
- url: http://petstore.{host}.io/v1 | ||
description: The production API server | ||
variables: | ||
host: | ||
default: openapi | ||
description: this value is assigned by the service provider | ||
- url: https://{subdomain}.swagger.io:{port}/{basePath} | ||
description: The production API server | ||
variables: | ||
subdomain: | ||
default: petstore | ||
description: this value is assigned by the service provider | ||
port: | ||
enum: | ||
- '8443' | ||
- '443' | ||
default: '443' | ||
basePath: | ||
default: v2 | ||
paths: | ||
/pets: | ||
post: | ||
summary: Creates a new pets. | ||
responses: | ||
'400': | ||
description: A User object | ||
content: | ||
application/json: | ||
schema: | ||
type: object | ||
properties: | ||
id: | ||
type: integer | ||
description: The error ID. | ||
errorType: | ||
type: string | ||
description: The error name. | ||
components: | ||
schemas: | ||
User: | ||
type: object | ||
required: | ||
- userName | ||
properties: | ||
userName: | ||
type: string | ||
firstName: | ||
type: string | ||
lastName: | ||
type: string | ||
Pet: | ||
type: object | ||
required: | ||
- userName | ||
properties: | ||
userName: | ||
type: string | ||
firstName: | ||
type: string | ||
lastName: | ||
type: string | ||
PetForm: | ||
type: object | ||
required: | ||
- userName | ||
properties: | ||
userName: | ||
type: string | ||
firstName: | ||
type: string | ||
lastName: | ||
type: string |
78 changes: 78 additions & 0 deletions
78
.../test/resources/generators/service/swagger/service_type/custom_name_with_empty_value.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
openapi: 3.0.0 | ||
info: | ||
title: refComponent | ||
description: refComponent | ||
version: 1.0.0 | ||
servers: | ||
- url: http://petstore.{host}.io/v1 | ||
description: The production API server | ||
variables: | ||
host: | ||
default: openapi | ||
description: this value is assigned by the service provider | ||
- url: https://{subdomain}.swagger.io:{port}/{basePath} | ||
description: The production API server | ||
variables: | ||
subdomain: | ||
default: petstore | ||
description: this value is assigned by the service provider | ||
port: | ||
enum: | ||
- '8443' | ||
- '443' | ||
default: '443' | ||
basePath: | ||
default: v2 | ||
paths: | ||
/pets: | ||
post: | ||
summary: Creates a new pets. | ||
responses: | ||
'400': | ||
description: A User object | ||
content: | ||
application/json: | ||
schema: | ||
type: object | ||
properties: | ||
id: | ||
type: integer | ||
description: The error ID. | ||
errorType: | ||
type: string | ||
description: The error name. | ||
components: | ||
schemas: | ||
User: | ||
type: object | ||
required: | ||
- userName | ||
properties: | ||
userName: | ||
type: string | ||
firstName: | ||
type: string | ||
lastName: | ||
type: string | ||
Pet: | ||
type: object | ||
required: | ||
- userName | ||
properties: | ||
userName: | ||
type: string | ||
firstName: | ||
type: string | ||
lastName: | ||
type: string | ||
PetForm: | ||
type: object | ||
required: | ||
- userName | ||
properties: | ||
userName: | ||
type: string | ||
firstName: | ||
type: string | ||
lastName: | ||
type: string |
Oops, something went wrong.