From d935fa6ed62a02d2c3e41c33ea71978f029f265f Mon Sep 17 00:00:00 2001 From: Lukas Ruegner Date: Mon, 11 Dec 2023 23:07:09 +0100 Subject: [PATCH] fix invalid name property for security scheme objects --- .../builder/openapi/SecuritySchemesBuilder.kt | 5 ++-- .../ktorswaggerui/data/SecuritySchemeData.kt | 6 +++-- .../dsl/OpenApiSecurityScheme.kt | 12 ++++++--- .../openapi/SecuritySchemesBuilderTest.kt | 26 +++++++++++++------ 4 files changed, 33 insertions(+), 16 deletions(-) diff --git a/src/main/kotlin/io/github/smiley4/ktorswaggerui/builder/openapi/SecuritySchemesBuilder.kt b/src/main/kotlin/io/github/smiley4/ktorswaggerui/builder/openapi/SecuritySchemesBuilder.kt index 9216f0fe..086aa89e 100644 --- a/src/main/kotlin/io/github/smiley4/ktorswaggerui/builder/openapi/SecuritySchemesBuilder.kt +++ b/src/main/kotlin/io/github/smiley4/ktorswaggerui/builder/openapi/SecuritySchemesBuilder.kt @@ -1,5 +1,6 @@ package io.github.smiley4.ktorswaggerui.builder.openapi +import io.github.smiley4.ktorswaggerui.data.AuthType import io.github.smiley4.ktorswaggerui.data.SecuritySchemeData import io.swagger.v3.oas.models.security.SecurityScheme @@ -10,7 +11,7 @@ class SecuritySchemesBuilder( fun build(securitySchemes: List): Map { return mutableMapOf().apply { securitySchemes.forEach { - put(it.name, build(it)) + put(it.schemeName, build(it)) } } } @@ -18,7 +19,7 @@ class SecuritySchemesBuilder( private fun build(securityScheme: SecuritySchemeData): SecurityScheme { return SecurityScheme().apply { description = securityScheme.description - name = securityScheme.name + name = if(securityScheme.type == AuthType.API_KEY) securityScheme.name ?: securityScheme.schemeName else null type = securityScheme.type?.swaggerType `in` = securityScheme.location?.swaggerType scheme = securityScheme.scheme?.swaggerType diff --git a/src/main/kotlin/io/github/smiley4/ktorswaggerui/data/SecuritySchemeData.kt b/src/main/kotlin/io/github/smiley4/ktorswaggerui/data/SecuritySchemeData.kt index 1e46bcaf..85378662 100644 --- a/src/main/kotlin/io/github/smiley4/ktorswaggerui/data/SecuritySchemeData.kt +++ b/src/main/kotlin/io/github/smiley4/ktorswaggerui/data/SecuritySchemeData.kt @@ -1,8 +1,9 @@ package io.github.smiley4.ktorswaggerui.data data class SecuritySchemeData( - val name: String, + val schemeName: String, val type: AuthType?, + val name: String?, val location: AuthKeyLocation?, val scheme: AuthScheme?, val bearerFormat: String?, @@ -13,8 +14,9 @@ data class SecuritySchemeData( companion object { val DEFAULT = SecuritySchemeData( - name = "", + schemeName = "", type = null, + name = null, location = null, scheme = null, bearerFormat = null, diff --git a/src/main/kotlin/io/github/smiley4/ktorswaggerui/dsl/OpenApiSecurityScheme.kt b/src/main/kotlin/io/github/smiley4/ktorswaggerui/dsl/OpenApiSecurityScheme.kt index 4a6e265d..f3b4c297 100644 --- a/src/main/kotlin/io/github/smiley4/ktorswaggerui/dsl/OpenApiSecurityScheme.kt +++ b/src/main/kotlin/io/github/smiley4/ktorswaggerui/dsl/OpenApiSecurityScheme.kt @@ -15,10 +15,9 @@ import io.github.smiley4.ktorswaggerui.data.SecuritySchemeData @OpenApiDslMarker class OpenApiSecurityScheme( /** - * The name of the header, query or cookie parameter to be used. - * Required for type [AuthType.API_KEY] + * The name of the security scheme. */ - val name: String + val schemeName: String ) { /** @@ -26,6 +25,10 @@ class OpenApiSecurityScheme( */ var type: AuthType? = null + /** + * The name scheme and of the header, query or cookie parameter to be used. + */ + var name: String? = null /** * The location of the API key (OpenAPI 'in'). @@ -73,8 +76,9 @@ class OpenApiSecurityScheme( fun build(base: SecuritySchemeData) = SecuritySchemeData( - name = name, + schemeName = schemeName, type = merge(base.type, type), + name = merge(base.name, name), location = merge(base.location, location), scheme = merge(base.scheme, scheme), bearerFormat = merge(base.bearerFormat, bearerFormat), diff --git a/src/test/kotlin/io/github/smiley4/ktorswaggerui/tests/openapi/SecuritySchemesBuilderTest.kt b/src/test/kotlin/io/github/smiley4/ktorswaggerui/tests/openapi/SecuritySchemesBuilderTest.kt index 96de733c..14a47efb 100644 --- a/src/test/kotlin/io/github/smiley4/ktorswaggerui/tests/openapi/SecuritySchemesBuilderTest.kt +++ b/src/test/kotlin/io/github/smiley4/ktorswaggerui/tests/openapi/SecuritySchemesBuilderTest.kt @@ -28,7 +28,7 @@ class SecuritySchemesBuilderTest : StringSpec({ schemes["TestAuth"]!!.also { scheme -> scheme.type shouldBe null scheme.description shouldBe null - scheme.name shouldBe "TestAuth" + scheme.name shouldBe null scheme.`$ref` shouldBe null scheme.`in` shouldBe null scheme.scheme shouldBe null @@ -47,20 +47,30 @@ class SecuritySchemesBuilderTest : StringSpec({ scheme = AuthScheme.BASIC }, "TestAuth2" to { - type = AuthType.HTTP - scheme = AuthScheme.BASIC + type = AuthType.API_KEY + location = AuthKeyLocation.COOKIE + }, + "TestAuth3" to { + type = AuthType.API_KEY + location = AuthKeyLocation.QUERY + name = "myQueryParam" } )).also { schemes -> - schemes.keys shouldContainExactlyInAnyOrder listOf("TestAuth1", "TestAuth2") + schemes.keys shouldContainExactlyInAnyOrder listOf("TestAuth1", "TestAuth2", "TestAuth3") schemes["TestAuth1"]!!.also { scheme -> - scheme.name shouldBe "TestAuth1" + scheme.name shouldBe null scheme.type shouldBe SecurityScheme.Type.HTTP scheme.scheme shouldBe "Basic" } schemes["TestAuth2"]!!.also { scheme -> scheme.name shouldBe "TestAuth2" - scheme.type shouldBe SecurityScheme.Type.HTTP - scheme.scheme shouldBe "Basic" + scheme.type shouldBe SecurityScheme.Type.APIKEY + scheme.scheme shouldBe null + } + schemes["TestAuth3"]!!.also { scheme -> + scheme.name shouldBe "myQueryParam" + scheme.type shouldBe SecurityScheme.Type.APIKEY + scheme.scheme shouldBe null } } } @@ -114,7 +124,7 @@ class SecuritySchemesBuilderTest : StringSpec({ })).also { schemes -> schemes.keys shouldContainExactlyInAnyOrder listOf("TestAuth") schemes["TestAuth"]!!.also { scheme -> - scheme.name shouldBe "TestAuth" + scheme.name shouldBe null scheme.type shouldBe SecurityScheme.Type.HTTP scheme.`in` shouldBe SecurityScheme.In.COOKIE scheme.scheme shouldBe "Basic"