From 105ea6f082f4a3f7090cf06f325f4e0dc217dae4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20=E2=80=9CCLOVIS=E2=80=9D=20Canet?= Date: Fri, 14 Oct 2022 18:59:12 +0200 Subject: [PATCH 1/2] fix(spine): Do not allow returning successful values from validateCorrectId and validateId Closes https://gitlab.com/opensavvy/pedestal/-/issues/28 --- .../kotlin/opensavvy.spine.ktor.server/ServerTest.kt | 8 ++++---- spine/src/commonMain/kotlin/opensavvy.spine/Resource.kt | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/spine-ktor/spine-ktor-server/src/commonTest/kotlin/opensavvy.spine.ktor.server/ServerTest.kt b/spine-ktor/spine-ktor-server/src/commonTest/kotlin/opensavvy.spine.ktor.server/ServerTest.kt index 467c488..b3e11cb 100644 --- a/spine-ktor/spine-ktor-server/src/commonTest/kotlin/opensavvy.spine.ktor.server/ServerTest.kt +++ b/spine-ktor/spine-ktor-server/src/commonTest/kotlin/opensavvy.spine.ktor.server/ServerTest.kt @@ -78,7 +78,7 @@ class ServerTest { ensureFound(userIndex >= 0) { "Could not find user $id" } val user = users.removeAt(userIndex) users.add(user.copy(archived = true)) - emit(successful(user)) + emit(successful(Unit)) } route(api.users.id.unarchive, context) { @@ -86,14 +86,14 @@ class ServerTest { ensureFound(userIndex >= 0) { "Could not find user $id" } val user = users.removeAt(userIndex) users.add(user.copy(archived = false)) - emit(successful(user)) + emit(successful(Unit)) } route(api.users.id.delete, context) { val userIndex = users.indexOfFirst { it.id == id } ensureFound(userIndex >= 0) { "Could not find user $id" } - val user = users.removeAt(userIndex) - emit(successful(user)) + users.removeAt(userIndex) + emit(successful(Unit)) } } diff --git a/spine/src/commonMain/kotlin/opensavvy.spine/Resource.kt b/spine/src/commonMain/kotlin/opensavvy.spine/Resource.kt index 9ea2762..8a9ebfe 100644 --- a/spine/src/commonMain/kotlin/opensavvy.spine/Resource.kt +++ b/spine/src/commonMain/kotlin/opensavvy.spine/Resource.kt @@ -77,7 +77,7 @@ sealed class ResourceGroup { /** * Validates that [id] identifies this resource. */ - suspend fun StateBuilder.validateCorrectId(id: Id) { + suspend fun StateBuilder.validateCorrectId(id: Id) { ensureValid( id.service == service.name ) { "The passed identifier refers to the service '${id.service}', but this resource belongs to the service '${service.name}'" } @@ -122,7 +122,7 @@ sealed class ResourceGroup { * For example, you can override this function to check access rights for read operations. * By default, this function does nothing. */ - open suspend fun StateBuilder.validateId(id: Id, context: Context) {} + open suspend fun StateBuilder.validateId(id: Id, context: Context) {} protected fun create( route: Route? = null, -- 2.51.2 From 9fe89a91a5504f9281a056726fa2b7e47f7c9dbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20=E2=80=9CCLOVIS=E2=80=9D=20Canet?= Date: Fri, 14 Oct 2022 19:03:02 +0200 Subject: [PATCH 2/2] fix(spine): Added missing '/' on server-side resource For resource `/v2/departments/{department}` and action `PUT` on endpoint `/open`, the previous version would generate `v2/departments/{department}open` instead of `v2/departments/{department}/open`. Closes https://gitlab.com/opensavvy/pedestal/-/issues/28 --- .../kotlin/opensavvy.spine.ktor.server/Server.kt | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/spine-ktor/spine-ktor-server/src/commonMain/kotlin/opensavvy.spine.ktor.server/Server.kt b/spine-ktor/spine-ktor-server/src/commonMain/kotlin/opensavvy.spine.ktor.server/Server.kt index 3d330f8..4af8563 100644 --- a/spine-ktor/spine-ktor-server/src/commonMain/kotlin/opensavvy.spine.ktor.server/Server.kt +++ b/spine-ktor/spine-ktor-server/src/commonMain/kotlin/opensavvy.spine.ktor.server/Server.kt @@ -47,7 +47,14 @@ inline fun , crossinline block: suspend ResponseStateBuilder.() -> Unit, ) { - val path: String = operation.resource.routeTemplate + (operation.route ?: "") + val path = buildString { + append(operation.resource.routeTemplate) + + for (segment in operation.route?.segments ?: emptyList()) { + append('/') + append(segment.segment) + } + } val method = operation.kind.toHttp() route(path, method) { -- 2.51.2