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 May 26, 2023
2 parents adb5ffc + 36ccb04 commit 9953980
Show file tree
Hide file tree
Showing 94 changed files with 4,433 additions and 4,118 deletions.
12 changes: 10 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@ plugins {
kotlin("jvm") version "1.7.21"
`maven-publish`
id("org.owasp.dependencycheck") version "8.2.1"
kotlin("plugin.serialization") version "1.8.21"
}

group = "io.github.smiley4"
version = "1.6.1"
version = "2.0.0-rc"

repositories {
mavenCentral()
maven(url = "https://raw.githubusercontent.com/glureau/json-schema-serialization/mvn-repo")
}

dependencies {

val ktorVersion = "2.2.4"
val ktorVersion = "2.3.0"
implementation("io.ktor:ktor-server-core-jvm:$ktorVersion")
implementation("io.ktor:ktor-server-webjars:$ktorVersion")
implementation("io.ktor:ktor-server-auth:$ktorVersion")
Expand All @@ -37,6 +39,9 @@ dependencies {
implementation("com.github.victools:jsonschema-module-jackson:$jsonSchemaGeneratorVersion")
implementation("com.github.victools:jsonschema-module-swagger-2:$jsonSchemaGeneratorVersion")

val jacksonVersion = "2.14.2"
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:${jacksonVersion}")

val kotlinLoggingVersion = "2.1.23"
implementation("io.github.microutils:kotlin-logging-jvm:$kotlinLoggingVersion")

Expand All @@ -53,6 +58,9 @@ dependencies {
val versionKotlinTest = "1.7.21"
testImplementation("org.jetbrains.kotlin:kotlin-test:$versionKotlinTest")

testImplementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.1")
testImplementation("com.github.Ricky12Awesome:json-schema-serialization:0.9.9")
testImplementation("org.jetbrains.kotlinx:kotlinx-datetime:0.4.0")
}

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

import io.github.smiley4.ktorswaggerui.dsl.SwaggerUI
import io.github.smiley4.ktorswaggerui.dsl.SwaggerUIDsl
import io.github.smiley4.ktorswaggerui.dsl.SwaggerUiSort
import io.ktor.http.ContentType
import io.ktor.http.HttpStatusCode
Expand All @@ -15,7 +15,7 @@ class SwaggerController(
private val swaggerWebjarVersion: String,
private val apiSpecUrl: String,
private val jsonSpecProvider: () -> String,
private val swaggerUiConfig: SwaggerUI
private val swaggerUiConfig: SwaggerUIDsl
) {

suspend fun serveOpenApiSpec(call: ApplicationCall) {
Expand Down
121 changes: 113 additions & 8 deletions src/main/kotlin/io/github/smiley4/ktorswaggerui/SwaggerPlugin.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package io.github.smiley4.ktorswaggerui

import io.github.smiley4.ktorswaggerui.specbuilder.ApiSpecBuilder
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.pluginOrNull
import io.ktor.server.webjars.Webjars
import io.github.smiley4.ktorswaggerui.spec.openapi.*
import io.github.smiley4.ktorswaggerui.spec.route.RouteCollector
import io.github.smiley4.ktorswaggerui.spec.route.RouteDocumentationMerger
import io.github.smiley4.ktorswaggerui.spec.route.RouteMeta
import io.github.smiley4.ktorswaggerui.spec.schema.SchemaBuilder
import io.github.smiley4.ktorswaggerui.spec.schema.SchemaContext
import io.github.smiley4.ktorswaggerui.spec.schema.SchemaContextBuilder
import io.ktor.server.application.*
import io.ktor.server.application.hooks.*
import io.ktor.server.routing.*
import io.ktor.server.webjars.*
import io.swagger.v3.core.util.Json

/**
* This version must match the version of the gradle dependency
Expand All @@ -21,7 +26,9 @@ val SwaggerUI = createApplicationPlugin(name = "SwaggerUI", createConfiguration
if (application.pluginOrNull(Webjars) == null) {
application.install(Webjars)
}
apiSpecJson = ApiSpecBuilder().build(application, pluginConfig)
val routes = routes(application, pluginConfig)
val schemaContext = schemaContext(pluginConfig, routes)
apiSpecJson = Json.pretty(builder(pluginConfig, schemaContext).build(routes))
}

SwaggerRouting(
Expand All @@ -31,3 +38,101 @@ val SwaggerUI = createApplicationPlugin(name = "SwaggerUI", createConfiguration
) { apiSpecJson }.setup(application)

}

private fun routes(application: Application, pluginConfig: SwaggerUIPluginConfig): List<RouteMeta> {
return RouteCollector(RouteDocumentationMerger())
.collectRoutes({ application.plugin(Routing) }, pluginConfig)
.toList()
}

private fun schemaContext(pluginConfig: SwaggerUIPluginConfig, routes: List<RouteMeta>): SchemaContext {
return SchemaContextBuilder(
config = pluginConfig,
schemaBuilder = SchemaBuilder(
definitionsField = pluginConfig.encodingConfig.schemaDefinitionsField,
schemaEncoder = pluginConfig.encodingConfig.getSchemaEncoder()
),
).build(routes.toList())
}

private fun builder(config: SwaggerUIPluginConfig, schemaContext: SchemaContext): OpenApiBuilder {
return OpenApiBuilder(
config = config,
schemaContext = schemaContext,
infoBuilder = InfoBuilder(
contactBuilder = ContactBuilder(),
licenseBuilder = LicenseBuilder()
),
serverBuilder = ServerBuilder(),
tagBuilder = TagBuilder(
externalDocumentationBuilder = ExternalDocumentationBuilder()
),
pathsBuilder = PathsBuilder(
pathBuilder = PathBuilder(
operationBuilder = OperationBuilder(
operationTagsBuilder = OperationTagsBuilder(config),
parameterBuilder = ParameterBuilder(schemaContext),
requestBodyBuilder = RequestBodyBuilder(
contentBuilder = ContentBuilder(
schemaContext = schemaContext,
exampleBuilder = ExampleBuilder(
config = config
),
headerBuilder = HeaderBuilder(schemaContext)
)
),
responsesBuilder = ResponsesBuilder(
responseBuilder = ResponseBuilder(
headerBuilder = HeaderBuilder(schemaContext),
contentBuilder = ContentBuilder(
schemaContext = schemaContext,
exampleBuilder = ExampleBuilder(
config = config
),
headerBuilder = HeaderBuilder(schemaContext)
)
),
config = config
),
securityRequirementsBuilder = SecurityRequirementsBuilder(config),
)
)
),
componentsBuilder = ComponentsBuilder(
config = config,
securitySchemesBuilder = SecuritySchemesBuilder(
oAuthFlowsBuilder = OAuthFlowsBuilder()
)
)
)
}






























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

import io.github.smiley4.ktorswaggerui.dsl.SwaggerUI
import io.github.smiley4.ktorswaggerui.dsl.SwaggerUIDsl
import io.ktor.server.application.Application
import io.ktor.server.application.call
import io.ktor.server.auth.authenticate
Expand All @@ -16,7 +16,7 @@ import mu.KotlinLogging
* Registers and handles routes required for the swagger-ui
*/
class SwaggerRouting(
private val swaggerUiConfig: SwaggerUI,
private val swaggerUiConfig: SwaggerUIDsl,
appConfig: ApplicationConfig,
swaggerWebjarVersion: String,
jsonSpecProvider: () -> String
Expand Down
Loading

0 comments on commit 9953980

Please sign in to comment.