diff --git a/codegen/src/main/kotlin/apifi/parser/PathsParser.kt b/codegen/src/main/kotlin/apifi/parser/PathsParser.kt index 95aaf3d..786f97c 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()) { + "operation$operationId".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..5c6838a 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 "operation1ListAllPets" + } } -}) \ No newline at end of file +})