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

de.codecentric.hikaku.converters.spring.extensions.QueryParametersSpringExtension.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 spring-mvc implementations.

There is a newer version: 3.3.0
Show newest version
package de.codecentric.hikaku.converters.spring.extensions

import de.codecentric.hikaku.endpoints.QueryParameter
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.ValueConstants
import org.springframework.web.method.HandlerMethod
import kotlin.reflect.KParameter
import kotlin.reflect.jvm.kotlinFunction

internal fun HandlerMethod.hikakuQueryParameters(): Set {
    val method = this.method.kotlinFunction ?: return emptySet()

    return method.parameters
            .filter { it.annotations.filterIsInstance().any() }
            .map { extractQueryParameter(it) }
            .toSet()
}

private fun extractQueryParameter(it: KParameter): QueryParameter {
    val requestParam = it.annotations.find { it is RequestParam } as RequestParam
    val parameterName = extractQueryParameterName(requestParam, it)
    val isRequired = isQueryParameterRequired(requestParam)

    return QueryParameter(parameterName, isRequired)
}

private fun isQueryParameterRequired(requestParam: RequestParam): Boolean {
    if (requestParam.defaultValue == ValueConstants.DEFAULT_NONE) {
        return requestParam.required
    }

    return false
}

private fun extractQueryParameterName(requestParam: RequestParam, it: KParameter): String {
    if (requestParam.value.isNotBlank() && requestParam.name.isNotBlank()) {
        throw IllegalStateException("Both 'value' and 'name' attribute are provided for query parameter '${it.name}'. Only one is permitted.")
    }

    return when {
        requestParam.value.isNotBlank() -> requestParam.value
        requestParam.name.isNotBlank() -> requestParam.name
        else -> it.name ?: ""
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy