Skip to content

Commit

Permalink
feat: handle root security requirements on root DSL
Browse files Browse the repository at this point in the history
  • Loading branch information
anthony-legay committed Oct 17, 2024
1 parent eb71e75 commit d456b6d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import io.swagger.v3.oas.models.Components
import io.swagger.v3.oas.models.ExternalDocumentation
import io.swagger.v3.oas.models.OpenAPI
import io.swagger.v3.oas.models.info.Info
import io.swagger.v3.oas.models.security.SecurityRequirement
import io.swagger.v3.oas.models.servers.Server

/**
Expand Down Expand Up @@ -52,6 +53,9 @@ interface RootDsl : InfoDsl, TagsDsl, PathsDsl {
@TegralDsl
infix fun String.server(server: ServerDsl.() -> Unit)

@TegralDsl
fun security(vararg name: String)

/**
* Description for additional external documentation for this API.
*/
Expand All @@ -76,7 +80,7 @@ class RootBuilder(
) : RootDsl, InfoDsl by infoBuilder, PathsDsl by paths, Buildable<OpenAPI> {
private val tags = mutableListOf<TagBuilder>()
private val servers = mutableListOf<Buildable<Server>>()

private val security = mutableListOf<SecurityRequirement>()
override var externalDocsDescription: String? = null
override var externalDocsUrl: String? = null

Expand All @@ -93,6 +97,10 @@ class RootBuilder(
servers.add(serverBuilder)
}

override fun security(vararg name: String) {
security.add(SecurityRequirement().apply { name.forEach(::addList) })
}

override fun build(): OpenAPI = OpenAPI().apply {
tags = this@RootBuilder.tags.map { it.build() }.ifEmpty { null }
// In case the info part is completely empty, output 'null' to avoid getting an empty, useless object.
Expand All @@ -114,7 +122,7 @@ class RootBuilder(
description = externalDocsDescription
}
}

security = this@RootBuilder.security.ifEmpty { null }
servers = this@RootBuilder.servers.map { it.build() }.ifEmpty { null }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import io.ktor.server.application.MissingApplicationPluginException
import io.ktor.server.testing.testApplication
import io.swagger.v3.oas.models.OpenAPI
import io.swagger.v3.oas.models.info.Info
import io.swagger.v3.oas.models.security.SecurityRequirement
import org.junit.jupiter.api.assertDoesNotThrow
import kotlin.test.Test
import kotlin.test.assertEquals
Expand Down Expand Up @@ -70,4 +71,26 @@ class PluginTest {
}
}
}

@Test
fun `Add security requirements on root`() {
testApplication {
environment { developmentMode = false } // HACK see KTOR-4729
install(TegralOpenApiKtor) {
security("ApiKeyAuth")
security("BearerAuth")
}

application {
val document = openApi.buildOpenApiDocument()
val expected = OpenAPI().apply {
security = listOf(
SecurityRequirement().addList("ApiKeyAuth"),
SecurityRequirement().addList("BearerAuth"),
)
}
assertEquals(expected, document)
}
}
}
}

0 comments on commit d456b6d

Please sign in to comment.