diff --git a/openapi-cli/src/test/java/io/ballerina/openapi/generators/service/ServiceContractTests.java b/openapi-cli/src/test/java/io/ballerina/openapi/generators/service/ServiceContractTests.java index 43f8478be..c3bf7714b 100644 --- a/openapi-cli/src/test/java/io/ballerina/openapi/generators/service/ServiceContractTests.java +++ b/openapi-cli/src/test/java/io/ballerina/openapi/generators/service/ServiceContractTests.java @@ -42,6 +42,23 @@ public class ServiceContractTests { 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"); @@ -60,13 +77,14 @@ public void testCustomServiceTypeNameInGeneratedService() throws IOException, Ba "service_type/custom_service_type_name.bal", 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"); + @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); @@ -74,6 +92,78 @@ public void testDefaultServiceTypeNameInGeneratedService() throws IOException, B ballerinaServiceGenerator.getFunctionsList()); syntaxTree = serviceContractGenerator.generateSyntaxTree(); CommonTestFunctions.compareGeneratedSyntaxTreewithExpectedSyntaxTree( - "service_type/default_service_type_name.bal", syntaxTree); + "service_type/custom_name_with_special_characters.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 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 only special characters") + public void testCustomServiceTypeNameWithOnlySpecialCharacters() throws IOException, BallerinaOpenApiException { + Path definitionPath = RES_DIR.resolve("swagger/service_type/custom_name_only_special_characters.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_special_characters.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); } } diff --git a/openapi-cli/src/test/resources/generators/service/ballerina/service_type/custom_name_only_special_characters.bal b/openapi-cli/src/test/resources/generators/service/ballerina/service_type/custom_name_only_special_characters.bal new file mode 100644 index 000000000..874783110 --- /dev/null +++ b/openapi-cli/src/test/resources/generators/service/ballerina/service_type/custom_name_only_special_characters.bal @@ -0,0 +1,7 @@ +import ballerina/http; + +@http:ServiceConfig { basePath: "/v1" } +type OASServiceType service object { + *http:ServiceContract; + resource function post pets() returns InlineResponse400BadRequest; +}; diff --git a/openapi-cli/src/test/resources/generators/service/ballerina/service_type/custom_name_only_whitespace.bal b/openapi-cli/src/test/resources/generators/service/ballerina/service_type/custom_name_only_whitespace.bal new file mode 100644 index 000000000..874783110 --- /dev/null +++ b/openapi-cli/src/test/resources/generators/service/ballerina/service_type/custom_name_only_whitespace.bal @@ -0,0 +1,7 @@ +import ballerina/http; + +@http:ServiceConfig { basePath: "/v1" } +type OASServiceType service object { + *http:ServiceContract; + resource function post pets() returns InlineResponse400BadRequest; +}; diff --git a/openapi-cli/src/test/resources/generators/service/ballerina/service_type/custom_name_with_empty_value.bal b/openapi-cli/src/test/resources/generators/service/ballerina/service_type/custom_name_with_empty_value.bal new file mode 100644 index 000000000..874783110 --- /dev/null +++ b/openapi-cli/src/test/resources/generators/service/ballerina/service_type/custom_name_with_empty_value.bal @@ -0,0 +1,7 @@ +import ballerina/http; + +@http:ServiceConfig { basePath: "/v1" } +type OASServiceType service object { + *http:ServiceContract; + resource function post pets() returns InlineResponse400BadRequest; +}; diff --git a/openapi-cli/src/test/resources/generators/service/ballerina/service_type/custom_name_with_special_characters.bal b/openapi-cli/src/test/resources/generators/service/ballerina/service_type/custom_name_with_special_characters.bal new file mode 100644 index 000000000..65c1d4aa7 --- /dev/null +++ b/openapi-cli/src/test/resources/generators/service/ballerina/service_type/custom_name_with_special_characters.bal @@ -0,0 +1,7 @@ +import ballerina/http; + +@http:ServiceConfig { basePath: "/v1" } +type CustomServiceObjectTypeName1 service object { + *http:ServiceContract; + resource function post pets() returns InlineResponse400BadRequest; +}; diff --git a/openapi-cli/src/test/resources/generators/service/ballerina/service_type/custom_name_with_whitespace.bal b/openapi-cli/src/test/resources/generators/service/ballerina/service_type/custom_name_with_whitespace.bal new file mode 100644 index 000000000..1089477d3 --- /dev/null +++ b/openapi-cli/src/test/resources/generators/service/ballerina/service_type/custom_name_with_whitespace.bal @@ -0,0 +1,7 @@ +import ballerina/http; + +@http:ServiceConfig { basePath: "/v1" } +type CustomServiceObjectTypeName2 service object { + *http:ServiceContract; + resource function post pets() returns InlineResponse400BadRequest; +}; diff --git a/openapi-cli/src/test/resources/generators/service/swagger/service_type/custom_name_only_special_characters.yaml b/openapi-cli/src/test/resources/generators/service/swagger/service_type/custom_name_only_special_characters.yaml new file mode 100644 index 000000000..081a1e037 --- /dev/null +++ b/openapi-cli/src/test/resources/generators/service/swagger/service_type/custom_name_only_special_characters.yaml @@ -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 diff --git a/openapi-cli/src/test/resources/generators/service/swagger/service_type/custom_name_only_whitespace.yaml b/openapi-cli/src/test/resources/generators/service/swagger/service_type/custom_name_only_whitespace.yaml new file mode 100644 index 000000000..081a1e037 --- /dev/null +++ b/openapi-cli/src/test/resources/generators/service/swagger/service_type/custom_name_only_whitespace.yaml @@ -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 diff --git a/openapi-cli/src/test/resources/generators/service/swagger/service_type/custom_name_with_empty_value.yaml b/openapi-cli/src/test/resources/generators/service/swagger/service_type/custom_name_with_empty_value.yaml new file mode 100644 index 000000000..081a1e037 --- /dev/null +++ b/openapi-cli/src/test/resources/generators/service/swagger/service_type/custom_name_with_empty_value.yaml @@ -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 diff --git a/openapi-cli/src/test/resources/generators/service/swagger/service_type/custom_name_with_special_characters.yaml b/openapi-cli/src/test/resources/generators/service/swagger/service_type/custom_name_with_special_characters.yaml new file mode 100644 index 000000000..081a1e037 --- /dev/null +++ b/openapi-cli/src/test/resources/generators/service/swagger/service_type/custom_name_with_special_characters.yaml @@ -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 diff --git a/openapi-cli/src/test/resources/generators/service/swagger/service_type/custom_name_with_whitespace.yaml b/openapi-cli/src/test/resources/generators/service/swagger/service_type/custom_name_with_whitespace.yaml new file mode 100644 index 000000000..081a1e037 --- /dev/null +++ b/openapi-cli/src/test/resources/generators/service/swagger/service_type/custom_name_with_whitespace.yaml @@ -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 diff --git a/openapi-core/src/main/java/io/ballerina/openapi/core/generators/service/model/OASServiceMetadata.java b/openapi-core/src/main/java/io/ballerina/openapi/core/generators/service/model/OASServiceMetadata.java index 830d66eff..d5eaae3d0 100644 --- a/openapi-core/src/main/java/io/ballerina/openapi/core/generators/service/model/OASServiceMetadata.java +++ b/openapi-core/src/main/java/io/ballerina/openapi/core/generators/service/model/OASServiceMetadata.java @@ -168,7 +168,14 @@ public Builder withIsUsingSanitizedOas(boolean isUsingSanitizedOas) { } public Builder withServiceObjectTypeName(String serviceObjectTypeName) { - this.serviceObjectTypeName = serviceObjectTypeName; + if (serviceObjectTypeName == null || serviceObjectTypeName.trim().isEmpty()) { + this.serviceObjectTypeName = GeneratorConstants.SERVICE_TYPE_NAME; + } else { + this.serviceObjectTypeName = serviceObjectTypeName.replaceAll("[^a-zA-Z0-9]", ""); + if (this.serviceObjectTypeName.isEmpty()) { + this.serviceObjectTypeName = GeneratorConstants.SERVICE_TYPE_NAME; + } + } return this; }