From 46acb3840cfde7525187d4d25a7705347bf4a8dc Mon Sep 17 00:00:00 2001 From: Frederic Nieto Date: Wed, 26 May 2021 13:34:25 +0200 Subject: [PATCH] made headers case insensitive, closes #96 --- build.gradle | 2 +- .../ktor/openapigen/annotations/mapping/OpenAPIName.kt | 6 +++++- .../parameters/handlers/ModularParameterHandler.kt | 2 +- .../com/papsign/ktor/openapigen/routing/RoutingTest.kt | 9 +++++---- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/build.gradle b/build.gradle index 22ee55f5a..27033df95 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { } group 'com.github.papsign' -version '0.2-beta.16-SNAPSHOT' +version '0.2-beta.17-SNAPSHOT' repositories { mavenCentral() diff --git a/src/main/kotlin/com/papsign/ktor/openapigen/annotations/mapping/OpenAPIName.kt b/src/main/kotlin/com/papsign/ktor/openapigen/annotations/mapping/OpenAPIName.kt index b7f9790e8..c0b3a66dc 100644 --- a/src/main/kotlin/com/papsign/ktor/openapigen/annotations/mapping/OpenAPIName.kt +++ b/src/main/kotlin/com/papsign/ktor/openapigen/annotations/mapping/OpenAPIName.kt @@ -1,5 +1,6 @@ package com.papsign.ktor.openapigen.annotations.mapping +import com.papsign.ktor.openapigen.annotations.parameters.HeaderParam import java.util.* import kotlin.reflect.KParameter import kotlin.reflect.full.findAnnotation @@ -10,7 +11,10 @@ annotation class OpenAPIName(val name: String) private val cache = Collections.synchronizedMap(HashMap()) val KParameter.openAPIName: String? - get() = cache.getOrPut(this) { findAnnotation()?.name ?: name } + get() = cache.getOrPut(this) { + val caseSensitiveName = findAnnotation()?.name ?: name + if (findAnnotation() != null) caseSensitiveName?.toLowerCase() else caseSensitiveName + } fun KParameter.remapOpenAPINames(map: Map): Map { val replace = this.openAPIName diff --git a/src/main/kotlin/com/papsign/ktor/openapigen/parameters/handlers/ModularParameterHandler.kt b/src/main/kotlin/com/papsign/ktor/openapigen/parameters/handlers/ModularParameterHandler.kt index 2cfd4f74b..7b6e9cce2 100644 --- a/src/main/kotlin/com/papsign/ktor/openapigen/parameters/handlers/ModularParameterHandler.kt +++ b/src/main/kotlin/com/papsign/ktor/openapigen/parameters/handlers/ModularParameterHandler.kt @@ -29,7 +29,7 @@ class ModularParameterHandler(val parsers: Map>, val c override fun parse(parameters: Parameters, headers: Headers): T { return constructor.callBy(parsers.mapValues { - val value = it.value.build(it.key.name.toString(), it.key.remapOpenAPINames(parameters.toMap() + headers.toMap())) + val value = it.value.build(it.key.name.toString(), it.key.remapOpenAPINames(parameters.toMap() + headers.toMap().mapKeys { it.key.toLowerCase() })) if (value != null || it.key.type.isMarkedNullable) { value } else { diff --git a/src/test/kotlin/com/papsign/ktor/openapigen/routing/RoutingTest.kt b/src/test/kotlin/com/papsign/ktor/openapigen/routing/RoutingTest.kt index 467f19988..c0d9b08f5 100644 --- a/src/test/kotlin/com/papsign/ktor/openapigen/routing/RoutingTest.kt +++ b/src/test/kotlin/com/papsign/ktor/openapigen/routing/RoutingTest.kt @@ -22,6 +22,7 @@ import kotlin.test.assertTrue class RoutingTest { data class TestHeaderParams(@HeaderParam("test param") val `Test-Header`: Long) + data class TestHeaderParams2(@HeaderParam("test param") val `test-header`: Long) data class TestBodyParams(val xyz: Long) data class TestResponse(val msg: String) @@ -43,7 +44,7 @@ class RoutingTest { handleRequest(HttpMethod.Post, route) { addHeader(HttpHeaders.ContentType, "application/json") addHeader(HttpHeaders.Accept, "application/json") - addHeader("Test-Header", "123") + addHeader("test-header", "123") setBody("{\"xyz\":456}") }.apply { assertTrue { response.contentType().match("application/json") } @@ -64,7 +65,7 @@ class RoutingTest { apiRouting { (this.ktorRoute as Routing).trace { println(it.buildText()) } route(route) { - get { params -> + get { params -> respond(TestResponse("$params")) } } @@ -76,7 +77,7 @@ class RoutingTest { }.apply { assertTrue { response.contentType().match("application/json") } assertEquals( - "{\"msg\":\"${TestHeaderParams(123)}\"}", + "{\"msg\":\"${TestHeaderParams2(123)}\"}", response.content ) } @@ -136,4 +137,4 @@ class RoutingTest { } } } -} \ No newline at end of file +}