Skip to content

Commit

Permalink
minor refinements
Browse files Browse the repository at this point in the history
  • Loading branch information
SMILEY4 committed Mar 29, 2024
1 parent 3aa85a1 commit 4bf6dd0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,13 @@ class PluginConfigDsl {
*/
var ignoredRouteSelectors: Set<KClass<*>> = PluginConfigData.DEFAULT.ignoredRouteSelectors


/**
* Invoked after generating the openapi-spec. Can be to e.g. further customize the spec.
*/
var whenBuildOpenApiSpecs: WhenBuildOpenApiSpecs? = null


internal fun build(base: PluginConfigData): PluginConfigData {
return PluginConfigData(
defaultUnauthorizedResponse = merge(base.defaultUnauthorizedResponse, defaultUnauthorizedResponse),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,6 @@ private fun Application.myModule() {
type = AuthType.HTTP
scheme = AuthScheme.BASIC
}
whenBuildOpenApiSpecs = {
it.paths.forEach { t, u ->
println("$t $u")
if (t == "/hello") {
u.servers ?: run {
u.servers = mutableListOf()
}
u.servers.add(
Server().apply {
url = "http://local.api"
}
)
}
}
}
}

// configure routes
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github.smiley4.ktorswaggerui.examples

import io.ktor.server.application.Application
import com.fasterxml.jackson.core.util.DefaultIndenter
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter
import com.fasterxml.jackson.databind.SerializationFeature
Expand All @@ -13,6 +12,7 @@ import io.ktor.http.ContentType
import io.ktor.http.HttpHeaders
import io.ktor.http.HttpStatusCode
import io.ktor.serialization.jackson.jackson
import io.ktor.server.application.Application
import io.ktor.server.application.call
import io.ktor.server.application.install
import io.ktor.server.engine.embeddedServer
Expand All @@ -21,8 +21,11 @@ import io.ktor.server.plugins.contentnegotiation.ContentNegotiation
import io.ktor.server.request.receive
import io.ktor.server.response.respond
import io.ktor.server.response.respondText
import io.ktor.server.routing.get
import io.ktor.server.routing.routing
import io.swagger.v3.oas.models.Operation
import io.swagger.v3.oas.models.PathItem
import io.swagger.v3.oas.models.responses.ApiResponse
import io.swagger.v3.oas.models.responses.ApiResponses
import java.util.Random

/**
Expand Down Expand Up @@ -81,6 +84,17 @@ private fun Application.myModule() {
description = "Routes for math related operations"
}
generateTags { url -> listOf(url.firstOrNull()) }
whenBuildOpenApiSpecs = { spec ->
spec.paths.addPathItem("customPath", PathItem().also { path ->
path.get = Operation().also { op ->
op.description = "This path was added after generating the openapi-spec"
op.responses = ApiResponses().also { responses ->
responses.addApiResponse("200", ApiResponse())
responses.addApiResponse("404", ApiResponse())
}
}
})
}
}

install(ContentNegotiation) {
Expand Down Expand Up @@ -259,14 +273,14 @@ private fun Application.myModule() {
body<List<List<Int>>>()
}
}
}){
}) {
call.respond(HttpStatusCode.NotImplemented, "...")
}

get("hidden", {
hidden = true
description = "This route is hidden and not visible in swagger"
}){
}) {
call.respond(HttpStatusCode.NotImplemented, "...")
}

Expand Down

0 comments on commit 4bf6dd0

Please sign in to comment.