Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import com.papsign.ktor.openapigen.model.Described
class SecurityModel : MutableMap<String, List<*>> by mutableMapOf(), DataModel {

operator fun <T> set(scheme: SecuritySchemeModel<T>, requirements: List<T>) where T: Enum<T>, T: Described {
this[scheme.name] = requirements
this[scheme.referenceName] = requirements
}

fun <T> set(scheme: SecuritySchemeModel<T>) where T: Enum<T>, T: Described {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import kotlin.reflect.full.memberProperties

data class SecuritySchemeModel<TScope> constructor(
val type: SecuritySchemeType,
val name: String,
val referenceName: String,
val name: String? = null,
val `in`: APIKeyLocation? = null,
val scheme: HttpSecurityScheme? = null,
val bearerFormat: String? = null,
Expand All @@ -20,6 +21,6 @@ data class SecuritySchemeModel<TScope> constructor(
override fun serialize(): Map<String, Any?> {
return this::class.memberProperties.associateBy { it.name }.mapValues<String, KProperty1<out SecuritySchemeModel<TScope>, Any?>, Any?> { (_, prop) ->
convertToValue((prop as KProperty1<DataModel, *>).get(this))
}.filter { it.key != "name" }.cleanEmptyValues()
}.filter { it.key != "referenceName" }.cleanEmptyValues()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ object AuthHandler: OperationModule {
val authHandlers = provider.ofType<AuthProvider<*>>()
val security = authHandlers.flatMap { it.security }.distinct()
operation.security = security.map { SecurityModel().also { sec ->
it.forEach { sec[it.scheme.name] = it.requirements }
it.forEach { sec[it.scheme.referenceName] = it.requirements }
} }
apiGen.api.components.securitySchemes.putAll(security.flatMap { it.map { it.scheme } }.associateBy { it.name })
apiGen.api.components.securitySchemes.putAll(security.flatMap { it.map { it.scheme } }.associateBy { it.referenceName })
}

}
28 changes: 17 additions & 11 deletions src/test/kotlin/JwtAuthDocumentationGenerationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,28 @@ internal class JwtAuthDocumentationGenerationTest {
assertEquals(HttpStatusCode.OK, response.status())
assertTrue(
response.content!!.contains(
"\"securitySchemes\" : {\n" +
" \"jwtAuth\" : {\n" +
" \"bearerFormat\" : \"JWT\",\n" +
" \"scheme\" : \"bearer\",\n" +
" \"type\" : \"http\"\n" +
" }\n" +
" }"
""""securitySchemes" : {
"ThisIsSchemeName" : {
"in" : "cookie",
"name" : "ThisIsCookieName",
"type" : "apiKey"
},
"jwtAuth" : {
"bearerFormat" : "JWT",
"scheme" : "bearer",
"type" : "http"
}
}"""
)
)
assertTrue(
response.content!!.contains(
"\"security\" : [ {\n" +
" \"jwtAuth\" : [ ]\n" +
" } ]"
""""security" : [ {
"jwtAuth" : [ ],
"ThisIsSchemeName" : [ ]
}"""
)
)
}
}
}
}
11 changes: 10 additions & 1 deletion src/test/kotlin/TestServerWithJwtAuth.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.papsign.ktor.openapigen.annotations.Response
import com.papsign.ktor.openapigen.annotations.parameters.PathParam
import com.papsign.ktor.openapigen.annotations.properties.description.Description
import com.papsign.ktor.openapigen.model.Described
import com.papsign.ktor.openapigen.model.security.APIKeyLocation
import com.papsign.ktor.openapigen.model.security.HttpSecurityScheme
import com.papsign.ktor.openapigen.model.security.SecuritySchemeModel
import com.papsign.ktor.openapigen.model.security.SecuritySchemeType
Expand Down Expand Up @@ -165,7 +166,15 @@ object TestServerWithJwtAuth {
SecuritySchemeType.http,
scheme = HttpSecurityScheme.bearer,
bearerFormat = "JWT",
name = "jwtAuth"
referenceName = "jwtAuth",
), emptyList<Scopes>()
),
AuthProvider.Security(
SecuritySchemeModel(
SecuritySchemeType.apiKey,
`in` = APIKeyLocation.cookie,
name = "ThisIsCookieName",
referenceName = "ThisIsSchemeName",
), emptyList<Scopes>()
)
))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ class GenericRoutesTest {
listOf(
AuthProvider.Security(
SecuritySchemeModel(
name = "basicAuth",
referenceName = "basicAuth",
type = SecuritySchemeType.http,
scheme = HttpSecurityScheme.basic
), emptyList<Scopes>()
Expand Down