Skip to content

Commit

Permalink
fix config for routing, option to disabled syntax highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
SMILEY4 committed May 24, 2024
1 parent 54c8c3a commit b81cfb5
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.smiley4.ktorswaggerui.data

enum class SwaggerUiSyntaxHighlight(val value: String) {
DISABLED("disabled"),
AGATE("agate"),
ARTA("arta"),
MONOKAI("monokai"),
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, String>()

fun setAll(specs: Map<String, String>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ 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.*
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) {
Expand All @@ -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)
}
}
}
Expand All @@ -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({
Expand Down

0 comments on commit b81cfb5

Please sign in to comment.