fix root route appending a trailing slash (#30)

This commit is contained in:
Ryan Brink
2021-04-19 14:47:35 -04:00
committed by GitHub
parent dd978276d2
commit a3cf7dad9b
4 changed files with 123 additions and 2 deletions

View File

@ -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

View File

@ -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<TestParams, TestResponse>(testGetInfo) {
call.respondText { "🤔🔥" }
}
}
}
}
}
private fun Application.trailingSlash() {
routing {
route("/test") {

View File

@ -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" : [ ]
}