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 Mar 20, 2023
2 parents c2450ea + e95645d commit acf3a7b
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 5 deletions.
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = "io.github.smiley4"
version = "1.3.2"
version = "1.4.0"

repositories {
mavenCentral()
Expand Down Expand Up @@ -34,6 +34,7 @@ dependencies {
val jsonSchemaGeneratorVersion = "4.28.0"
implementation("com.github.victools:jsonschema-generator:$jsonSchemaGeneratorVersion")
implementation("com.github.victools:jsonschema-module-jackson:$jsonSchemaGeneratorVersion")
implementation("com.github.victools:jsonschema-module-swagger-2:$jsonSchemaGeneratorVersion")

val kotlinLoggingVersion = "2.1.23"
implementation("io.github.microutils:kotlin-logging-jvm:$kotlinLoggingVersion")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.github.victools.jsonschema.generator.OptionPreset
import com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder
import com.github.victools.jsonschema.generator.SchemaVersion
import com.github.victools.jsonschema.module.jackson.JacksonModule
import com.github.victools.jsonschema.module.swagger2.Swagger2Module
import io.github.smiley4.ktorswaggerui.dsl.CustomSchemas
import io.github.smiley4.ktorswaggerui.dsl.OpenApiDslMarker
import io.github.smiley4.ktorswaggerui.dsl.OpenApiInfo
Expand Down Expand Up @@ -156,6 +157,7 @@ class SwaggerUIPluginConfig {
var schemaGeneratorConfigBuilder: SchemaGeneratorConfigBuilder =
SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2019_09, OptionPreset.PLAIN_JSON)
.with(JacksonModule())
.with(Swagger2Module())
.without(Option.DEFINITIONS_FOR_ALL_OBJECTS)
.with(Option.INLINE_ALL_SCHEMAS)
.with(Option.EXTRA_OPEN_API_FORMAT_VALUES)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,25 @@ package io.github.smiley4.ktorswaggerui.specbuilder

import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.node.ArrayNode
import io.swagger.v3.oas.models.media.Schema

class JsonToOpenApiSchemaConverter {

fun toSchema(json: String) = toSchema(ObjectMapper().readTree(json))


fun toSchema(node: JsonNode): Schema<Any> {
return Schema<Any>().apply {
node["\$schema"]?.let { this.`$schema` = it.asText() }
node["type"]?.let { this.type = it.asText() }
node["description"]?.let { this.description = it.asText() }
node["title"]?.let { this.title = it.asText() }
node["type"]?.let {
val types = if (it is ArrayNode) it.collectElements().map { e -> e.asText() } else listOf(it.asText())
this.type = types.firstOrNull { e -> e != "null" }
if (types.contains("null")) {
this.nullable = true
}
}
node["format"]?.let { this.format = it.asText() }
node["items"]?.let { this.items = toSchema(it) }
node["properties"]?.let { this.properties = it.collectFields().associate { prop -> prop.key to toSchema(prop.value) } }
Expand All @@ -27,12 +35,10 @@ class JsonToOpenApiSchemaConverter {
}
}


private fun JsonNode.collectFields(): List<MutableMap.MutableEntry<String, JsonNode>> {
return this.fields().asSequence().toList()
}


private fun JsonNode.collectElements(): List<JsonNode> {
return this.elements().asSequence().toList()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package io.github.smiley4.ktorswaggerui.examples

import com.fasterxml.jackson.core.util.DefaultIndenter
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter
import com.fasterxml.jackson.databind.SerializationFeature
import io.github.smiley4.ktorswaggerui.SwaggerUI
import io.github.smiley4.ktorswaggerui.dsl.get
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
import io.ktor.server.netty.Netty
import io.ktor.server.plugins.contentnegotiation.ContentNegotiation
import io.ktor.server.response.respond
import io.ktor.server.routing.routing
import io.swagger.v3.oas.annotations.media.Schema

/**
* An example showing the [Schema]-annotation, adding additional information to models
*/
fun main() {
embeddedServer(Netty, port = 8080, host = "localhost", module = Application::myModule).start(wait = true)
}

private fun Application.myModule() {
install(SwaggerUI)
install(ContentNegotiation) {
jackson {
configure(SerializationFeature.INDENT_OUTPUT, true)
setDefaultPrettyPrinter(DefaultPrettyPrinter().apply {
indentArraysWith(DefaultPrettyPrinter.FixedSpaceIndenter.instance)
indentObjectsWith(DefaultIndenter(" ", "\n"))
})
}
}
routing {
get("somebody", {
response {
HttpStatusCode.OK to {
body<Person>()
}
}
}) {
call.respond(Person("Somebody", 42))
}
}
}

@Schema(title = "The Schema for a person")
data class Person(

@field:Schema(description = "the name of the person")
val name: String,

@field:Schema(description = "the age of the person in years", nullable = true)
val age: Int

)

0 comments on commit acf3a7b

Please sign in to comment.