From aa3290243b62a4d74e71cdf8773566f2262497eb Mon Sep 17 00:00:00 2001 From: Ryan Brink <5607577+rgbrizzlehizzle@users.noreply.github.com> Date: Thu, 3 Jun 2021 09:00:33 -0400 Subject: [PATCH] explicit decoupled serialization (#65) --- CHANGELOG.md | 6 ++ README.md | 10 +++ gradle.properties | 2 +- gradle/libs.versions.toml | 2 +- kompendium-auth/build.gradle.kts | 1 + kompendium-core/build.gradle.kts | 1 + kompendium-playground/build.gradle.kts | 2 + .../io/bkbn/kompendium/playground/Main.kt | 68 +++++++++---------- 8 files changed, 56 insertions(+), 36 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 61ca21081..6429fb241 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## [1.2.3] - June 3rd, 2021 + +### Added + +- Updates showing/explaining serializer agnostic approach + ## [1.2.2] - May 23rd, 2021 This is just to get my repo back to normal now that I have confirmed sonatype publish is happening diff --git a/README.md b/README.md index df3e2f2c6..1527f0478 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,16 @@ Out of the box, Kompendium has support for sealed classes. At runtime, it will b and build a spec that takes `anyOf` the implementations. This is currently a weak point of the entire library, and suggestions on better implementations are welcome 🤠 +### Serialization + +Kompendium is serialization agnostic, meaning that there is no serializer library included out of the box. This grants +developer flexibility, at the cost of some potential extra legwork. The example in the playground shows Jackson working +pretty much out of the box, but more explicit serializers like Moshi and Kotlinx Serialization will require registering +custom serializers in order to encode the api spec payload. + +This overhead is annoying, and will hopefully be reduced in the future. Should you have ideas on how to tackle this +issue, please head on over to the discussion on this topic [here](https://github.com/bkbnio/kompendium/discussions/64) + ## Examples The full source code can be found in the `kompendium-playground` module. Here is a simple get endpoint example diff --git a/gradle.properties b/gradle.properties index 97603680e..24f949931 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ # Kompendium -project.version=1.2.2 +project.version=1.2.3 # Kotlin kotlin.code.style=official # Gradle diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 80750334c..b194dc0e0 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -24,6 +24,6 @@ logback-core = { group = "ch.qos.logback", name = "logback-core", version.ref = webjars-swagger-ui = { group = "org.webjars", name = "swagger-ui", version.ref = "swagger-ui" } [bundles] -ktor = [ "ktor-server-core", "ktor-server-netty", "ktor-jackson", "ktor-html-builder" ] +ktor = [ "ktor-server-core", "ktor-server-netty", "ktor-html-builder" ] ktorAuth = [ "ktor-auth-lib", "ktor-auth-jwt" ] logging = [ "slf4j", "logback-classic", "logback-core" ] diff --git a/kompendium-auth/build.gradle.kts b/kompendium-auth/build.gradle.kts index 40cb88efd..9d65485bf 100644 --- a/kompendium-auth/build.gradle.kts +++ b/kompendium-auth/build.gradle.kts @@ -11,6 +11,7 @@ dependencies { implementation(libs.bundles.ktorAuth) implementation(projects.kompendiumCore) + testImplementation(libs.ktor.jackson) testImplementation("org.jetbrains.kotlin:kotlin-test") testImplementation("org.jetbrains.kotlin:kotlin-test-junit") testImplementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.12.0") diff --git a/kompendium-core/build.gradle.kts b/kompendium-core/build.gradle.kts index 41f3d8ce8..80932a312 100644 --- a/kompendium-core/build.gradle.kts +++ b/kompendium-core/build.gradle.kts @@ -10,6 +10,7 @@ dependencies { implementation(libs.bundles.ktor) testImplementation("org.jetbrains.kotlin:kotlin-test") testImplementation("org.jetbrains.kotlin:kotlin-test-junit") + testImplementation(libs.ktor.jackson) testImplementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.12.0") testImplementation("io.ktor:ktor-server-test-host:1.5.3") } diff --git a/kompendium-playground/build.gradle.kts b/kompendium-playground/build.gradle.kts index 90a010c3a..5bfd50470 100644 --- a/kompendium-playground/build.gradle.kts +++ b/kompendium-playground/build.gradle.kts @@ -1,4 +1,5 @@ plugins { + kotlin("plugin.serialization") version "1.5.0" application } @@ -11,6 +12,7 @@ dependencies { implementation(projects.kompendiumSwaggerUi) implementation(libs.bundles.ktor) + implementation(libs.ktor.jackson) implementation(libs.bundles.ktorAuth) implementation(libs.bundles.logging) diff --git a/kompendium-playground/src/main/kotlin/io/bkbn/kompendium/playground/Main.kt b/kompendium-playground/src/main/kotlin/io/bkbn/kompendium/playground/Main.kt index e9c819cf8..3a5722e2f 100644 --- a/kompendium-playground/src/main/kotlin/io/bkbn/kompendium/playground/Main.kt +++ b/kompendium-playground/src/main/kotlin/io/bkbn/kompendium/playground/Main.kt @@ -91,46 +91,46 @@ fun Application.mainModule() { openApi(oas) redoc(oas) swaggerUI() -// route("/potato/spud") { -// notarizedGet(testGetWithExamples) { -// call.respond(HttpStatusCode.OK) -// } -// notarizedPost(testPostWithExamples) { -// call.respond(HttpStatusCode.Created, ExampleResponse("hey")) -// } -// } + route("/potato/spud") { + notarizedGet(testGetWithExamples) { + call.respond(HttpStatusCode.OK) + } + notarizedPost(testPostWithExamples) { + call.respond(HttpStatusCode.Created, ExampleResponse("hey")) + } + } route("/test") { route("/{id}") { notarizedGet(testIdGetInfo) { call.respondText("get by id") } } -// route("/single") { -// notarizedGet(testSingleGetInfo) { -// call.respondText("get single") -// } -// notarizedPost(testSinglePostInfo) { -// call.respondText("test post") -// } -// notarizedPut(testSinglePutInfo) { -// call.respondText { "hey" } -// } -// notarizedDelete(testSingleDeleteInfo) { -// call.respondText { "heya" } -// } -// } -// authenticate("basic") { -// route("/authenticated/single") { -// notarizedGet(testAuthenticatedSingleGetInfo) { -// call.respond(HttpStatusCode.OK) -// } -// } -// } + route("/single") { + notarizedGet(testSingleGetInfo) { + call.respondText("get single") + } + notarizedPost(testSinglePostInfo) { + call.respondText("test post") + } + notarizedPut(testSinglePutInfo) { + call.respondText { "hey" } + } + notarizedDelete(testSingleDeleteInfo) { + call.respondText { "heya" } + } + } + authenticate("basic") { + route("/authenticated/single") { + notarizedGet(testAuthenticatedSingleGetInfo) { + call.respond(HttpStatusCode.OK) + } + } + } + } + route("/error") { + notarizedGet(testSingleGetInfoWithThrowable) { + error("bad things just happened") + } } -// route("/error") { -// notarizedGet(testSingleGetInfoWithThrowable) { -// error("bad things just happened") -// } -// } } }