Skip to content

Commit

Permalink
improve default schema generator, minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
SMILEY4 committed Jun 8, 2024
1 parent ef9f609 commit 48faa19
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 4 deletions.
2 changes: 2 additions & 0 deletions ktor-swagger-ui-examples/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ version = projectVersion

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

repositories {
Expand All @@ -29,6 +30,7 @@ dependencies {

implementation("io.github.smiley4:schema-kenerator-core:$versionSchemaKenerator")
implementation("io.github.smiley4:schema-kenerator-reflection:$versionSchemaKenerator")
implementation("io.github.smiley4:schema-kenerator-serialization:$versionSchemaKenerator")
implementation("io.github.smiley4:schema-kenerator-swagger:$versionSchemaKenerator")
implementation("io.github.smiley4:schema-kenerator-jackson:$versionSchemaKenerator")

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package io.github.smiley4.ktorswaggerui.examples

import io.github.smiley4.ktorswaggerui.SwaggerUI
import io.github.smiley4.ktorswaggerui.dsl.routing.get
import io.github.smiley4.ktorswaggerui.routing.openApiSpec
import io.github.smiley4.ktorswaggerui.routing.swaggerUI
import io.github.smiley4.schemakenerator.serialization.processKotlinxSerialization
import io.github.smiley4.schemakenerator.swagger.compileReferencingRoot
import io.github.smiley4.schemakenerator.swagger.data.TitleType
import io.github.smiley4.schemakenerator.swagger.generateSwaggerSchema
import io.github.smiley4.schemakenerator.swagger.withAutoTitle
import io.ktor.http.HttpStatusCode
import io.ktor.server.application.Application
import io.ktor.server.application.call
import io.ktor.server.application.install
import io.ktor.server.engine.embeddedServer
import io.ktor.server.netty.Netty
import io.ktor.server.response.respond
import io.ktor.server.routing.route
import io.ktor.server.routing.routing
import kotlinx.serialization.Serializable

fun main() {
embeddedServer(Netty, port = 8080, host = "localhost", module = Application::myModule).start(wait = true)
}

private fun Application.myModule() {

// Install and configure the "SwaggerUI"-Plugin
install(SwaggerUI) {
schemas {
// replace default schema-generator with customized one
generator = { type ->
type
// process type using kotlinx-serialization instead of reflection
// requires additional dependency "io.github.smiley4:schema-kenerator-kotlinx-serialization:<VERSION>"
// see https://github.com/SMILEY4/schema-kenerator for more information
.processKotlinxSerialization()
.generateSwaggerSchema()
.withAutoTitle(TitleType.SIMPLE)
.compileReferencingRoot()
}
}
}

routing {

// Create a route for the swagger-ui using the openapi-spec at "/api.json".
// This route will not be included in the spec.
route("swagger") {
swaggerUI("/api.json")
}
// Create a route for the openapi-spec file.
// This route will not be included in the spec.
route("api.json") {
openApiSpec()
}

// a documented route
get("hello", {
// information about the request
response {
// information about a "200 OK" response
HttpStatusCode.OK to {
// body of the response
body<MyResponseBody>()
}
}
}) {
call.respond(HttpStatusCode.NotImplemented, "...")
}

}

}

@Serializable
private class MyResponseBody(
val name: String,
)
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package io.github.smiley4.ktorswaggerui.data

import io.github.smiley4.schemakenerator.core.connectSubTypes
import io.github.smiley4.schemakenerator.core.handleNameAnnotation
import io.github.smiley4.schemakenerator.reflection.collectSubTypes
import io.github.smiley4.schemakenerator.reflection.processReflection
import io.github.smiley4.schemakenerator.swagger.compileReferencingRoot
import io.github.smiley4.schemakenerator.swagger.data.CompiledSwaggerSchema
import io.github.smiley4.schemakenerator.swagger.data.TitleType
import io.github.smiley4.schemakenerator.swagger.generateSwaggerSchema
import io.github.smiley4.schemakenerator.swagger.handleCoreAnnotations
import io.github.smiley4.schemakenerator.swagger.withAutoTitle
import kotlin.reflect.KType

Expand All @@ -19,8 +23,12 @@ data class SchemaConfigData(
schemas = emptyMap(),
generator = { type ->
type
.collectSubTypes()
.processReflection()
.connectSubTypes()
.handleNameAnnotation()
.generateSwaggerSchema()
.handleCoreAnnotations()
.withAutoTitle(TitleType.SIMPLE)
.compileReferencingRoot()
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class OpenApiSecurityScheme(


/**
* information for the oauth flow types supported.
* Information for the oauth flow types supported.
* Required for type [AuthType.OAUTH2]
*/
fun flows(block: OpenIdOAuthFlows.() -> Unit) {
Expand All @@ -71,7 +71,7 @@ class OpenApiSecurityScheme(


/**
* A short description for security scheme.
* A short description of the security scheme.
*/
var description: String? = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class OpenApiTag(
var externalDocDescription: String? = null

/**
*The URL for additional external documentation for this tag.
* The URL for additional external documentation for this tag.
*/
var externalDocUrl: String? = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ class OpenApiHeader {
*/
var deprecated: Boolean? = null


/**
* Specifies whether arrays and objects should generate separate parameters for each array item or object property.
*/
var explode: Boolean? = null

fun build() = OpenApiHeaderData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class OpenApiMultipartPart(
) {

/**
* Set a specific content type for this part
* Set a specific content types for this part
*/
var mediaTypes: Collection<ContentType> = setOf()

Expand Down

0 comments on commit 48faa19

Please sign in to comment.