Skip to content

Commit 755a290

Browse files
authored
Support nullable contextual serializers (#392)
1 parent 0db8fb0 commit 755a290

File tree

5 files changed

+20
-1
lines changed

5 files changed

+20
-1
lines changed

krpc/krpc-core/src/commonMain/kotlin/kotlinx/rpc/krpc/internal/SerializationUtils.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import kotlinx.rpc.descriptor.RpcTypeKrpc
1010
import kotlinx.rpc.internal.rpcInternalKClass
1111
import kotlinx.rpc.internal.utils.InternalRpcApi
1212
import kotlinx.serialization.*
13+
import kotlinx.serialization.builtins.nullable
1314
import kotlinx.serialization.descriptors.SerialDescriptor
1415
import kotlinx.serialization.descriptors.buildClassSerialDescriptor
1516
import kotlinx.serialization.encoding.*
@@ -29,7 +30,7 @@ internal fun SerializersModule.buildContextualInternal(type: KType): KSerializer
2930
)
3031

3132
@Suppress("UNCHECKED_CAST")
32-
return result as? KSerializer<Any?>
33+
return if (type.isMarkedNullable) result?.nullable else result as? KSerializer<Any?>
3334
}
3435

3536
private fun SerializersModule.buildContextual(type: KType): KSerializer<Any?> {

krpc/krpc-test/src/commonMain/kotlin/kotlinx/rpc/krpc/test/KrpcTestService.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ interface KrpcTestService {
6868
suspend fun nonSerializableClassWithSerializer(
6969
localDateTime: @Serializable(LocalDateTimeSerializer::class) LocalDateTime,
7070
): @Serializable(LocalDateTimeSerializer::class) LocalDateTime
71+
suspend fun nullableNonSerializableClass(localDate: LocalDate?): LocalDate?
7172

7273
suspend fun incomingStreamSyncCollect(arg1: Flow<String>): Int
7374
suspend fun incomingStreamSyncCollectMultiple(arg1: Flow<String>, arg2: Flow<String>, arg3: Flow<String>): Int

krpc/krpc-test/src/commonMain/kotlin/kotlinx/rpc/krpc/test/KrpcTestServiceBackend.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ class KrpcTestServiceBackend : KrpcTestService {
129129
)
130130
}
131131

132+
override suspend fun nullableNonSerializableClass(localDate: LocalDate?): LocalDate? {
133+
return localDate?.let { LocalDate(it.year, it.month, it.day + 1) }
134+
}
135+
132136
override suspend fun incomingStreamSyncCollect(arg1: Flow<String>): Int {
133137
return arg1.count()
134138
}

krpc/krpc-test/src/commonMain/kotlin/kotlinx/rpc/krpc/test/KrpcTransportTestBase.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,16 @@ abstract class KrpcTransportTestBase {
176176
)
177177
}
178178

179+
@Test
180+
open fun nullableNonSerializableParameter() = runTest {
181+
val localDate = LocalDate(2001, 8, 23)
182+
val resultDate = client.nonSerializableClass(localDate)
183+
assertEquals(LocalDate(2001, 8, 24), resultDate)
184+
185+
val resultNull = client.nullableNonSerializableClass(null)
186+
assertNull(resultNull)
187+
}
188+
179189
@Test
180190
fun doubleGenericReturnType() = runTest {
181191
val result = client.doubleGenericReturnType()

krpc/krpc-test/src/commonTest/kotlin/kotlinx/rpc/krpc/test/LocalTransportTest.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ class ProtoBufLocalTransportTest : LocalTransportTest() {
5454
@Test
5555
override fun nullableReturn(): TestResult = runTest { }
5656

57+
@Test
58+
override fun nullableNonSerializableParameter(): TestResult = runTest { }
59+
5760
@Test
5861
override fun testByteArraySerialization(): TestResult = runTest { }
5962

0 commit comments

Comments
 (0)