From a3cf7dad9b8fb99780bbef5dd4cdd5fe9e1acfb6 Mon Sep 17 00:00:00 2001 From: Ryan Brink <5607577+rgbrizzlehizzle@users.noreply.github.com> Date: Mon, 19 Apr 2021 14:47:35 -0400 Subject: [PATCH] fix root route appending a trailing slash (#30) --- CHANGELOG.md | 6 ++ .../leafygreens/kompendium/util/Helpers.kt | 2 +- .../leafygreens/kompendium/KompendiumTest.kt | 31 ++++++- .../src/test/resources/nested_under_root.json | 86 +++++++++++++++++++ 4 files changed, 123 insertions(+), 2 deletions(-) create mode 100644 kompendium-core/src/test/resources/nested_under_root.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 49d3185bc..7dd0daa9e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## [0.5.1] - April 19th, 2021 + +### Changed + +- Resolved bug where paths under root route where appending a trailing `/` + ## [0.5.0] - April 19th, 2021 ### Added diff --git a/kompendium-core/src/main/kotlin/org/leafygreens/kompendium/util/Helpers.kt b/kompendium-core/src/main/kotlin/org/leafygreens/kompendium/util/Helpers.kt index 1ed5bcf8b..2d12a1732 100644 --- a/kompendium-core/src/main/kotlin/org/leafygreens/kompendium/util/Helpers.kt +++ b/kompendium-core/src/main/kotlin/org/leafygreens/kompendium/util/Helpers.kt @@ -49,7 +49,7 @@ object Helpers { // dumb ass workaround to this object being internal to ktor "TrailingSlashRouteSelector" -> { logger.info("Found trailing slash route selector") - val newTail = "$tail/" + val newTail = tail.ifBlank { "/" } parent?.calculatePath(newTail) ?: run { logger.info("No parent found, returning current path") newTail diff --git a/kompendium-core/src/test/kotlin/org/leafygreens/kompendium/KompendiumTest.kt b/kompendium-core/src/test/kotlin/org/leafygreens/kompendium/KompendiumTest.kt index b565fd108..dddc60a97 100644 --- a/kompendium-core/src/test/kotlin/org/leafygreens/kompendium/KompendiumTest.kt +++ b/kompendium-core/src/test/kotlin/org/leafygreens/kompendium/KompendiumTest.kt @@ -228,6 +228,22 @@ internal class KompendiumTest { } } + @Test + fun `Nested under root module does not append trailing slash`() { + withTestApplication({ + configModule() + docs() + nestedUnderRootModule() + }) { + // do + val json = handleRequest(HttpMethod.Get, "/openapi.json").response.content + + // expect + val expected = TestData.getFileSnapshot("nested_under_root.json").trim() + assertEquals(expected, json, "The received json spec should match the expected content") + } + } + @Test fun `Can notarize a trailing slash route`() { withTestApplication({ @@ -328,7 +344,8 @@ internal class KompendiumTest { private companion object { val testGetResponse = ResponseInfo(KompendiumHttpCodes.OK, "A Successful Endeavor") val testPostResponse = ResponseInfo(KompendiumHttpCodes.CREATED, "A Successful Endeavor") - val testDeleteResponse = ResponseInfo(KompendiumHttpCodes.NO_CONTENT, "A Successful Endeavor", mediaTypes = emptyList()) + val testDeleteResponse = + ResponseInfo(KompendiumHttpCodes.NO_CONTENT, "A Successful Endeavor", mediaTypes = emptyList()) val testRequest = RequestInfo("A Test request") val testGetInfo = MethodInfo("Another get test", "testing more", testGetResponse) val testPostInfo = MethodInfo("Test post endpoint", "Post your tests here!", testPostResponse, testRequest) @@ -415,6 +432,18 @@ internal class KompendiumTest { } } + private fun Application.nestedUnderRootModule() { + routing { + route("/") { + route("/testerino") { + notarizedGet(testGetInfo) { + call.respondText { "🤔🔥" } + } + } + } + } + } + private fun Application.trailingSlash() { routing { route("/test") { diff --git a/kompendium-core/src/test/resources/nested_under_root.json b/kompendium-core/src/test/resources/nested_under_root.json new file mode 100644 index 000000000..8e5cbc12c --- /dev/null +++ b/kompendium-core/src/test/resources/nested_under_root.json @@ -0,0 +1,86 @@ +{ + "openapi" : "3.0.3", + "info" : { + "title" : "Test API", + "version" : "1.33.7", + "description" : "An amazing, fully-ish \uD83D\uDE09 generated API spec", + "termsOfService" : "https://example.com", + "contact" : { + "name" : "Homer Simpson", + "url" : "https://gph.is/1NPUDiM", + "email" : "chunkylover53@aol.com" + }, + "license" : { + "name" : "MIT", + "url" : "https://github.com/lg-backbone/kompendium/blob/main/LICENSE" + } + }, + "servers" : [ { + "url" : "https://myawesomeapi.com", + "description" : "Production instance of my API" + }, { + "url" : "https://staging.myawesomeapi.com", + "description" : "Where the fun stuff happens" + } ], + "paths" : { + "/testerino" : { + "get" : { + "tags" : [ ], + "summary" : "Another get test", + "description" : "testing more", + "parameters" : [ { + "name" : "a", + "in" : "path", + "schema" : { + "$ref" : "#/components/schemas/String" + }, + "required" : true, + "deprecated" : false + }, { + "name" : "aa", + "in" : "query", + "schema" : { + "$ref" : "#/components/schemas/Int" + }, + "required" : true, + "deprecated" : false + } ], + "responses" : { + "200" : { + "description" : "A Successful Endeavor", + "content" : { + "application/json" : { + "schema" : { + "$ref" : "#/components/schemas/TestResponse" + } + } + } + } + }, + "deprecated" : false + } + } + }, + "components" : { + "schemas" : { + "String" : { + "type" : "string" + }, + "TestResponse" : { + "properties" : { + "c" : { + "$ref" : "#/components/schemas/String" + } + }, + "type" : "object" + }, + "Int" : { + "format" : "int32", + "type" : "integer" + } + }, + "securitySchemes" : { } + }, + "security" : [ ], + "tags" : [ ] +}