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

com.ancientlightstudios.quarkus.kotlin.openapi.parser.RequestBuilder.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.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]() }
        }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy