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

codegen: applying kotlin naming rules to operationId param #49

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions codegen/src/main/kotlin/apifi/parser/PathsParser.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand All @@ -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())
}

/**
* 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()
niraj8 marked this conversation as resolved.
Show resolved Hide resolved
} else {
operationId.toCamelCase()
}
}
}
}
16 changes: 16 additions & 0 deletions codegen/src/test-res/parser/params/with-invalid-operationId.yml
Original file line number Diff line number Diff line change
@@ -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
11 changes: 10 additions & 1 deletion codegen/src/test/kotlin/apifi/parser/PathsParserTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<kotlin.String>", 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"
}
}
})
})