com.ancientlightstudios.quarkus.kotlin.openapi.parser.RequestBuilder.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of quarkus-kotlin-openapi-maven-plugin Show documentation
Show all versions of quarkus-kotlin-openapi-maven-plugin Show documentation
A Maven plugin to use the OpenAPI generator.
package com.ancientlightstudios.quarkus.kotlin.openapi.parser
import com.ancientlightstudios.quarkus.kotlin.openapi.models.hints.OriginPathHint.originPath
import com.ancientlightstudios.quarkus.kotlin.openapi.models.transformable.RequestMethod
import com.ancientlightstudios.quarkus.kotlin.openapi.models.transformable.ResponseCode
import com.ancientlightstudios.quarkus.kotlin.openapi.models.transformable.TransformableParameter
import com.ancientlightstudios.quarkus.kotlin.openapi.models.transformable.TransformableRequest
import com.fasterxml.jackson.databind.node.ObjectNode
class RequestBuilder(
private val path: String, private val method: RequestMethod,
private val defaultParameter: () -> List, private val node: ObjectNode
) {
fun ParseContext.build() = TransformableRequest(
path, method,
node.getTextOrNull("operationId"),
node.withArray("tags").map { it.asText() },
extractParameters(),
extractRequestBody(),
extractResponses()
).apply {
originPath = contextPath
}
private fun ParseContext.extractParameters(): List {
val localParameter = node
.withArray("parameters")
.mapIndexed { index, itemNode ->
contextFor(itemNode, "parameters", "$index").parseAsRequestParameter()
}
val localParameterNames = localParameter.map { it.name }.toSet()
// overwrite parameters in the default list if they are redefined here
return defaultParameter().filterNot { localParameterNames.contains(it.name) } + localParameter
}
private fun ParseContext.extractRequestBody() = node.get("requestBody")
?.let { contextFor(it, "requestBody").parseAsRequestBody() }
private fun ParseContext.extractResponses() = node.with("responses")
.propertiesAsList()
.map { (code, responseNode) ->
contextFor(responseNode, "responses", code).parseAsResponse(ResponseCode.fromString(code))
}
}
fun ParseContext.parseAsRequest(
path: String,
method: RequestMethod,
defaultParameter: () -> List
) =
contextNode.asObjectNode { "Json object expected for $contextPath" }
.let {
RequestBuilder(path, method, defaultParameter, it).run { [email protected]() }
}