From 8978558d131b61381475994633fd392b9c836ff6 Mon Sep 17 00:00:00 2001 From: Petr Kadlec Date: Fri, 9 Oct 2020 20:37:06 +0200 Subject: [PATCH 1/2] codegen: applying kotlin naming rules to operationId param --- .../main/kotlin/apifi/parser/PathsParser.kt | 18 ++++++++++++++++-- .../parser/params/with-invalid-operationId.yml | 16 ++++++++++++++++ .../kotlin/apifi/parser/PathsParserTest.kt | 11 ++++++++++- 3 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 codegen/src/test-res/parser/params/with-invalid-operationId.yml diff --git a/codegen/src/main/kotlin/apifi/parser/PathsParser.kt b/codegen/src/main/kotlin/apifi/parser/PathsParser.kt index 95aaf3d..1971de9 100644 --- a/codegen/src/main/kotlin/apifi/parser/PathsParser.kt +++ b/codegen/src/main/kotlin/apifi/parser/PathsParser.kt @@ -1,6 +1,7 @@ package apifi.parser import apifi.helpers.replaceArrayToList +import apifi.helpers.toCamelCase import apifi.helpers.toTitleCase import apifi.helpers.typeDeclaration import apifi.models.Model @@ -21,7 +22,7 @@ object PathsParser { val responses = ResponseBodyParser.parse(operation.responses, operationSpecifier) models.addAll(request?.models ?: emptyList()) models.addAll(responses?.models ?: emptyList()) - Operation(httpMethod, operation.operationId ?: httpMethod.toString().toLowerCase(), + Operation(httpMethod, getValidOperationId(operation.operationId) ?: httpMethod.toString().toLowerCase(), operation.tags ?: emptyList(), params, request?.result, responses?.result ?: emptyList()) } Path(endpoint, operations) @@ -31,4 +32,17 @@ object PathsParser { private fun operationSpecifier(operation: io.swagger.v3.oas.models.Operation, httpMethod: PathItem.HttpMethod, endpoint: String) = (operation.operationId ?: (httpMethod.toString() + endpoint.replace(Regex("[^A-Za-z ]"), " ")).toTitleCase()) -} \ No newline at end of file + + /** + * Must start with a lower case letter and use the camel case and no underscores. + */ + private fun getValidOperationId(operationId: String?): String? { + return operationId?.let { + if (it.first().isDigit()) { + operationId.substring(1).toCamelCase() + } else { + operationId.toCamelCase() + } + } + } +} diff --git a/codegen/src/test-res/parser/params/with-invalid-operationId.yml b/codegen/src/test-res/parser/params/with-invalid-operationId.yml new file mode 100644 index 0000000..ff447c0 --- /dev/null +++ b/codegen/src/test-res/parser/params/with-invalid-operationId.yml @@ -0,0 +1,16 @@ +openapi: "3.0.0" +paths: + /pets: + get: + summary: List all pets + operationId: '1 list All_Pets ' + parameters: + - name: id + in: query + description: id to filter by + required: false + style: form + schema: + type: array + items: + type: string diff --git a/codegen/src/test/kotlin/apifi/parser/PathsParserTest.kt b/codegen/src/test/kotlin/apifi/parser/PathsParserTest.kt index f4a97e4..cd7d068 100644 --- a/codegen/src/test/kotlin/apifi/parser/PathsParserTest.kt +++ b/codegen/src/test/kotlin/apifi/parser/PathsParserTest.kt @@ -5,6 +5,7 @@ import apifi.parser.models.Param import apifi.parser.models.ParamType import io.kotest.core.spec.style.DescribeSpec import io.kotest.matchers.shouldBe +import io.kotest.matchers.shouldNotBe import io.swagger.v3.oas.models.PathItem.HttpMethod import io.swagger.v3.parser.OpenAPIV3Parser import org.apache.commons.io.FileUtils @@ -57,5 +58,13 @@ class PathsParserTest : DescribeSpec({ path.operations!![0].type shouldBe HttpMethod.GET path.operations!![0].params[0] shouldBe Param("id", "kotlin.collections.List", false, ParamType.Query) } + it("with invalid operationId") { + val file = FileUtils.getFile("src", "test-res", "parser", "params", "with-invalid-operationId.yml").readText().trimIndent() + val openApi = OpenAPIV3Parser().readContents(file).openAPI + val path = PathsParser.parse(openApi.paths).result[0] + val operation = path.operations?.first() + operation shouldNotBe null + operation?.name shouldBe "listAllPets" + } } -}) \ No newline at end of file +}) From 11e99bef0c910534aa823ce9b660e82a47cc1903 Mon Sep 17 00:00:00 2001 From: Petr Kadlec Date: Mon, 12 Oct 2020 18:17:55 +0200 Subject: [PATCH 2/2] codegen: applying kotlin naming rules to operationId param - 'operation' prepend if operationId starts with a digit --- codegen/src/main/kotlin/apifi/parser/PathsParser.kt | 2 +- codegen/src/test/kotlin/apifi/parser/PathsParserTest.kt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/codegen/src/main/kotlin/apifi/parser/PathsParser.kt b/codegen/src/main/kotlin/apifi/parser/PathsParser.kt index 1971de9..786f97c 100644 --- a/codegen/src/main/kotlin/apifi/parser/PathsParser.kt +++ b/codegen/src/main/kotlin/apifi/parser/PathsParser.kt @@ -39,7 +39,7 @@ object PathsParser { private fun getValidOperationId(operationId: String?): String? { return operationId?.let { if (it.first().isDigit()) { - operationId.substring(1).toCamelCase() + "operation$operationId".toCamelCase() } else { operationId.toCamelCase() } diff --git a/codegen/src/test/kotlin/apifi/parser/PathsParserTest.kt b/codegen/src/test/kotlin/apifi/parser/PathsParserTest.kt index cd7d068..5c6838a 100644 --- a/codegen/src/test/kotlin/apifi/parser/PathsParserTest.kt +++ b/codegen/src/test/kotlin/apifi/parser/PathsParserTest.kt @@ -64,7 +64,7 @@ class PathsParserTest : DescribeSpec({ val path = PathsParser.parse(openApi.paths).result[0] val operation = path.operations?.first() operation shouldNotBe null - operation?.name shouldBe "listAllPets" + operation?.name shouldBe "operation1ListAllPets" } } })