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

de.codecentric.hikaku.converters.openapi.extractors.PathParameterExtractor.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.PathParameter
import io.swagger.v3.oas.models.OpenAPI
import io.swagger.v3.oas.models.parameters.PathParameter as OpenApiPathParameter
import io.swagger.v3.oas.models.parameters.Parameter as OpenApiParameter

internal class PathParameterExtractor(private val openApi: OpenAPI) {

    operator fun invoke(parameters: List?): Set {
        return extractInlinePathParameters(parameters).union(extractPathParametersFromComponents(parameters))
    }

    private fun extractInlinePathParameters(parameters: List?): Set {
        return parameters
                ?.filterIsInstance()
                ?.map { PathParameter(it.name) }
                .orEmpty()
                .toSet()
    }

    private fun extractPathParametersFromComponents(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` == "path" }
                ?.map { PathParameter(it?.name ?: "") }
                .orEmpty()
                .toSet()
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy