Skip to content

Commit

Permalink
Merge branch 'develop' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
SMILEY4 committed Oct 24, 2024
2 parents 7324a57 + 24dd129 commit 0a85abe
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 63 deletions.
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
kotlin("jvm") version "1.9.25"
kotlin("jvm") version "2.0.21"
id("org.jetbrains.dokka") version "1.9.20" apply false
id("org.owasp.dependencycheck") version "8.2.1" apply false
id("io.gitlab.arturbosch.detekt") version "1.23.0" apply false
Expand All @@ -8,4 +8,4 @@ plugins {

repositories {
mavenCentral()
}
}
8 changes: 4 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ kotlin.code.style=official
# project id
projectGroupId=io.github.smiley4
projectArtifactIdBase=ktor-swagger-ui
projectVersion=3.6.0
projectVersion=4.0.0

# publishing information
projectNameBase=Ktor Swagger UI
Expand All @@ -16,12 +16,12 @@ projectDeveloperName=smiley4
projectDeveloperUrl=https://github.com/SMILEY4

# dependency versions
versionKtor=2.3.12
versionKtor=3.0.0
versionSwaggerUI=5.17.11
versionSwaggerParser=2.1.22
versionSchemaKenerator=1.5.0
versionKotlinLogging=7.0.0
versionKotest=5.8.0
versionKotlinTest=1.9.25
versionKotlinTest=2.0.21
versionMockk=1.13.12
versionLogback=1.5.6
versionLogback=1.5.6
4 changes: 2 additions & 2 deletions ktor-swagger-ui-examples/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ version = projectVersion

plugins {
kotlin("jvm")
kotlin("plugin.serialization") version "1.9.25"
kotlin("plugin.serialization") version "2.0.21"
}

