From 452ab4773fc173a9331af221c32cb70fe882a745 Mon Sep 17 00:00:00 2001 From: Ryan Brink <5607577+unredundant@users.noreply.github.com> Date: Thu, 10 Feb 2022 07:44:52 -0500 Subject: [PATCH] doc: add custom type example to playground (#189) --- CHANGELOG.md | 1 + kompendium-playground/build.gradle.kts | 1 + .../playground/CustomTypePlayground.kt | 74 +++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 kompendium-playground/src/main/kotlin/io/bkbn/kompendium/playground/CustomTypePlayground.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 73c449e69..336b550ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Unreleased ### Added +- Custom Type example to playground ### Changed - Cleaned up and broke out handlers into separate classes diff --git a/kompendium-playground/build.gradle.kts b/kompendium-playground/build.gradle.kts index 54f6f275c..3f42aa239 100644 --- a/kompendium-playground/build.gradle.kts +++ b/kompendium-playground/build.gradle.kts @@ -37,6 +37,7 @@ dependencies { implementation(group = "org.jetbrains.kotlinx", "kotlinx-serialization-json", version = "1.3.2") + implementation(group = "org.jetbrains.kotlinx", "kotlinx-datetime", version = "0.3.2") implementation(group = "joda-time", name = "joda-time", version = "2.10.13") } diff --git a/kompendium-playground/src/main/kotlin/io/bkbn/kompendium/playground/CustomTypePlayground.kt b/kompendium-playground/src/main/kotlin/io/bkbn/kompendium/playground/CustomTypePlayground.kt new file mode 100644 index 000000000..1c4fe6860 --- /dev/null +++ b/kompendium-playground/src/main/kotlin/io/bkbn/kompendium/playground/CustomTypePlayground.kt @@ -0,0 +1,74 @@ +package io.bkbn.kompendium.playground + +import io.bkbn.kompendium.core.Kompendium +import io.bkbn.kompendium.core.Notarized.notarizedGet +import io.bkbn.kompendium.core.metadata.ResponseInfo +import io.bkbn.kompendium.core.metadata.method.GetInfo +import io.bkbn.kompendium.core.routes.redoc +import io.bkbn.kompendium.oas.schema.SimpleSchema +import io.bkbn.kompendium.oas.serialization.KompendiumSerializersModule +import io.bkbn.kompendium.playground.CustomTypePlaygroundToC.simpleGetExample +import io.bkbn.kompendium.playground.util.Util +import io.ktor.application.Application +import io.ktor.application.call +import io.ktor.application.install +import io.ktor.features.ContentNegotiation +import io.ktor.http.HttpStatusCode +import io.ktor.response.respond +import io.ktor.routing.routing +import io.ktor.serialization.json +import io.ktor.server.engine.embeddedServer +import io.ktor.server.netty.Netty +import kotlinx.datetime.Clock +import kotlinx.datetime.Instant +import kotlinx.serialization.Serializable +import kotlinx.serialization.json.Json + +fun main() { + embeddedServer( + Netty, + port = 8081, + module = Application::mainModule + ).start(wait = true) +} + +private fun Application.mainModule() { + install(ContentNegotiation) { + json(Json { + serializersModule = KompendiumSerializersModule.module + encodeDefaults = true + explicitNulls = false + }) + } + install(Kompendium) { + spec = Util.baseSpec + // Tells Kompendium how to handle a specific type + addCustomTypeSchema(Instant::class, SimpleSchema("string")) + } + routing { + redoc(pageTitle = "Custom overridden type Docs") + notarizedGet(simpleGetExample) { + call.respond(HttpStatusCode.OK, CustomTypeModels.AwesomeThingHappened("this http call!", Clock.System.now())) + } + } +} + +private object CustomTypePlaygroundToC { + val simpleGetExample = GetInfo( + summary = "Simple, Documented GET Request", + description = "This is to showcase just how easy it is to document your Ktor API!", + responseInfo = ResponseInfo( + status = HttpStatusCode.OK, + description = "This means everything went as expected!", + ), + tags = setOf("Simple") + ) +} + +private object CustomTypeModels { + @Serializable + data class AwesomeThingHappened( + val thing: String, + val timestamp: Instant + ) +}