diff --git a/krpc/krpc-core/src/commonMain/kotlin/kotlinx/rpc/krpc/internal/SerializationUtils.kt b/krpc/krpc-core/src/commonMain/kotlin/kotlinx/rpc/krpc/internal/SerializationUtils.kt index 5ed02c722..5e1862702 100644 --- a/krpc/krpc-core/src/commonMain/kotlin/kotlinx/rpc/krpc/internal/SerializationUtils.kt +++ b/krpc/krpc-core/src/commonMain/kotlin/kotlinx/rpc/krpc/internal/SerializationUtils.kt @@ -10,6 +10,7 @@ import kotlinx.rpc.descriptor.RpcTypeKrpc import kotlinx.rpc.internal.rpcInternalKClass import kotlinx.rpc.internal.utils.InternalRpcApi import kotlinx.serialization.* +import kotlinx.serialization.builtins.nullable import kotlinx.serialization.descriptors.SerialDescriptor import kotlinx.serialization.descriptors.buildClassSerialDescriptor import kotlinx.serialization.encoding.* @@ -29,7 +30,7 @@ internal fun SerializersModule.buildContextualInternal(type: KType): KSerializer ) @Suppress("UNCHECKED_CAST") - return result as? KSerializer + return if (type.isMarkedNullable) result?.nullable else result as? KSerializer } private fun SerializersModule.buildContextual(type: KType): KSerializer { diff --git a/krpc/krpc-test/src/commonMain/kotlin/kotlinx/rpc/krpc/test/KrpcTestService.kt b/krpc/krpc-test/src/commonMain/kotlin/kotlinx/rpc/krpc/test/KrpcTestService.kt index d62538a1f..54f191724 100644 --- a/krpc/krpc-test/src/commonMain/kotlin/kotlinx/rpc/krpc/test/KrpcTestService.kt +++ b/krpc/krpc-test/src/commonMain/kotlin/kotlinx/rpc/krpc/test/KrpcTestService.kt @@ -68,6 +68,7 @@ interface KrpcTestService { suspend fun nonSerializableClassWithSerializer( localDateTime: @Serializable(LocalDateTimeSerializer::class) LocalDateTime, ): @Serializable(LocalDateTimeSerializer::class) LocalDateTime + suspend fun nullableNonSerializableClass(localDate: LocalDate?): LocalDate? suspend fun incomingStreamSyncCollect(arg1: Flow): Int suspend fun incomingStreamSyncCollectMultiple(arg1: Flow, arg2: Flow, arg3: Flow): Int diff --git a/krpc/krpc-test/src/commonMain/kotlin/kotlinx/rpc/krpc/test/KrpcTestServiceBackend.kt b/krpc/krpc-test/src/commonMain/kotlin/kotlinx/rpc/krpc/test/KrpcTestServiceBackend.kt index a8b924995..d626714bb 100644 --- a/krpc/krpc-test/src/commonMain/kotlin/kotlinx/rpc/krpc/test/KrpcTestServiceBackend.kt +++ b/krpc/krpc-test/src/commonMain/kotlin/kotlinx/rpc/krpc/test/KrpcTestServiceBackend.kt @@ -129,6 +129,10 @@ class KrpcTestServiceBackend : KrpcTestService { ) } + override suspend fun nullableNonSerializableClass(localDate: LocalDate?): LocalDate? { + return localDate?.let { LocalDate(it.year, it.month, it.day + 1) } + } + override suspend fun incomingStreamSyncCollect(arg1: Flow): Int { return arg1.count() } diff --git a/krpc/krpc-test/src/commonMain/kotlin/kotlinx/rpc/krpc/test/KrpcTransportTestBase.kt b/krpc/krpc-test/src/commonMain/kotlin/kotlinx/rpc/krpc/test/KrpcTransportTestBase.kt index ff32015c4..6b9b6fa46 100644 --- a/krpc/krpc-test/src/commonMain/kotlin/kotlinx/rpc/krpc/test/KrpcTransportTestBase.kt +++ b/krpc/krpc-test/src/commonMain/kotlin/kotlinx/rpc/krpc/test/KrpcTransportTestBase.kt @@ -176,6 +176,16 @@ abstract class KrpcTransportTestBase { ) } + @Test + open fun nullableNonSerializableParameter() = runTest { + val localDate = LocalDate(2001, 8, 23) + val resultDate = client.nonSerializableClass(localDate) + assertEquals(LocalDate(2001, 8, 24), resultDate) + + val resultNull = client.nullableNonSerializableClass(null) + assertNull(resultNull) + } + @Test fun doubleGenericReturnType() = runTest { val result = client.doubleGenericReturnType() diff --git a/krpc/krpc-test/src/commonTest/kotlin/kotlinx/rpc/krpc/test/LocalTransportTest.kt b/krpc/krpc-test/src/commonTest/kotlin/kotlinx/rpc/krpc/test/LocalTransportTest.kt index 7b6f0ebcf..1e8e30f84 100644 --- a/krpc/krpc-test/src/commonTest/kotlin/kotlinx/rpc/krpc/test/LocalTransportTest.kt +++ b/krpc/krpc-test/src/commonTest/kotlin/kotlinx/rpc/krpc/test/LocalTransportTest.kt @@ -54,6 +54,9 @@ class ProtoBufLocalTransportTest : LocalTransportTest() { @Test override fun nullableReturn(): TestResult = runTest { } + @Test + override fun nullableNonSerializableParameter(): TestResult = runTest { } + @Test override fun testByteArraySerialization(): TestResult = runTest { }