com.ancientlightstudios.quarkus.kotlin.openapi.parser.ReferenceResolver.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.openapi.OpenApiVersion
import com.ancientlightstudios.quarkus.kotlin.openapi.models.openapi.RequestBody
import com.ancientlightstudios.quarkus.kotlin.openapi.models.openapi.ResponseBody
import com.ancientlightstudios.quarkus.kotlin.openapi.models.openapi.ResponseHeader
import com.ancientlightstudios.quarkus.kotlin.openapi.models.openapi.parameter.Parameter
import com.ancientlightstudios.quarkus.kotlin.openapi.models.openapi.schema.Schema
import com.fasterxml.jackson.databind.JsonNode
class ReferenceResolver(private val openApiVersion: OpenApiVersion, private val root: JsonNode) {
private val schemas = mutableMapOf>()
private val parameters = mutableMapOf>()
private val requestBodies = mutableMapOf>()
private val responseBodies = mutableMapOf>()
private val headers = mutableMapOf>()
private fun resolveReference(reference: String, message: () -> String): ParseContext {
val node = root.resolvePointer(reference) ?: throw IllegalArgumentException(message())
return ParseContext(openApiVersion, node, reference, this)
}
private fun String.targetName() = substringAfterLast('/')
fun resolveSchema(reference: String) = schemas.getOrPut(reference) {
resolveReference(reference) { "Unresolvable schema reference $reference" }
.parseAsSchema()
.let { reference.targetName() to it }
}
fun resolveParameter(reference: String) = parameters.getOrPut(reference) {
resolveReference(reference) { "Unresolvable parameter reference $reference" }
.parseAsRequestParameter()
.let { reference.targetName() to it }
}
fun resolveRequestBody(reference: String) = requestBodies.getOrPut(reference) {
resolveReference(reference) { "Unresolvable request body reference $reference" }
.parseAsRequestBody()
.let { reference.targetName() to it }
}
fun resolveResponseBody(reference: String) = responseBodies.getOrPut(reference) {
resolveReference(reference) { "Unresolvable response body reference $reference" }
.parseAsResponseBody()
.let { reference.targetName() to it }
}
fun resolveResponseHeader(reference: String) = headers.getOrPut(reference) {
resolveReference(reference) { "Unresolvable response header reference $reference" }
.parseAsResponseHeader()
.let { reference.targetName() to it }
}
}