From 5c7ec4fdb4879b310753761e50fd191a137fb531 Mon Sep 17 00:00:00 2001 From: NovolMob <60779683+NovolMob@users.noreply.github.com> Date: Mon, 8 May 2023 18:49:29 +0300 Subject: [PATCH] fix: route with parameter declared via ktor function (#455) --- CHANGELOG.md | 2 + .../kompendium/core/plugin/NotarizedRoute.kt | 1 + .../io/bkbn/kompendium/core/KompendiumTest.kt | 4 + .../bkbn/kompendium/core/util/RouteParsing.kt | 30 +++++++ .../test/resources/T0068__param_wrapper.json | 82 +++++++++++++++++++ 5 files changed, 119 insertions(+) create mode 100644 core/src/test/resources/T0068__param_wrapper.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 38ecf902b..f62a7461b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ ### Changed +- Route generation with parameters doesn`t add parameters to the path. + ### Remove --- diff --git a/core/src/main/kotlin/io/bkbn/kompendium/core/plugin/NotarizedRoute.kt b/core/src/main/kotlin/io/bkbn/kompendium/core/plugin/NotarizedRoute.kt index 428aded6a..ff693f776 100644 --- a/core/src/main/kotlin/io/bkbn/kompendium/core/plugin/NotarizedRoute.kt +++ b/core/src/main/kotlin/io/bkbn/kompendium/core/plugin/NotarizedRoute.kt @@ -86,6 +86,7 @@ object NotarizedRoute { ?: it } .replace(Regex("/\\(.+\\)"), "") + .replace(Regex("/\\[.+\\]"), "") fun Route.collectAuthMethods() = toString() .split("/") diff --git a/core/src/test/kotlin/io/bkbn/kompendium/core/KompendiumTest.kt b/core/src/test/kotlin/io/bkbn/kompendium/core/KompendiumTest.kt index 745e42b62..3c4811082 100644 --- a/core/src/test/kotlin/io/bkbn/kompendium/core/KompendiumTest.kt +++ b/core/src/test/kotlin/io/bkbn/kompendium/core/KompendiumTest.kt @@ -67,6 +67,7 @@ import io.bkbn.kompendium.core.util.stringContentEncodingConstraints import io.bkbn.kompendium.core.util.stringPatternConstraints import io.bkbn.kompendium.core.util.topLevelNullable import io.bkbn.kompendium.core.util.trailingSlash +import io.bkbn.kompendium.core.util.paramWrapper import io.bkbn.kompendium.core.util.unbackedFieldsResponse import io.bkbn.kompendium.core.util.withOperationId import io.bkbn.kompendium.json.schema.definition.TypeDefinition @@ -152,6 +153,9 @@ class KompendiumTest : DescribeSpec({ it("Can notarize a route with a trailing slash") { openApiTestAllSerializers("T0015__trailing_slash.json") { trailingSlash() } } + it("Can notarize a route with a parameter") { + openApiTestAllSerializers("T0068__param_wrapper.json") { paramWrapper() } + } } describe("Exceptions") { it("Can add an exception status code to a response") { diff --git a/core/src/test/kotlin/io/bkbn/kompendium/core/util/RouteParsing.kt b/core/src/test/kotlin/io/bkbn/kompendium/core/util/RouteParsing.kt index eead3dc5b..a277517f4 100644 --- a/core/src/test/kotlin/io/bkbn/kompendium/core/util/RouteParsing.kt +++ b/core/src/test/kotlin/io/bkbn/kompendium/core/util/RouteParsing.kt @@ -14,6 +14,7 @@ import io.ktor.http.HttpStatusCode import io.ktor.server.application.install import io.ktor.server.routing.Routing import io.ktor.server.routing.route +import io.ktor.server.routing.param fun Routing.simplePathParsing() { route("/this") { @@ -100,3 +101,32 @@ fun Routing.trailingSlash() { } } } + +fun Routing.paramWrapper() { + route("/test") { + param("a") { + param("b") { + param("c") { + install(NotarizedRoute()) { + parameters = listOf( + Parameter( + name = "test", + `in` = Parameter.Location.query, + schema = TypeDefinition.STRING + ) + ) + get = GetInfo.builder { + summary(defaultPathSummary) + description(defaultPathDescription) + response { + description(defaultResponseDescription) + responseCode(HttpStatusCode.OK) + responseType() + } + } + } + } + } + } + } +} diff --git a/core/src/test/resources/T0068__param_wrapper.json b/core/src/test/resources/T0068__param_wrapper.json new file mode 100644 index 000000000..80cf32fb3 --- /dev/null +++ b/core/src/test/resources/T0068__param_wrapper.json @@ -0,0 +1,82 @@ +{ + "openapi": "3.1.0", + "jsonSchemaDialect": "https://json-schema.org/draft/2020-12/schema", + "info": { + "title": "Test API", + "version": "1.33.7", + "description": "An amazing, fully-ish 😉 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/bkbnio/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": { + "/test": { + "get": { + "tags": [], + "summary": "Great Summary!", + "description": "testing more", + "parameters": [], + "responses": { + "200": { + "description": "A Successful Endeavor", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/TestResponse" + } + } + } + } + }, + "deprecated": false + }, + "parameters": [ + { + "name": "test", + "in": "query", + "schema": { + "type": "string" + }, + "required": true, + "deprecated": false + } + ] + } + }, + "webhooks": {}, + "components": { + "schemas": { + "TestResponse": { + "type": "object", + "properties": { + "c": { + "type": "string" + } + }, + "required": [ + "c" + ] + } + }, + "securitySchemes": {} + }, + "security": [], + "tags": [] +}