Skip to content

Commit 60f21e7

Browse files
authored
Merge pull request #115 from Szer/security-scheme-name
Added support of name for cookies and query parameters in SecurityScheme
2 parents 19e2b92 + f969b6b commit 60f21e7

File tree

6 files changed

+34
-18
lines changed

6 files changed

+34
-18
lines changed

src/main/kotlin/com/papsign/ktor/openapigen/model/security/SecurityModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import com.papsign.ktor.openapigen.model.Described
99
class SecurityModel : MutableMap<String, List<*>> by mutableMapOf(), DataModel {
1010

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

1515
fun <T> set(scheme: SecuritySchemeModel<T>) where T: Enum<T>, T: Described {

src/main/kotlin/com/papsign/ktor/openapigen/model/security/SecuritySchemeModel.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import kotlin.reflect.full.memberProperties
99

1010
data class SecuritySchemeModel<TScope> constructor(
1111
val type: SecuritySchemeType,
12-
val name: String,
12+
val referenceName: String,
13+
val name: String? = null,
1314
val `in`: APIKeyLocation? = null,
1415
val scheme: HttpSecurityScheme? = null,
1516
val bearerFormat: String? = null,
@@ -20,6 +21,6 @@ data class SecuritySchemeModel<TScope> constructor(
2021
override fun serialize(): Map<String, Any?> {
2122
return this::class.memberProperties.associateBy { it.name }.mapValues<String, KProperty1<out SecuritySchemeModel<TScope>, Any?>, Any?> { (_, prop) ->
2223
convertToValue((prop as KProperty1<DataModel, *>).get(this))
23-
}.filter { it.key != "name" }.cleanEmptyValues()
24+
}.filter { it.key != "referenceName" }.cleanEmptyValues()
2425
}
2526
}

src/main/kotlin/com/papsign/ktor/openapigen/modules/handlers/AuthHandler.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ object AuthHandler: OperationModule {
1414
val authHandlers = provider.ofType<AuthProvider<*>>()
1515
val security = authHandlers.flatMap { it.security }.distinct()
1616
operation.security = security.map { SecurityModel().also { sec ->
17-
it.forEach { sec[it.scheme.name] = it.requirements }
17+
it.forEach { sec[it.scheme.referenceName] = it.requirements }
1818
} }
19-
apiGen.api.components.securitySchemes.putAll(security.flatMap { it.map { it.scheme } }.associateBy { it.name })
19+
apiGen.api.components.securitySchemes.putAll(security.flatMap { it.map { it.scheme } }.associateBy { it.referenceName })
2020
}
2121

2222
}

src/test/kotlin/JwtAuthDocumentationGenerationTest.kt

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,28 @@ internal class JwtAuthDocumentationGenerationTest {
1818
assertEquals(HttpStatusCode.OK, response.status())
1919
assertTrue(
2020
response.content!!.contains(
21-
"\"securitySchemes\" : {\n" +
22-
" \"jwtAuth\" : {\n" +
23-
" \"bearerFormat\" : \"JWT\",\n" +
24-
" \"scheme\" : \"bearer\",\n" +
25-
" \"type\" : \"http\"\n" +
26-
" }\n" +
27-
" }"
21+
""""securitySchemes" : {
22+
"ThisIsSchemeName" : {
23+
"in" : "cookie",
24+
"name" : "ThisIsCookieName",
25+
"type" : "apiKey"
26+
},
27+
"jwtAuth" : {
28+
"bearerFormat" : "JWT",
29+
"scheme" : "bearer",
30+
"type" : "http"
31+
}
32+
}"""
2833
)
2934
)
3035
assertTrue(
3136
response.content!!.contains(
32-
"\"security\" : [ {\n" +
33-
" \"jwtAuth\" : [ ]\n" +
34-
" } ]"
37+
""""security" : [ {
38+
"jwtAuth" : [ ],
39+
"ThisIsSchemeName" : [ ]
40+
}"""
3541
)
3642
)
3743
}
3844
}
39-
}
45+
}

src/test/kotlin/TestServerWithJwtAuth.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import com.papsign.ktor.openapigen.annotations.Response
1313
import com.papsign.ktor.openapigen.annotations.parameters.PathParam
1414
import com.papsign.ktor.openapigen.annotations.properties.description.Description
1515
import com.papsign.ktor.openapigen.model.Described
16+
import com.papsign.ktor.openapigen.model.security.APIKeyLocation
1617
import com.papsign.ktor.openapigen.model.security.HttpSecurityScheme
1718
import com.papsign.ktor.openapigen.model.security.SecuritySchemeModel
1819
import com.papsign.ktor.openapigen.model.security.SecuritySchemeType
@@ -165,7 +166,15 @@ object TestServerWithJwtAuth {
165166
SecuritySchemeType.http,
166167
scheme = HttpSecurityScheme.bearer,
167168
bearerFormat = "JWT",
168-
name = "jwtAuth"
169+
referenceName = "jwtAuth",
170+
), emptyList<Scopes>()
171+
),
172+
AuthProvider.Security(
173+
SecuritySchemeModel(
174+
SecuritySchemeType.apiKey,
175+
`in` = APIKeyLocation.cookie,
176+
name = "ThisIsCookieName",
177+
referenceName = "ThisIsSchemeName",
169178
), emptyList<Scopes>()
170179
)
171180
))

src/test/kotlin/com/papsign/ktor/openapigen/routing/GenericRoutesTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ class GenericRoutesTest {
234234
listOf(
235235
AuthProvider.Security(
236236
SecuritySchemeModel(
237-
name = "basicAuth",
237+
referenceName = "basicAuth",
238238
type = SecuritySchemeType.http,
239239
scheme = HttpSecurityScheme.basic
240240
), emptyList<Scopes>()

0 commit comments

Comments
 (0)