From ff27e039837e4a00e2f84fa799d2e4e481ea8f73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20=E2=80=9CCLOVIS=E2=80=9D=20Canet?= Date: Wed, 3 Jun 2026 20:38:05 +0200 Subject: [PATCH] breaking(api): Replace AnyEndpoint types from KClass to KType KotlinX.Serialization allows looking up serializers based on the KType (since it has information on type parameters) but not on the KClass (since it doesn't). Additionally, KType.classifier is the KClass, so no information is lost. Accessing KotlinX.Serialization serializers from an endpoint will be useful, for example for OpenAPI generation. --- api/src/commonMain/kotlin/Endpoint.kt | 37 ++++++++++++++++----------- api/src/commonMain/kotlin/Resource.kt | 20 ++++++++------- 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/api/src/commonMain/kotlin/Endpoint.kt b/api/src/commonMain/kotlin/Endpoint.kt index de72bc1..13bc9e3 100644 --- a/api/src/commonMain/kotlin/Endpoint.kt +++ b/api/src/commonMain/kotlin/Endpoint.kt @@ -1,8 +1,9 @@ package opensavvy.spine.api import io.ktor.http.* -import kotlin.reflect.KClass import kotlin.reflect.KProperty +import kotlin.reflect.KType +import kotlin.reflect.typeOf /** * A specific HTTP [method] in a [resource]. @@ -135,12 +136,12 @@ sealed interface AnyEndpoint { /** * The type of the body went the client sends data to the server. */ - val requestType: KClass<*> + val requestType: KType /** * The type of the body when the server responds to a client. */ - val responseType: KClass<*> + val responseType: KType /** * Constructor for query parameters. @@ -178,6 +179,8 @@ sealed interface AnyEndpoint { * Note that you may need to perform some configuration on the Ktor side before using some types, see the * official documentation for instructions. * + * Do not specify the [type] parameter. It will be passed automatically when declaring the endpoint. + * * ### Example * * ```kotlin @@ -190,7 +193,7 @@ sealed interface AnyEndpoint { * * @see response */ - fun request(kClass: KClass): Builder + fun request(type: KType): Builder /** * Declares the response body type. @@ -202,6 +205,8 @@ sealed interface AnyEndpoint { * Note that you may need to perform some configuration on the Ktor side before using some types, see the * official documentation for instructions. * + * Do not specify the [type] parameter. It will be passed automatically when declaring the endpoint. + * * ### Example * * ```kotlin @@ -214,7 +219,7 @@ sealed interface AnyEndpoint { * * @see request */ - fun response(kClass: KClass): Builder + fun response(type: KType): Builder /** * Declares query parameters that the client will need to provide to the server. @@ -320,8 +325,8 @@ class Endpoint override val resource: Resource, override val method: HttpMethod, override val path: Path.Segment?, - override val requestType: KClass, - override val responseType: KClass, + override val requestType: KType, + override val responseType: KType, override val failureSpec: Failure, override val buildParameters: ParameterConstructor, ) : AnyEndpoint { @@ -345,26 +350,28 @@ class Endpoint private val onCreate: (AnyEndpoint) -> Unit, ) : AnyEndpoint.Builder { - override fun request(kClass: KClass) = EndpointBuilder( - Endpoint(endpoint.resource, endpoint.method, endpoint.path, kClass, endpoint.responseType, endpoint.failureSpec, endpoint.buildParameters), + override fun request(type: KType): EndpointBuilder = EndpointBuilder( + Endpoint(endpoint.resource, endpoint.method, endpoint.path, type, endpoint.responseType, endpoint.failureSpec, endpoint.buildParameters), onCreate ) - inline fun request() = request(T::class) + inline fun request(): EndpointBuilder = + request(typeOf()) - override fun response(kClass: KClass) = EndpointBuilder( - Endpoint(endpoint.resource, endpoint.method, endpoint.path, endpoint.requestType, kClass, endpoint.failureSpec, endpoint.buildParameters), + override fun response(type: KType): EndpointBuilder = EndpointBuilder( + Endpoint(endpoint.resource, endpoint.method, endpoint.path, endpoint.requestType, type, endpoint.failureSpec, endpoint.buildParameters), onCreate ) - inline fun response() = response(T::class) + inline fun response(): EndpointBuilder = + response(typeOf()) - override fun

parameters(build: (ParameterStorage) -> P) = EndpointBuilder( + override fun

parameters(build: (ParameterStorage) -> P): EndpointBuilder = EndpointBuilder( Endpoint(endpoint.resource, endpoint.method, endpoint.path, endpoint.requestType, endpoint.responseType, endpoint.failureSpec, build), onCreate ) - override fun failure(spec: S) = EndpointBuilder( + override fun failure(spec: S): EndpointBuilder, Params> = EndpointBuilder( Endpoint(endpoint.resource, endpoint.method, endpoint.path, endpoint.requestType, endpoint.responseType, endpoint.failureSpec + spec, endpoint.buildParameters), onCreate ) diff --git a/api/src/commonMain/kotlin/Resource.kt b/api/src/commonMain/kotlin/Resource.kt index 932dac2..6ffab07 100644 --- a/api/src/commonMain/kotlin/Resource.kt +++ b/api/src/commonMain/kotlin/Resource.kt @@ -1,6 +1,7 @@ package opensavvy.spine.api import io.ktor.http.* +import kotlin.reflect.typeOf /** * Common parent for all resource types. @@ -72,15 +73,16 @@ sealed class Resource( get() = _endpoints.asSequence() @Suppress("DEPRECATION_ERROR") - private fun endpoint(method: HttpMethod, path: String? = null) = Endpoint( - resource = this@Resource, - method = method, - path = path?.let(Path::Segment), - requestType = Unit::class, - responseType = Unit::class, - buildParameters = { Parameters.Empty }, - failureSpec = FailureSpec.Never, - ).asBuilder { _endpoints += it } + private fun endpoint(method: HttpMethod, path: String? = null): Endpoint.EndpointBuilder = + Endpoint( + resource = this@Resource, + method = method, + path = path?.let(Path::Segment), + requestType = typeOf(), + responseType = typeOf(), + buildParameters = { Parameters.Empty }, + failureSpec = FailureSpec.Never, + ).asBuilder { _endpoints += it } /** * Creates a [`GET`][HttpMethod.Get] HTTP endpoint in this resource. -- knotmirror 0.0.1