All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ancientlightstudios.quarkus.kotlin.openapi.parser.ReferenceResolver.kt Maven / Gradle / Ivy

There is a newer version: 0.4.14
Show newest version
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 }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy