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

com.ancientlightstudios.quarkus.kotlin.openapi.transformer.RequestTransformer.kt Maven / Gradle / Ivy

There is a newer version: 0.4.14
Show newest version
package com.ancientlightstudios.quarkus.kotlin.openapi.transformer

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.ResponseCode
import com.ancientlightstudios.quarkus.kotlin.openapi.models.transformed.AdditionalInformation
import com.ancientlightstudios.quarkus.kotlin.openapi.models.transformed.Request
import com.ancientlightstudios.quarkus.kotlin.openapi.models.transformed.name.MethodName.Companion.methodName
import com.ancientlightstudios.quarkus.kotlin.openapi.models.openapi.Request as OpenApiRequest

class RequestTransformer(private val source: OpenApiRequest) {

    fun initializeSchemaRegistry(schemaCollector: SchemaCollector) {
        schemaCollector.registerSchema(source.body?.schema, FlowDirection.Up) { "${source.operationId} body" }
        source.parameters.forEach {
            ParameterTransformer(it).initializeSchemaRegistry(schemaCollector, source.operationId)
        }
        source.responses.forEach { (code, response) ->
            schemaCollector.registerSchema(
                response.schema,
                FlowDirection.Down
            ) { "${source.operationId} $code response" }
            response.headers.forEach { (name, header) ->
                schemaCollector.registerSchema(
                    header.schema,
                    FlowDirection.Down
                ) { "${source.operationId} $code $name header" }
            }
        }
    }

    fun transform(typeDefinitionRegistry: TypeDefinitionRegistry): Request {
        return Request(
            source.operationId.methodName(),
            source.path,
            source.method,
            source.parameters.map { ParameterTransformer(it).transform(typeDefinitionRegistry) },
            transformRequestBody(source.body, typeDefinitionRegistry),
            source.responses.map { transformResponse(it, typeDefinitionRegistry) },
            AdditionalInformation(source.description, source.deprecated)
        )
    }

    private fun transformRequestBody(body: RequestBody?, typeDefinitionRegistry: TypeDefinitionRegistry) = body?.let {
        typeDefinitionRegistry.getTypeDefinition(it.schema, FlowDirection.Up)
            .useAs(it.required)
    }

    private fun transformResponse(
        response: Pair,
        typeDefinitionRegistry: TypeDefinitionRegistry
    ) = response.first to response.second.schema?.let {
        typeDefinitionRegistry.getTypeDefinition(it, FlowDirection.Down).useAs(true)
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy