Skip to content

Commit

Permalink
Serialize null when passed to a ZiplineService function with a nu…
Browse files Browse the repository at this point in the history
…llable `@Contextual` parameter (#1146)
  • Loading branch information
veyndan authored Oct 16, 2023
1 parent 7c8a253 commit 5ef12c0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import app.cash.zipline.ZiplineFunction
import app.cash.zipline.ZiplineScoped
import app.cash.zipline.ZiplineService
import app.cash.zipline.ziplineServiceSerializer
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.builtins.serializer
Expand Down Expand Up @@ -225,6 +226,7 @@ internal class RealCallSerializer(
}
}

@OptIn(ExperimentalSerializationApi::class)
internal class ArgsListSerializer(
internal val serializers: List<KSerializer<*>>,
) : KSerializer<List<*>> {
Expand All @@ -235,7 +237,7 @@ internal class ArgsListSerializer(
encoder.encodeStructure(descriptor) {
for (i in serializers.indices) {
@Suppress("UNCHECKED_CAST") // We don't have a type argument T for each parameter.
encodeSerializableElement(descriptor, i, serializers[i] as KSerializer<Any?>, value[i])
encodeNullableSerializableElement(descriptor, i, serializers[i] as KSerializer<Any?>, value[i])
}
}
}
Expand All @@ -245,7 +247,7 @@ internal class ArgsListSerializer(
val result = mutableListOf<Any?>()
for (i in serializers.indices) {
check(decodeElementIndex(descriptor) == i)
result += decodeSerializableElement(descriptor, i, serializers[i])
result += decodeNullableSerializableElement(descriptor, i, serializers[i])
}
check(decodeElementIndex(descriptor) == DECODE_DONE)
return@decodeStructure result
Expand Down
24 changes: 24 additions & 0 deletions zipline/src/hostTest/kotlin/app/cash/zipline/ZiplineServiceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ import kotlinx.coroutines.Dispatchers.Unconfined
import kotlinx.coroutines.runBlocking
import kotlinx.serialization.Contextual
import kotlinx.serialization.Serializable
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.modules.SerializersModule
import kotlinx.serialization.modules.contextual

internal class ZiplineServiceTest {
@Test
Expand Down Expand Up @@ -134,6 +136,28 @@ internal class ZiplineServiceTest {
assertEquals(setOf("factory"), endpointA.serviceNames)
}

@Test
fun contextualNullableParameter() = runBlocking(Unconfined) {
val serializersModule = SerializersModule {
contextual(String.serializer())
}
val (endpointA, endpointB) = newEndpointPair(this, serializersModule)

endpointA.bind<ContextualNullableParameter>("factory", RealContextualNullableParameter())
val factoryClient = endpointB.take<ContextualNullableParameter>("factory")

factoryClient.create("hello")
factoryClient.create(null)
}

interface ContextualNullableParameter : ZiplineService {
fun create(string: @Contextual String?)
}

class RealContextualNullableParameter : ContextualNullableParameter {
override fun create(string: String?) = Unit
}

interface EchoServiceFactory : ZiplineService {
fun create(greeting: String): EchoService
}
Expand Down

0 comments on commit 5ef12c0

Please sign in to comment.