Skip to content

Commit

Permalink
made headers case insensitive, closes #96
Browse files Browse the repository at this point in the history
  • Loading branch information
Wicpar committed May 26, 2021
1 parent f7e7048 commit 46acb38
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group 'com.github.papsign'
version '0.2-beta.16-SNAPSHOT'
version '0.2-beta.17-SNAPSHOT'

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -10,7 +11,10 @@ annotation class OpenAPIName(val name: String)
private val cache = Collections.synchronizedMap(HashMap<KParameter, String?>())

val KParameter.openAPIName: String?
get() = cache.getOrPut(this) { findAnnotation<OpenAPIName>()?.name ?: name }
get() = cache.getOrPut(this) {
val caseSensitiveName = findAnnotation<OpenAPIName>()?.name ?: name
if (findAnnotation<HeaderParam>() != null) caseSensitiveName?.toLowerCase() else caseSensitiveName
}

fun <T> KParameter.remapOpenAPINames(map: Map<String, T>): Map<String, T> {
val replace = this.openAPIName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ModularParameterHandler<T>(val parsers: Map<KParameter, Builder<*>>, 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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") }
Expand All @@ -64,7 +65,7 @@ class RoutingTest {
apiRouting {
(this.ktorRoute as Routing).trace { println(it.buildText()) }
route(route) {
get<TestHeaderParams, TestResponse> { params ->
get<TestHeaderParams2, TestResponse> { params ->
respond(TestResponse("$params"))
}
}
Expand All @@ -76,7 +77,7 @@ class RoutingTest {
}.apply {
assertTrue { response.contentType().match("application/json") }
assertEquals(
"{\"msg\":\"${TestHeaderParams(123)}\"}",
"{\"msg\":\"${TestHeaderParams2(123)}\"}",
response.content
)
}
Expand Down Expand Up @@ -136,4 +137,4 @@ class RoutingTest {
}
}
}
}
}

0 comments on commit 46acb38

Please sign in to comment.