diff --git a/build.gradle.kts b/build.gradle.kts index 576eb9e..825c776 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -6,7 +6,7 @@ plugins { } group = "io.github.smiley4" -version = "0.5.0" +version = "0.5.1" repositories { mavenCentral() @@ -34,6 +34,7 @@ dependencies { testImplementation("io.ktor:ktor-server-netty-jvm:$ktorVersion") testImplementation("io.ktor:ktor-server-content-negotiation:$ktorVersion") testImplementation("io.ktor:ktor-serialization-jackson:$ktorVersion") + testImplementation("io.ktor:ktor-serialization-kotlinx-json:$ktorVersion") testImplementation("io.ktor:ktor-server-auth:$ktorVersion") testImplementation("io.ktor:ktor-server-call-logging:$ktorVersion") testImplementation("io.ktor:ktor-server-test-host:$ktorVersion") diff --git a/src/main/kotlin/io/github/smiley4/ktorswaggerui/SwaggerPlugin.kt b/src/main/kotlin/io/github/smiley4/ktorswaggerui/SwaggerPlugin.kt index 5767a2c..4bf6559 100644 --- a/src/main/kotlin/io/github/smiley4/ktorswaggerui/SwaggerPlugin.kt +++ b/src/main/kotlin/io/github/smiley4/ktorswaggerui/SwaggerPlugin.kt @@ -53,9 +53,6 @@ private fun getBuilder(): ApiSpecBuilder { return ApiSpecBuilder( OApiInfoBuilder(), OApiServersBuilder(), - OApiSecuritySchemesBuilder( - OApiOAuthFlowsBuilder() - ), OApiTagsBuilder(), OApiPathsBuilder( RouteCollector(), @@ -87,7 +84,10 @@ private fun getBuilder(): ApiSpecBuilder { ), ), OApiComponentsBuilder( - OApiExampleBuilder() + OApiExampleBuilder(), + OApiSecuritySchemesBuilder( + OApiOAuthFlowsBuilder() + ) ), ) } diff --git a/src/main/kotlin/io/github/smiley4/ktorswaggerui/specbuilder/ApiSpecBuilder.kt b/src/main/kotlin/io/github/smiley4/ktorswaggerui/specbuilder/ApiSpecBuilder.kt index 6e12bd9..9e0111e 100644 --- a/src/main/kotlin/io/github/smiley4/ktorswaggerui/specbuilder/ApiSpecBuilder.kt +++ b/src/main/kotlin/io/github/smiley4/ktorswaggerui/specbuilder/ApiSpecBuilder.kt @@ -12,7 +12,6 @@ import io.swagger.v3.oas.models.OpenAPI class ApiSpecBuilder( private val infoBuilder: OApiInfoBuilder, private val serversBuilder: OApiServersBuilder, - private val securitySchemesBuilder: OApiSecuritySchemesBuilder, private val tagsBuilder: OApiTagsBuilder, private val pathsBuilder: OApiPathsBuilder, private val componentsBuilder: OApiComponentsBuilder @@ -26,14 +25,9 @@ class ApiSpecBuilder( val openAPI = OpenAPI().apply { info = infoBuilder.build(config.getInfo()) servers = serversBuilder.build(config.getServers()) - if (config.getSecuritySchemes().isNotEmpty()) { - components = Components().apply { - securitySchemes = securitySchemesBuilder.build(config.getSecuritySchemes()) - } - } tags = tagsBuilder.build(config.getTags()) paths = pathsBuilder.build(config, application, componentCtx) - components = componentsBuilder.build(componentCtx) + components = componentsBuilder.build(componentCtx, config.getSecuritySchemes()) } return Json.pretty(openAPI) } diff --git a/src/main/kotlin/io/github/smiley4/ktorswaggerui/specbuilder/OApiComponentsBuilder.kt b/src/main/kotlin/io/github/smiley4/ktorswaggerui/specbuilder/OApiComponentsBuilder.kt index 433a528..d8bdae1 100644 --- a/src/main/kotlin/io/github/smiley4/ktorswaggerui/specbuilder/OApiComponentsBuilder.kt +++ b/src/main/kotlin/io/github/smiley4/ktorswaggerui/specbuilder/OApiComponentsBuilder.kt @@ -1,20 +1,25 @@ package io.github.smiley4.ktorswaggerui.specbuilder +import io.github.smiley4.ktorswaggerui.dsl.OpenApiSecurityScheme import io.swagger.v3.oas.models.Components /** * Builder for the OpenAPI Components Object */ class OApiComponentsBuilder( - private val exampleBuilder: OApiExampleBuilder + private val exampleBuilder: OApiExampleBuilder, + private val securitySchemesBuilder: OApiSecuritySchemesBuilder ) { - fun build(ctx: ComponentsContext): Components { + fun build(ctx: ComponentsContext, securitySchemes: List): Components { return Components().apply { schemas = ctx.schemas examples = ctx.examples.mapValues { exampleBuilder.build("", it.value, ComponentsContext.NOOP) } + if (securitySchemes.isNotEmpty()) { + this.securitySchemes = securitySchemesBuilder.build(securitySchemes) + } } } diff --git a/src/test/kotlin/io/github/smiley4/ktorswaggerui/tests/BuilderUtils.kt b/src/test/kotlin/io/github/smiley4/ktorswaggerui/tests/BuilderUtils.kt index a2c5963..2e4a4c8 100644 --- a/src/test/kotlin/io/github/smiley4/ktorswaggerui/tests/BuilderUtils.kt +++ b/src/test/kotlin/io/github/smiley4/ktorswaggerui/tests/BuilderUtils.kt @@ -22,9 +22,6 @@ fun getApiSpecBuilder(): ApiSpecBuilder { return ApiSpecBuilder( OApiInfoBuilder(), OApiServersBuilder(), - OApiSecuritySchemesBuilder( - OApiOAuthFlowsBuilder() - ), OApiTagsBuilder(), OApiPathsBuilder( RouteCollector(), @@ -56,7 +53,10 @@ fun getApiSpecBuilder(): ApiSpecBuilder { ), ), OApiComponentsBuilder( - OApiExampleBuilder() + OApiExampleBuilder(), + OApiSecuritySchemesBuilder( + OApiOAuthFlowsBuilder() + ), ), ) } @@ -68,7 +68,10 @@ fun getOApiInfoBuilder(): OApiInfoBuilder { fun getOApiComponentsBuilder(): OApiComponentsBuilder { return OApiComponentsBuilder( - OApiExampleBuilder() + OApiExampleBuilder(), + OApiSecuritySchemesBuilder( + OApiOAuthFlowsBuilder() + ) ) } diff --git a/src/test/kotlin/io/github/smiley4/ktorswaggerui/tests/ComponentsObjectTest.kt b/src/test/kotlin/io/github/smiley4/ktorswaggerui/tests/ComponentsObjectTest.kt index dd56fc3..b4a0063 100644 --- a/src/test/kotlin/io/github/smiley4/ktorswaggerui/tests/ComponentsObjectTest.kt +++ b/src/test/kotlin/io/github/smiley4/ktorswaggerui/tests/ComponentsObjectTest.kt @@ -201,7 +201,7 @@ class ComponentsObjectTest : StringSpec({ companion object { private fun buildComponentsObject(context: ComponentsContext): Components { - return getOApiComponentsBuilder().build(context) + return getOApiComponentsBuilder().build(context, listOf()) } private fun buildSchema(type: KClass<*>, context: ComponentsContext): Schema<*> {