From 9f005208ce7b11b9874f025b01c0cceb7beaa930 Mon Sep 17 00:00:00 2001 From: Thiago Santos Date: Tue, 13 Aug 2024 09:51:41 -0300 Subject: [PATCH] fix: type-safe routing limitation There was a limitation routing from parent to child route using type-safe types. --- .../routing/resources/ResourcesRouting.kt | 7 ++- .../routing/resources/ResourcesTest.kt | 44 +++++++++++++++++++ .../routing/resources/helper/Path.kt | 9 ++++ 3 files changed, 59 insertions(+), 1 deletion(-) diff --git a/resources/common/src/dev/programadorthi/routing/resources/ResourcesRouting.kt b/resources/common/src/dev/programadorthi/routing/resources/ResourcesRouting.kt index 5b2fe32..a701e1b 100644 --- a/resources/common/src/dev/programadorthi/routing/resources/ResourcesRouting.kt +++ b/resources/common/src/dev/programadorthi/routing/resources/ResourcesRouting.kt @@ -85,8 +85,13 @@ public fun Route.resource( val path = resources.resourcesFormat.encodeToPathPattern(serializer) val queryParameters = resources.resourcesFormat.encodeToQueryParameters(serializer) var route = this + var routePath = route.toString() + if (routePath.startsWith("/")) { + routePath = routePath.removePrefix("/") + } + val finalPath = path.removePrefix(routePath) // Required for register to parents - route(path = path, name = null) { + route(path = finalPath, name = null) { route = queryParameters.fold(this) { entry, query -> val selector = diff --git a/resources/common/test/dev/programadorthi/routing/resources/ResourcesTest.kt b/resources/common/test/dev/programadorthi/routing/resources/ResourcesTest.kt index 76c806b..f63553b 100644 --- a/resources/common/test/dev/programadorthi/routing/resources/ResourcesTest.kt +++ b/resources/common/test/dev/programadorthi/routing/resources/ResourcesTest.kt @@ -11,6 +11,7 @@ import dev.programadorthi.routing.core.call import dev.programadorthi.routing.core.errors.RouteNotFoundException import dev.programadorthi.routing.core.install import dev.programadorthi.routing.core.routing +import dev.programadorthi.routing.resources.helper.ParentRouting import dev.programadorthi.routing.resources.helper.Path import io.ktor.http.Parameters import io.ktor.http.parametersOf @@ -469,4 +470,47 @@ class ResourcesTest { assertEquals(parametersOf(), result?.parameters) assertEquals("body content", body, "should have received a body") } + + @Test + fun shouldDoParentToChildRoutingUsingChildType() = + runTest { + // GIVEN + val job = Job() + var result: ApplicationCall? = null + + val parent = + routing( + rootPath = "/parent", + parentCoroutineContext = coroutineContext + job, + ) { + install(Resources) + + handle { + } + } + + routing( + rootPath = "/child", + parentCoroutineContext = coroutineContext + job, + parent = parent, + ) { + install(Resources) + + handle { + result = call + job.complete() + } + } + + // WHEN + parent.call(resource = ParentRouting.ChildRouting.Destination()) + advanceTimeBy(99) + + // THEN + assertNotNull(result) + assertEquals("/parent/child/destination", "${result?.uri}") + assertEquals("", "${result?.name}") + assertEquals(RouteMethod.Empty, result?.routeMethod) + assertEquals(Parameters.Empty, result?.parameters) + } } diff --git a/resources/common/test/dev/programadorthi/routing/resources/helper/Path.kt b/resources/common/test/dev/programadorthi/routing/resources/helper/Path.kt index 22f57d9..8361bce 100644 --- a/resources/common/test/dev/programadorthi/routing/resources/helper/Path.kt +++ b/resources/common/test/dev/programadorthi/routing/resources/helper/Path.kt @@ -7,3 +7,12 @@ class Path { @Resource("{id}") class Id(val parent: Path = Path(), val id: Int) } + +@Resource("/parent") +class ParentRouting { + @Resource("/child") + class ChildRouting(val parent: ParentRouting = ParentRouting()) { + @Resource("/destination") + class Destination(val parent: ChildRouting = ChildRouting()) + } +}