From b81cfb50cb70cd229ba721194e6fc228b7a105d3 Mon Sep 17 00:00:00 2001 From: Lukas Ruegner Date: Fri, 24 May 2024 22:59:39 +0200 Subject: [PATCH] fix config for routing, option to disabled syntax highlighting --- .../io/github/smiley4/ktorswaggerui/SwaggerPlugin.kt | 1 + .../ktorswaggerui/data/SwaggerUiSyntaxHighlight.kt | 1 + .../io/github/smiley4/ktorswaggerui/routing/ApiSpec.kt | 4 ++++ .../io/github/smiley4/ktorswaggerui/routing/routing.kt | 8 +++++--- 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/ktor-swagger-ui/src/main/kotlin/io/github/smiley4/ktorswaggerui/SwaggerPlugin.kt b/ktor-swagger-ui/src/main/kotlin/io/github/smiley4/ktorswaggerui/SwaggerPlugin.kt index 83d674dc..8c3dd7e6 100644 --- a/ktor-swagger-ui/src/main/kotlin/io/github/smiley4/ktorswaggerui/SwaggerPlugin.kt +++ b/ktor-swagger-ui/src/main/kotlin/io/github/smiley4/ktorswaggerui/SwaggerPlugin.kt @@ -64,6 +64,7 @@ val SwaggerUI = createApplicationPlugin(name = "SwaggerUI", createConfiguration try { val routes = routes(application, config) ApiSpec.setAll(buildOpenApiSpecs(config, routes)) + ApiSpec.swaggerUiConfig = config.swagger } catch (e: Exception) { logger.error("Error during application startup in swagger-ui-plugin", e) } diff --git a/ktor-swagger-ui/src/main/kotlin/io/github/smiley4/ktorswaggerui/data/SwaggerUiSyntaxHighlight.kt b/ktor-swagger-ui/src/main/kotlin/io/github/smiley4/ktorswaggerui/data/SwaggerUiSyntaxHighlight.kt index 3d38d957..f31ffcc4 100644 --- a/ktor-swagger-ui/src/main/kotlin/io/github/smiley4/ktorswaggerui/data/SwaggerUiSyntaxHighlight.kt +++ b/ktor-swagger-ui/src/main/kotlin/io/github/smiley4/ktorswaggerui/data/SwaggerUiSyntaxHighlight.kt @@ -1,6 +1,7 @@ package io.github.smiley4.ktorswaggerui.data enum class SwaggerUiSyntaxHighlight(val value: String) { + DISABLED("disabled"), AGATE("agate"), ARTA("arta"), MONOKAI("monokai"), diff --git a/ktor-swagger-ui/src/main/kotlin/io/github/smiley4/ktorswaggerui/routing/ApiSpec.kt b/ktor-swagger-ui/src/main/kotlin/io/github/smiley4/ktorswaggerui/routing/ApiSpec.kt index 3e0a8970..c3ff84e2 100644 --- a/ktor-swagger-ui/src/main/kotlin/io/github/smiley4/ktorswaggerui/routing/ApiSpec.kt +++ b/ktor-swagger-ui/src/main/kotlin/io/github/smiley4/ktorswaggerui/routing/ApiSpec.kt @@ -1,7 +1,11 @@ package io.github.smiley4.ktorswaggerui.routing +import io.github.smiley4.ktorswaggerui.data.SwaggerUIData + object ApiSpec { + var swaggerUiConfig: SwaggerUIData = SwaggerUIData.DEFAULT + private val apiSpecs = mutableMapOf() fun setAll(specs: Map) { diff --git a/ktor-swagger-ui/src/main/kotlin/io/github/smiley4/ktorswaggerui/routing/routing.kt b/ktor-swagger-ui/src/main/kotlin/io/github/smiley4/ktorswaggerui/routing/routing.kt index a2dc7894..3e4bf2ac 100644 --- a/ktor-swagger-ui/src/main/kotlin/io/github/smiley4/ktorswaggerui/routing/routing.kt +++ b/ktor-swagger-ui/src/main/kotlin/io/github/smiley4/ktorswaggerui/routing/routing.kt @@ -4,6 +4,7 @@ import io.github.smiley4.ktorswaggerui.SWAGGER_UI_WEBJARS_VERSION import io.github.smiley4.ktorswaggerui.SwaggerUI import io.github.smiley4.ktorswaggerui.data.SwaggerUIData import io.github.smiley4.ktorswaggerui.data.SwaggerUiSort +import io.github.smiley4.ktorswaggerui.data.SwaggerUiSyntaxHighlight import io.github.smiley4.ktorswaggerui.dsl.config.PluginConfigDsl import io.github.smiley4.ktorswaggerui.dsl.routing.route import io.ktor.http.* @@ -11,7 +12,6 @@ import io.ktor.server.application.* import io.ktor.server.request.* import io.ktor.server.response.* import io.ktor.server.routing.* -import mu.KotlinLogging fun Route.openApiSpec(specId: String = PluginConfigDsl.DEFAULT_SPEC_ID) { @@ -31,7 +31,7 @@ fun Route.swaggerUI(apiUrl: String) { serveStaticResource(call.parameters["filename"]!!, SWAGGER_UI_WEBJARS_VERSION, call) } get("swagger-initializer.js") { - serveSwaggerInitializer(call, SwaggerUIData.DEFAULT, apiUrl) + serveSwaggerInitializer(call, ApiSpec.swaggerUiConfig, apiUrl) } } } @@ -44,7 +44,9 @@ private suspend fun serveSwaggerInitializer(call: ApplicationCall, swaggerUiConf val propSort = "operationsSorter: " + if (swaggerUiConfig.sort == SwaggerUiSort.NONE) "undefined" else "\"${swaggerUiConfig.sort.value}\"" - val propSyntaxHighlight = "syntaxHighlight: { theme: \"${swaggerUiConfig.syntaxHighlight.value}\" }" + val propSyntaxHighlight = "syntaxHighlight: " + + if(swaggerUiConfig.syntaxHighlight == SwaggerUiSyntaxHighlight.DISABLED) "false" + else "{ theme: \"${swaggerUiConfig.syntaxHighlight.value}\" }" val content = """ window.onload = function() { window.ui = SwaggerUIBundle({