fix: swagger ui bug (#177)
This commit is contained in:
@ -12,6 +12,11 @@
|
|||||||
|
|
||||||
## Released
|
## Released
|
||||||
|
|
||||||
|
## [2.0.3] - February 7th, 2022
|
||||||
|
### Changed
|
||||||
|
- Fixed swagger documentation bug
|
||||||
|
- Deprecated Swagger Webjar approach
|
||||||
|
|
||||||
## [2.0.2] - February 4th, 2022
|
## [2.0.2] - February 4th, 2022
|
||||||
### Added
|
### Added
|
||||||
- `@Referenced` annotation enabling support for recursive models
|
- `@Referenced` annotation enabling support for recursive models
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
# Kompendium
|
# Kompendium
|
||||||
project.version=2.0.2
|
project.version=2.0.3
|
||||||
# Kotlin
|
# Kotlin
|
||||||
kotlin.code.style=official
|
kotlin.code.style=official
|
||||||
# Gradle
|
# Gradle
|
||||||
|
@ -25,7 +25,7 @@ fun Routing.redoc(pageTitle: String = "Docs", specUrl: String = "/openapi.json")
|
|||||||
call.respondHtml {
|
call.respondHtml {
|
||||||
head {
|
head {
|
||||||
title {
|
title {
|
||||||
+"$pageTitle"
|
+pageTitle
|
||||||
}
|
}
|
||||||
meta {
|
meta {
|
||||||
charset = "utf-8"
|
charset = "utf-8"
|
||||||
|
@ -0,0 +1,81 @@
|
|||||||
|
package io.bkbn.kompendium.core.routes
|
||||||
|
|
||||||
|
import io.ktor.application.call
|
||||||
|
import io.ktor.html.respondHtml
|
||||||
|
import io.ktor.routing.Routing
|
||||||
|
import io.ktor.routing.get
|
||||||
|
import io.ktor.routing.route
|
||||||
|
import kotlinx.html.body
|
||||||
|
import kotlinx.html.div
|
||||||
|
import kotlinx.html.head
|
||||||
|
import kotlinx.html.id
|
||||||
|
import kotlinx.html.link
|
||||||
|
import kotlinx.html.meta
|
||||||
|
import kotlinx.html.script
|
||||||
|
import kotlinx.html.title
|
||||||
|
import kotlinx.html.unsafe
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides an out-of-the-box route to view docs using Swagger
|
||||||
|
* @param pageTitle Webpage title you wish to be displayed on your docs
|
||||||
|
* @param specUrl url to point Swagger to the OpenAPI json document
|
||||||
|
*/
|
||||||
|
fun Routing.swagger(pageTitle: String = "Docs", specUrl: String = "/openapi.json") {
|
||||||
|
route("/swagger-ui") {
|
||||||
|
get {
|
||||||
|
call.respondHtml {
|
||||||
|
head {
|
||||||
|
title {
|
||||||
|
+pageTitle
|
||||||
|
}
|
||||||
|
meta {
|
||||||
|
charset = "utf-8"
|
||||||
|
}
|
||||||
|
meta {
|
||||||
|
name = "viewport"
|
||||||
|
content = "width=device-width, initial-scale=1"
|
||||||
|
}
|
||||||
|
link {
|
||||||
|
href = "https://unpkg.com/swagger-ui-dist@3.12.1/swagger-ui.css"
|
||||||
|
rel = "stylesheet"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
body {
|
||||||
|
div {
|
||||||
|
id = "swagger-ui"
|
||||||
|
}
|
||||||
|
script {
|
||||||
|
src = "https://unpkg.com/swagger-ui-dist@3.12.1/swagger-ui-standalone-preset.js"
|
||||||
|
}
|
||||||
|
script {
|
||||||
|
src = "https://unpkg.com/swagger-ui-dist@3.12.1/swagger-ui-bundle.js"
|
||||||
|
}
|
||||||
|
unsafe {
|
||||||
|
+"""
|
||||||
|
<script>
|
||||||
|
|
||||||
|
window.onload = function () {
|
||||||
|
// Build a system
|
||||||
|
const ui = SwaggerUIBundle({
|
||||||
|
url: "$specUrl",
|
||||||
|
dom_id: '#swagger-ui',
|
||||||
|
deepLinking: true,
|
||||||
|
presets: [
|
||||||
|
SwaggerUIBundle.presets.apis,
|
||||||
|
SwaggerUIStandalonePreset
|
||||||
|
],
|
||||||
|
plugins: [
|
||||||
|
SwaggerUIBundle.plugins.DownloadUrl
|
||||||
|
],
|
||||||
|
layout: "StandaloneLayout",
|
||||||
|
})
|
||||||
|
window.ui = ui
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
""".trimIndent()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -14,7 +14,6 @@ dependencies {
|
|||||||
implementation(projects.kompendiumCore)
|
implementation(projects.kompendiumCore)
|
||||||
implementation(projects.kompendiumAuth)
|
implementation(projects.kompendiumAuth)
|
||||||
implementation(projects.kompendiumLocations)
|
implementation(projects.kompendiumLocations)
|
||||||
implementation(projects.kompendiumSwaggerUi)
|
|
||||||
|
|
||||||
// Ktor
|
// Ktor
|
||||||
val ktorVersion: String by project
|
val ktorVersion: String by project
|
||||||
|
@ -2,8 +2,8 @@ package io.bkbn.kompendium.playground
|
|||||||
|
|
||||||
import io.bkbn.kompendium.core.Kompendium
|
import io.bkbn.kompendium.core.Kompendium
|
||||||
import io.bkbn.kompendium.core.Notarized.notarizedGet
|
import io.bkbn.kompendium.core.Notarized.notarizedGet
|
||||||
|
import io.bkbn.kompendium.core.routes.swagger
|
||||||
import io.bkbn.kompendium.playground.util.Util
|
import io.bkbn.kompendium.playground.util.Util
|
||||||
import io.bkbn.kompendium.swagger.swaggerUI
|
|
||||||
import io.ktor.application.Application
|
import io.ktor.application.Application
|
||||||
import io.ktor.application.call
|
import io.ktor.application.call
|
||||||
import io.ktor.application.install
|
import io.ktor.application.install
|
||||||
@ -43,7 +43,7 @@ private fun Application.mainModule() {
|
|||||||
// Configures the routes for our API
|
// Configures the routes for our API
|
||||||
routing {
|
routing {
|
||||||
// This is all you need to do to add Swagger! Reachable at `/swagger-ui`
|
// This is all you need to do to add Swagger! Reachable at `/swagger-ui`
|
||||||
swaggerUI()
|
swagger()
|
||||||
// Kompendium infers the route path from the Ktor Route. This will show up as the root path `/`
|
// Kompendium infers the route path from the Ktor Route. This will show up as the root path `/`
|
||||||
notarizedGet(BasicPlaygroundToC.simpleGetExample) {
|
notarizedGet(BasicPlaygroundToC.simpleGetExample) {
|
||||||
call.respond(HttpStatusCode.OK, BasicModels.BasicResponse(c = UUID.randomUUID().toString()))
|
call.respond(HttpStatusCode.OK, BasicModels.BasicResponse(c = UUID.randomUUID().toString()))
|
||||||
|
@ -5,6 +5,10 @@ import io.ktor.response.respondRedirect
|
|||||||
import io.ktor.routing.Routing
|
import io.ktor.routing.Routing
|
||||||
import io.ktor.routing.get
|
import io.ktor.routing.get
|
||||||
|
|
||||||
|
@Deprecated(
|
||||||
|
"Webjar approach is deprecated",
|
||||||
|
replaceWith = ReplaceWith("swagger()", "io.bkbn.kompendium.core.routes.swagger")
|
||||||
|
)
|
||||||
fun Routing.swaggerUI(openApiJsonUrl: String = "/openapi.json") {
|
fun Routing.swaggerUI(openApiJsonUrl: String = "/openapi.json") {
|
||||||
get("/swagger-ui") {
|
get("/swagger-ui") {
|
||||||
call.respondRedirect("/webjars/swagger-ui/index.html?url=$openApiJsonUrl", true)
|
call.respondRedirect("/webjars/swagger-ui/index.html?url=$openApiJsonUrl", true)
|
||||||
|
Reference in New Issue
Block a user