repositories {
Expand Down Expand Up @@ -41,4 +41,4 @@ dependencies {

kotlin {
jvmToolchain(11)
}
}
1 change: 0 additions & 1 deletion ktor-swagger-ui/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ dependencies {
val versionMockk: String by project

implementation("io.ktor:ktor-server-core-jvm:$versionKtor")
implementation("io.ktor:ktor-server-webjars:$versionKtor")
implementation("io.ktor:ktor-server-auth:$versionKtor")
implementation("io.ktor:ktor-server-resources:$versionKtor")

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

import io.github.oshai.kotlinlogging.KotlinLogging
import io.github.smiley4.ktorswaggerui.builder.example.ExampleContext
import io.github.smiley4.ktorswaggerui.builder.example.ExampleContextImpl
import io.github.smiley4.ktorswaggerui.builder.openapi.ComponentsBuilder
Expand Down Expand Up @@ -29,21 +30,17 @@ import io.github.smiley4.ktorswaggerui.builder.route.RouteDocumentationMerger
import io.github.smiley4.ktorswaggerui.builder.route.RouteMeta
import io.github.smiley4.ktorswaggerui.builder.schema.SchemaContext
import io.github.smiley4.ktorswaggerui.builder.schema.SchemaContextImpl
import io.github.smiley4.ktorswaggerui.data.OutputFormat
import io.github.smiley4.ktorswaggerui.data.PluginConfigData
import io.github.smiley4.ktorswaggerui.dsl.config.PluginConfigDsl
import io.github.smiley4.ktorswaggerui.routing.ApiSpec
import io.ktor.server.application.Application
import io.ktor.server.application.ApplicationStarted
import io.ktor.server.application.createApplicationPlugin
import io.ktor.server.application.hooks.MonitoringEvent
import io.ktor.server.application.install
import io.ktor.server.application.plugin
import io.ktor.server.application.pluginOrNull
import io.ktor.server.routing.Routing
import io.ktor.server.webjars.Webjars
import io.ktor.server.routing.RoutingRoot
import io.swagger.v3.core.util.Json31
import io.github.oshai.kotlinlogging.KotlinLogging
import io.github.smiley4.ktorswaggerui.data.OutputFormat
import io.swagger.v3.core.util.Yaml31

/**
Expand All @@ -59,10 +56,6 @@ val SwaggerUI = createApplicationPlugin(name = "SwaggerUI", createConfiguration

on(MonitoringEvent(ApplicationStarted)) { application ->

if (application.pluginOrNull(Webjars) == null) {
application.install(Webjars)
}

try {
val routes = routes(application, config)
ApiSpec.setAll(buildOpenApiSpecs(config, routes))
Expand Down Expand Up @@ -102,7 +95,7 @@ private fun buildOpenApiSpec(specName: String, pluginConfig: PluginConfigData, r
}
val openApi = builder(pluginConfig, schemaContext, exampleContext).build(routes)
pluginConfig.postBuild?.let { it(openApi, specName) }
when(pluginConfig.outputFormat) {
when (pluginConfig.outputFormat) {
OutputFormat.JSON -> Json31.pretty(openApi) to pluginConfig.outputFormat
OutputFormat.YAML -> Yaml31.pretty(openApi) to pluginConfig.outputFormat
}
Expand All @@ -114,11 +107,12 @@ private fun buildOpenApiSpec(specName: String, pluginConfig: PluginConfigData, r

private fun routes(application: Application, config: PluginConfigData): List<RouteMeta> {
return RouteCollector(RouteDocumentationMerger())
.collectRoutes({ application.plugin(Routing) }, config)
.collectRoutes({ application.plugin(RoutingRoot) }, config)
.map { it.copy(path = "${application.rootPath()}${it.path}") }
.toList()
}


/**
* fix [#97](https://github.com/SMILEY4/ktor-swagger-ui/pull/97)
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import io.ktor.server.routing.HttpMethodRouteSelector
import io.ktor.server.routing.OptionalParameterRouteSelector
import io.ktor.server.routing.ParameterRouteSelector
import io.ktor.server.routing.RootRouteSelector
import io.ktor.server.routing.Route
import io.ktor.server.routing.RouteSelector
import io.ktor.server.routing.RoutingNode
import io.ktor.server.routing.TrailingSlashRouteSelector
import kotlin.reflect.full.isSubclassOf

Expand All @@ -25,7 +25,7 @@ class RouteCollector(
/**
* Collect all routes from the given application
*/
fun collectRoutes(routeProvider: () -> Route, config: PluginConfigData): Sequence<RouteMeta> {
fun collectRoutes(routeProvider: () -> RoutingNode, config: PluginConfigData): Sequence<RouteMeta> {
return allRoutes(routeProvider())
.asSequence()
.map { route ->
Expand All @@ -42,7 +42,7 @@ class RouteCollector(
}


private fun getDocumentation(route: Route, base: OpenApiRoute): OpenApiRoute {
private fun getDocumentation(route: RoutingNode, base: OpenApiRoute): OpenApiRoute {
var documentation = base
if (route.selector is DocumentedRouteSelector) {
documentation = routeDocumentationMerger.merge(documentation, (route.selector as DocumentedRouteSelector).documentation)
Expand All @@ -55,13 +55,13 @@ class RouteCollector(
}


private fun getMethod(route: Route): HttpMethod {
private fun getMethod(route: RoutingNode): HttpMethod {
return (route.selector as HttpMethodRouteSelector).method
}


@Suppress("CyclomaticComplexMethod")
private fun getPath(route: Route, config: PluginConfigData): String {
private fun getPath(route: RoutingNode, config: PluginConfigData): String {
val selector = route.selector
return if (isIgnoredSelector(selector, config)) {
route.parent?.let { getPath(it, config) } ?: ""
Expand Down Expand Up @@ -96,7 +96,7 @@ class RouteCollector(
}


private fun isProtected(route: Route): Boolean {
private fun isProtected(route: RoutingNode): Boolean {
return when (route.selector) {
is AuthenticationRouteSelector -> true
is TrailingSlashRouteSelector -> false
Expand All @@ -108,7 +108,7 @@ class RouteCollector(
}
}

private fun allRoutes(root: Route): List<Route> {
private fun allRoutes(root: RoutingNode): List<RoutingNode> {
return (listOf(root) + root.children.flatMap { allRoutes(it) })
.filter { it.selector is HttpMethodRouteSelector }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package io.github.smiley4.ktorswaggerui.dsl.routing

import io.github.smiley4.ktorswaggerui.dsl.routes.OpenApiRoute
import io.ktor.http.HttpMethod
import io.ktor.server.application.ApplicationCall
import io.ktor.server.routing.Route
import io.ktor.server.routing.RouteSelector
import io.ktor.server.routing.RouteSelectorEvaluation
Expand All @@ -16,9 +15,7 @@ import io.ktor.server.routing.patch
import io.ktor.server.routing.post
import io.ktor.server.routing.put
import io.ktor.server.routing.route
import io.ktor.util.KtorDsl
import io.ktor.util.pipeline.PipelineContext
import io.ktor.util.pipeline.PipelineInterceptor
import io.ktor.utils.io.KtorDsl

class DocumentedRouteSelector(val documentation: OpenApiRoute) : RouteSelector() {

Expand All @@ -29,7 +26,7 @@ class DocumentedRouteSelector(val documentation: OpenApiRoute) : RouteSelector()
}
}

override fun evaluate(context: RoutingResolveContext, segmentIndex: Int) = RouteSelectorEvaluation.Transparent
override suspend fun evaluate(context: RoutingResolveContext, segmentIndex: Int) = RouteSelectorEvaluation.Transparent

override fun toString() = if (includeDocumentedRouteInRouteToString) super.toString() else ""
}
Expand Down Expand Up @@ -94,14 +91,14 @@ fun Route.method(
fun Route.get(
path: String,
builder: OpenApiRoute.() -> Unit = { },
body: PipelineInterceptor<Unit, ApplicationCall>
body: suspend io.ktor.server.routing.RoutingContext.() -> Unit
): Route {
return documentation(builder) { get(path, body) }
}

fun Route.get(
builder: OpenApiRoute.() -> Unit = { },
body: PipelineInterceptor<Unit, ApplicationCall>
body: suspend io.ktor.server.routing.RoutingContext.() -> Unit
): Route {
return documentation(builder) { get(body) }
}
Expand All @@ -114,15 +111,15 @@ fun Route.get(
fun Route.post(
path: String,
builder: OpenApiRoute.() -> Unit = { },
body: PipelineInterceptor<Unit, ApplicationCall>
body: suspend io.ktor.server.routing.RoutingContext.() -> Unit
): Route {
return documentation(builder) { post(path, body) }
}

@JvmName("postTyped")
inline fun <reified R : Any> Route.post(
noinline builder: OpenApiRoute.() -> Unit = { },
crossinline body: suspend PipelineContext<Unit, ApplicationCall>.(R) -> Unit
crossinline body: suspend io.ktor.server.routing.RoutingContext.(R) -> Unit
): Route {
return documentation(builder) { post(body) }
}
Expand All @@ -131,15 +128,15 @@ inline fun <reified R : Any> Route.post(
inline fun <reified R : Any> Route.post(
path: String,
noinline builder: OpenApiRoute.() -> Unit = { },
crossinline body: suspend PipelineContext<Unit, ApplicationCall>.(R) -> Unit
crossinline body: suspend io.ktor.server.routing.RoutingContext.(R) -> Unit
): Route {
return documentation(builder) { post(path, body) }
}


fun Route.post(
builder: OpenApiRoute.() -> Unit = { },
body: PipelineInterceptor<Unit, ApplicationCall>
body: suspend io.ktor.server.routing.RoutingContext.() -> Unit
): Route {
return documentation(builder) { post(body) }
}
Expand All @@ -152,22 +149,22 @@ fun Route.post(
fun Route.put(
path: String,
builder: OpenApiRoute.() -> Unit = { },
body: PipelineInterceptor<Unit, ApplicationCall>
body: suspend io.ktor.server.routing.RoutingContext.() -> Unit
): Route {
return documentation(builder) { put(path, body) }
}

fun Route.put(
builder: OpenApiRoute.() -> Unit = { },
body: PipelineInterceptor<Unit, ApplicationCall>
body: suspend io.ktor.server.routing.RoutingContext.() -> Unit
): Route {
return documentation(builder) { put(body) }
}

@JvmName("putTyped")
inline fun <reified R : Any> Route.put(
noinline builder: OpenApiRoute.() -> Unit = { },
crossinline body: suspend PipelineContext<Unit, ApplicationCall>.(R) -> Unit
crossinline body: suspend io.ktor.server.routing.RoutingContext.(R) -> Unit
): Route {
return documentation(builder) { put(body) }
}
Expand All @@ -176,7 +173,7 @@ inline fun <reified R : Any> Route.put(
inline fun <reified R : Any> Route.put(
path: String,
noinline builder: OpenApiRoute.() -> Unit = { },
crossinline body: suspend PipelineContext<Unit, ApplicationCall>.(R) -> Unit
crossinline body: suspend io.ktor.server.routing.RoutingContext.(R) -> Unit
): Route {
return documentation(builder) { put(path, body) }
}
Expand All @@ -189,14 +186,14 @@ inline fun <reified R : Any> Route.put(
fun Route.delete(
path: String,
builder: OpenApiRoute.() -> Unit = { },
body: PipelineInterceptor<Unit, ApplicationCall>
body: suspend io.ktor.server.routing.RoutingContext.() -> Unit
): Route {
return documentation(builder) { delete(path, body) }
}

fun Route.delete(
builder: OpenApiRoute.() -> Unit = { },
body: PipelineInterceptor<Unit, ApplicationCall>
body: suspend io.ktor.server.routing.RoutingContext.() -> Unit
): Route {
return documentation(builder) { delete(body) }
}
Expand All @@ -210,23 +207,23 @@ fun Route.delete(
fun Route.patch(
path: String,
builder: OpenApiRoute.() -> Unit = { },
body: PipelineInterceptor<Unit, ApplicationCall>
body: suspend io.ktor.server.routing.RoutingContext.() -> Unit
): Route {
return documentation(builder) { patch(path, body) }
}

@KtorDsl
fun Route.patch(
builder: OpenApiRoute.() -> Unit = { },
body: PipelineInterceptor<Unit, ApplicationCall>
body: suspend io.ktor.server.routing.RoutingContext.() -> Unit
): Route {
return documentation(builder) { patch(body) }
}

@JvmName("patchTyped")
inline fun <reified R : Any> Route.patch(
noinline builder: OpenApiRoute.() -> Unit = { },
crossinline body: suspend PipelineContext<Unit, ApplicationCall>.(R) -> Unit
crossinline body: suspend io.ktor.server.routing.RoutingContext.(R) -> Unit
): Route {
return documentation(builder) { patch(body) }

Expand All @@ -236,7 +233,7 @@ inline fun <reified R : Any> Route.patch(
inline fun <reified R : Any> Route.patch(
path: String,
noinline builder: OpenApiRoute.() -> Unit = { },
crossinline body: suspend PipelineContext<Unit, ApplicationCall>.(R) -> Unit
crossinline body: suspend io.ktor.server.routing.RoutingContext.(R) -> Unit
): Route {
return documentation(builder) { patch(path, body) }
}
Expand All @@ -249,14 +246,14 @@ inline fun <reified R : Any> Route.patch(
fun Route.options(
path: String,
builder: OpenApiRoute.() -> Unit = { },
body: PipelineInterceptor<Unit, ApplicationCall>
body: suspend io.ktor.server.routing.RoutingContext.() -> Unit
): Route {
return documentation(builder) { options(path, body) }
}

fun Route.options(
builder: OpenApiRoute.() -> Unit = { },
body: PipelineInterceptor<Unit, ApplicationCall>
body: suspend io.ktor.server.routing.RoutingContext.() -> Unit
): Route {
return documentation(builder) { options(body) }
}
Expand All @@ -269,14 +266,14 @@ fun Route.options(
fun Route.head(
path: String,
builder: OpenApiRoute.() -> Unit = { },
body: PipelineInterceptor<Unit, ApplicationCall>
body: suspend io.ktor.server.routing.RoutingContext.() -> Unit
): Route {
return documentation(builder) { head(path, body) }
}

fun Route.head(
builder: OpenApiRoute.() -> Unit = { },
body: PipelineInterceptor<Unit, ApplicationCall>
body: suspend io.ktor.server.routing.RoutingContext.() -> Unit
): Route {
return documentation(builder) { head(body) }
}
Loading

0 comments on commit 0a85abe

Please sign in to comment.