diff options
Diffstat (limited to 'server/swagger/src')
-rw-r--r-- | server/swagger/src/main/kotlin/moe/nea/ledger/server/core/api/OpenApiModel.kt | 19 | ||||
-rw-r--r-- | server/swagger/src/main/kotlin/moe/nea/ledger/server/core/api/docs.kt | 16 |
2 files changed, 34 insertions, 1 deletions
diff --git a/server/swagger/src/main/kotlin/moe/nea/ledger/server/core/api/OpenApiModel.kt b/server/swagger/src/main/kotlin/moe/nea/ledger/server/core/api/OpenApiModel.kt index ebee503..8392c5c 100644 --- a/server/swagger/src/main/kotlin/moe/nea/ledger/server/core/api/OpenApiModel.kt +++ b/server/swagger/src/main/kotlin/moe/nea/ledger/server/core/api/OpenApiModel.kt @@ -3,6 +3,7 @@ package moe.nea.ledger.server.core.api import io.ktor.http.ContentType import io.ktor.http.HttpStatusCode import kotlinx.serialization.KSerializer +import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable import kotlinx.serialization.descriptors.PrimitiveKind import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor @@ -41,10 +42,26 @@ data class OpenApiOperation( val description: String, val operationId: String, val deprecated: Boolean, -// val parameters: List<Parameter>, + val parameters: List<OpenApiParameter>, val responses: Map<@Serializable(HttpStatusCodeIntAsString::class) HttpStatusCode, OpenApiResponse> ) +@Serializable +data class OpenApiParameter( + @SerialName("in") val location: ParameterLocation, + val name: String, + val description: String, + val schema: JsonSchema?, +) + +@Serializable +enum class ParameterLocation { + @SerialName("query") + QUERY, + @SerialName("path") + PATH, +} + object HttpStatusCodeIntAsString : KSerializer<HttpStatusCode> { override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("HttpStatusCodeIntAsString", PrimitiveKind.STRING) diff --git a/server/swagger/src/main/kotlin/moe/nea/ledger/server/core/api/docs.kt b/server/swagger/src/main/kotlin/moe/nea/ledger/server/core/api/docs.kt index 5d222ee..c1b550d 100644 --- a/server/swagger/src/main/kotlin/moe/nea/ledger/server/core/api/docs.kt +++ b/server/swagger/src/main/kotlin/moe/nea/ledger/server/core/api/docs.kt @@ -190,6 +190,7 @@ class DocumentationOperationContext(val route: DocumentationContext) { var deprecated: Boolean = false var operationId: String = "" val tags: MutableList<String> = mutableListOf() + val parameters: MutableList<OpenApiParameter> = mutableListOf() fun tag(vararg tag: String) { tags.addAll(tag) } @@ -198,6 +199,20 @@ class DocumentationOperationContext(val route: DocumentationContext) { tag.mapTo(tags) { it.intoTag() } } + inline fun <reified T : Any> queryParameter(name: String, description: String = "") { + parameter(ParameterLocation.QUERY, name, description, jsonSchema<T>()) + } + + fun parameter( + location: ParameterLocation, name: String, + description: String = "", schema: JsonSchema? = null + ) { + parameters.add(OpenApiParameter( + location, name, description, + schema + )) + } + fun intoJson(): OpenApiOperation { return OpenApiOperation( tags = tags.map { Tag(it) }, @@ -205,6 +220,7 @@ class DocumentationOperationContext(val route: DocumentationContext) { description = description, operationId = operationId, deprecated = deprecated, + parameters = parameters, responses = responses.mapValues { it.value.intoJson() } |