fix: serialization of api key auth location (#261)

This commit is contained in:
Florian Mutter
2022-06-07 23:08:04 +02:00
committed by GitHub
parent 37b8dfffd1
commit 9e8b35d26d
4 changed files with 36 additions and 8 deletions

View File

@ -5,6 +5,7 @@
### Added ### Added
### Changed ### Changed
- Fix serialization for api key location in api key auth configuration
### Remove ### Remove

View File

@ -18,6 +18,10 @@ sourdough {
dependencies { dependencies {
implementation(group = "org.jetbrains.kotlinx", "kotlinx-serialization-json", version = "1.3.2") implementation(group = "org.jetbrains.kotlinx", "kotlinx-serialization-json", version = "1.3.2")
// TESTING
testImplementation(testFixtures(projects.kompendiumCore))
} }
testing { testing {

View File

@ -1,20 +1,18 @@
package io.bkbn.kompendium.oas.security package io.bkbn.kompendium.oas.security
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import java.util.Locale
// TODO... is there even an official ktor api auth mechanism?? // TODO... is there even an official ktor api auth mechanism??
@Serializable @Serializable
@Suppress("UnusedPrivateMember") class ApiKeyAuth private constructor(val `in`: String, val name: String) : SecuritySchema {
class ApiKeyAuth(val `in`: ApiKeyLocation, val name: String) : SecuritySchema {
val type: String = "apiKey" val type: String = "apiKey"
enum class ApiKeyLocation { constructor(location: ApiKeyLocation, name: String) : this(location.value, name)
HEADER,
QUERY,
COOKIE;
override fun toString(): String = name.lowercase(Locale.getDefault()) enum class ApiKeyLocation(val value: String) {
HEADER("header"),
QUERY("query"),
COOKIE("cookie");
} }
} }

View File

@ -0,0 +1,25 @@
package io.bkbn.kompendium.oas.security
import io.kotest.core.spec.style.DescribeSpec
import io.kotest.matchers.shouldBe
import kotlinx.serialization.json.Json
private val json = Json { encodeDefaults = true }
class ApiKeyAuthTest : DescribeSpec({
describe("ApiKeyAuth") {
it("should produce correct json") {
mapOf(
ApiKeyAuth.ApiKeyLocation.HEADER to "header",
ApiKeyAuth.ApiKeyLocation.COOKIE to "cookie",
ApiKeyAuth.ApiKeyLocation.QUERY to "query",
).forEach {
val example = ApiKeyAuth(it.key, "test-name")
val json = json.encodeToString(ApiKeyAuth.serializer(), example)
json.shouldBe("""{"in":"${it.value}","name":"test-name","type":"apiKey"}""")
}
}
}
})