Skip to content

Commit

Permalink
Remove/inline operation from Route, and introduce Route naming.
Browse files Browse the repository at this point in the history
  • Loading branch information
nomisRev committed Jul 10, 2024
1 parent e29ec6f commit db2411e
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 73 deletions.
6 changes: 3 additions & 3 deletions generation/src/main/kotlin/io/github/nomisrev/openapi/APIs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,13 @@ private fun API.toImplementation(context: NamingContext? = null): TypeSpec {
context(OpenAPIContext, TypeSpecHolder.Builder<*>, MemberSpecHolder.Builder<*>)
private fun Route.addFunction(implemented: Boolean) {
val function =
FunSpec.builder(toParamName(Named(operation.operationId!!)))
FunSpec.builder(toParamName(Named(naming.name)))

Check warning on line 204 in generation/src/main/kotlin/io/github/nomisrev/openapi/APIs.kt

View check run for this annotation

Codecov / codecov/patch

generation/src/main/kotlin/io/github/nomisrev/openapi/APIs.kt#L204

Added line #L204 was not covered by tests
.addModifiers(KModifier.SUSPEND, if (implemented) KModifier.OVERRIDE else KModifier.ABSTRACT)
.addParameters(params(defaults = !implemented))
.addParameters(requestBody(defaults = !implemented))
.addParameter(configure(defaults = !implemented))
.apply {
operation.summary?.let { addKdoc(it) }
summary?.let { addKdoc(it) }
returnType()
if (implemented) {
addCode(
Expand Down Expand Up @@ -353,7 +353,7 @@ fun Route.returnType() {
}
returns(typeName)
} else {
val response = ClassName(`package`, "${operation.operationId}Response")
val response = ClassName(`package`, "${naming.name}Response")

Check warning on line 356 in generation/src/main/kotlin/io/github/nomisrev/openapi/APIs.kt

View check run for this annotation

Codecov / codecov/patch

generation/src/main/kotlin/io/github/nomisrev/openapi/APIs.kt#L356

Added line #L356 was not covered by tests
addType(
TypeSpec.interfaceBuilder(response)
.addModifiers(KModifier.SEALED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fun APIInterceptor.Companion.openAIStreaming(`package`: String): APIInterceptor
): TypeSpec.Builder {
val functions =
api.routes
.associateBy { it.operation.operationId!! }
.associateBy { it.naming.name }

Check warning on line 87 in generation/src/main/kotlin/io/github/nomisrev/openapi/OpenAIInterceptor.kt

View check run for this annotation

Codecov / codecov/patch

generation/src/main/kotlin/io/github/nomisrev/openapi/OpenAIInterceptor.kt#L87

Added line #L87 was not covered by tests
.mapNotNull { (key, route) ->
if (streamingOps.containsKey(key))
route.toStreamingFun(implemented = implemented, streamingOps[key]!!)
Expand All @@ -99,7 +99,7 @@ fun APIInterceptor.Companion.openAIStreaming(`package`: String): APIInterceptor

context(OpenAPIContext)
private fun Route.toStreamingFun(implemented: Boolean, returnType: ClassName): FunSpec =
FunSpec.builder(toParamName(Named(operation.operationId!! + "Stream")))
FunSpec.builder(toParamName(Named(naming.name + "Stream")))

Check warning on line 102 in generation/src/main/kotlin/io/github/nomisrev/openapi/OpenAIInterceptor.kt

View check run for this annotation

Codecov / codecov/patch

generation/src/main/kotlin/io/github/nomisrev/openapi/OpenAIInterceptor.kt#L102

Added line #L102 was not covered by tests
.addModifiers(
KModifier.SUSPEND,
if (implemented) KModifier.OVERRIDE else KModifier.ABSTRACT
Expand Down
13 changes: 12 additions & 1 deletion typed/src/commonMain/kotlin/io/github/nomisrev/openapi/Model.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,19 @@ import io.ktor.http.HttpMethod
import io.ktor.http.HttpStatusCode
import kotlinx.serialization.json.JsonElement

sealed interface RouteNaming {
val name: String

data class OperationId(override val name: String) : RouteNaming
// data class Path(
// val value: String,
// val operation: HttpMethod
// ): RouteNaming
}

data class Route(
val operation: Operation,
val naming: RouteNaming,
val summary: String?,
val path: String,
val method: HttpMethod,
val body: Bodies,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ private class OpenAPITransformer(private val openAPI: OpenAPI) {
.let(::listOfNotNull)

Route(
operation = operation,
naming = RouteNaming.OperationId(operation.operationId!!),
summary = operation.summary,
path = path,
method = method,
body = toRequestBody(operation, operation.requestBody?.get(), ::context),
Expand Down
102 changes: 36 additions & 66 deletions typed/src/commonTest/kotlin/io/github/nomisrev/openapi/RootTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,80 +14,50 @@ class RootTest {

@Test
fun operation() {
val operation =
Operation(
operationId = "echo",
responses =
Responses(
200 to
value(
Response(
content =
mapOf(
"application/json" to
MediaType(schema = value(Schema(type = Schema.Type.Basic.String)))
)
)
)
)
)
val actual =
testAPI
.copy(
paths =
mapOf(
"/echo" to
PathItem(
get =
Operation(
operationId = "echo",
responses =
Responses(
200 to
value(
Response(
content =
mapOf(
"application/json" to
MediaType(
schema = value(Schema(type = Schema.Type.Basic.String))
)
)
)
)
)
)
Operation(
operationId = "echo",
summary = "Echoes the input",
responses =
Responses(
200 to
value(
Response(
content =
mapOf(
"application/json" to
MediaType(schema = value(Schema(type = Schema.Type.Basic.String)))
)
)
)
)
)
.root("OpenAPI")
.toAPI("/echo")
val expected =
Root(
"OpenAPI",
emptyList(),
API(
"echo",
listOf(
API(
"echo",
listOf(
Route(
operation = operation,
path = "/echo",
method = HttpMethod.Get,
body = Route.Bodies(required = false, emptyMap(), emptyMap()),
input = emptyList(),
returnType =
Route.Returns(
HttpStatusCode.OK to
Route.ReturnType(Model.Primitive.string(null, null), emptyMap())
),
extensions = emptyMap(),
nested = listOf(Model.Primitive.string(null, null))
)
),
nested = emptyList()
Route(
naming = RouteNaming.OperationId("echo"),
summary = "Echoes the input",
path = "/echo",
method = HttpMethod.Get,
body = Route.Bodies(required = false, emptyMap(), emptyMap()),
input = emptyList(),
returnType =
Route.Returns(
HttpStatusCode.OK to
Route.ReturnType(Model.Primitive.string(null, null), emptyMap())
),
extensions = emptyMap(),
nested = listOf(Model.Primitive.string(null, null))
)
)
),
nested = emptyList()
)
assertEquals(expected, actual)
}
}

fun Operation.toAPI(path: String): API =
testAPI.copy(paths = mapOf(path to PathItem(get = this))).root("OpenAPI").endpoints.single()

0 comments on commit db2411e

Please sign in to comment.