Skip to content

Commit

Permalink
fix invalid name property for security scheme objects
Browse files Browse the repository at this point in the history
  • Loading branch information
SMILEY4 committed Dec 11, 2023
1 parent 86f2597 commit d935fa6
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -10,15 +11,15 @@ class SecuritySchemesBuilder(
fun build(securitySchemes: List<SecuritySchemeData>): Map<String, SecurityScheme> {
return mutableMapOf<String, SecurityScheme>().apply {
securitySchemes.forEach {
put(it.name, build(it))
put(it.schemeName, build(it))
}
}
}

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
Expand Down
Original file line number Diff line number Diff line change
@@ -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?,
Expand All @@ -13,8 +14,9 @@ data class SecuritySchemeData(

companion object {
val DEFAULT = SecuritySchemeData(
name = "",
schemeName = "",
type = null,
name = null,
location = null,
scheme = null,
bearerFormat = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@ 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
) {

/**
* The type of the security scheme
*/
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').
Expand Down Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
}
}
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit d935fa6

Please sign in to comment.