Skip to content

Conversation

@mrf7
Copy link

@mrf7 mrf7 commented Dec 18, 2025

An attempt at adding support for typesafe routing support to the ktor server raise handlers. Temporarily named all the functions *OrError instead of *OrRaise because the conflicting overloads on params and type arguments were funky. Maybe there should be a module for the type safe routing variants and a module for the regular variants? Would be a little restrictive to only use one or the other without weird override conflicts Nevermind i tried the rename and it seems fine

@mrf7 mrf7 force-pushed the mrf/ktor-typesafe-routing branch from 55090a5 to f6bd738 Compare December 18, 2025 21:15
@mrf7 mrf7 force-pushed the mrf/ktor-typesafe-routing branch from f6bd738 to cb72f82 Compare December 18, 2025 21:15
@mrf7 mrf7 marked this pull request as draft December 18, 2025 21:16
@mrf7 mrf7 marked this pull request as ready for review December 18, 2025 21:25
@mrf7 mrf7 changed the title Mrf/ktor typesafe routing ktor typesafe routing Dec 19, 2025
@nomisRev nomisRev requested review from nomisRev and tKe December 20, 2025 10:57
Copy link
Collaborator

@tKe tKe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may want to consider using a separate module or variant to avoiding forcing a dependency on the resources plugin for consumers that aren't using it.

Can you also ensure the correct imports are used for the resources patch/post/put as currently they'll do the wrong thing and attempt to .recieve<TRoute>(). May be worth adding a test case for each to ensure the right things is being done.

Comment on lines +16 to +18
import io.ktor.server.routing.patch
import io.ktor.server.routing.post
import io.ktor.server.routing.put
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ These are pulling in the wrong handlers and will result in trying to call.receive<TRoute>() instead of using the resources plugin.

@@ -0,0 +1,79 @@
@file:Suppress("API_NOT_AVAILABLE")

package arrow.raise.ktor.server.routing.typesafe
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use the .resources package naming to be consistent with the ktor package?

I'm not sure if it's best to fully align with

package arrow.raise.ktor.server.resources

or keep to a subpackage of

package arrow.raise.ktor.server.routing.resources

import io.ktor.server.routing.put
import io.ktor.utils.io.KtorDsl

public typealias TypeSafeRespondingRaiseRoutingHandler<TRoute, TResponse> = suspend context(Raise<Response>) RoutingContext.(TRoute) -> TResponse
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we be consistent with this naming? We seem to have Resourced, TypeSafe, and Typed (for the tests). I suggest sticking with Resources to align with the Resources plugin.

Comment on lines +17 to +20
public fun interface ResourcedReceivingRespondingRaiseRoutingHandler<TRoute, TBody, TResponse> {
context(_: Raise<Response>)
public suspend fun RoutingContext.handle(route: TRoute, body: TBody): TResponse
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't need to be a fun interface and could just be a typealias for the appropriate lambda (making the usage below simpler).

The reason for the fun interface before was to rely on SAM usage to deprioritise it against the zero-arity equivalents without received parameters (as otherwise the compiler finds it ambiguous if between a no-arg lambda or a one-arg lambda with implicit it parameter).

In this case, both candidates should be distinct by needing either one (explicit or implicit) or two explicit parameters on the lambda.

commonMain {
dependencies {
api(libs.ktor.server.core)
implementation(libs.ktor.server.resources)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather we avoid bringing in a dependency on the ktor-server-resources plugin for all consumers of this library as it's unnecessary unless explicitly wanting to use this feature.

That said, I'm not sure if gradle feature variants are easily achievable with kotlin multiplatform libraries.

An alternative might be to add another arrow module with the resources support included, that depends on this one. Any thoughts @nomisRev / @serras?

@@ -0,0 +1,131 @@
package arrow.raise.ktor.server.response
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we relocate the tests to the same package?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants