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

de.codecentric.hikaku.converters.openapi.extractors.HeaderParameterExtractor.kt Maven / Gradle / Ivy

Go to download

A library that tests if the implementation of a REST-API meets its specification. This module contains a converter for OpenAPI specifications.

There is a newer version: 3.3.0
Show newest version
package de.codecentric.hikaku.converters.openapi.extractors

import de.codecentric.hikaku.converters.openapi.extensions.referencedSchema
import de.codecentric.hikaku.endpoints.HeaderParameter
import io.swagger.v3.oas.models.OpenAPI
import io.swagger.v3.oas.models.parameters.Parameter as OpenApiParameter
import io.swagger.v3.oas.models.parameters.HeaderParameter as OpenApiHeaderParameter

internal class HeaderParameterExtractor(private val openApi: OpenAPI) {

    operator fun invoke(parameters: List?): Set {
        return extractInlineHeaderParameters(parameters).union(extractHeaderParametersFromComponents(parameters))
    }

    private fun extractInlineHeaderParameters(parameters: List?): Set {
        return parameters
                ?.filterIsInstance()
                ?.map { HeaderParameter(it.name, it.required) }
                .orEmpty()
                .toSet()
    }

    private fun extractHeaderParametersFromComponents(parameters: List?): Set {
        return parameters
                ?.filter { it.referencedSchema != null }
                ?.map {
                    Regex("#/components/parameters/(?.+)")
                            .find(it.referencedSchema)
                            ?.groups
                            ?.get("key")
                            ?.value
                }
                ?.map {
                    openApi.components
                            .parameters[it]
                }
                ?.filter { it?.`in` == "header" }
                ?.map { HeaderParameter(it?.name ?: "", it?.required ?: false) }
                .orEmpty()
                .toSet()
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy