Don't fail if there is no serializer for type parameters of contextual serializer

Return null instead.
Such behaviour is needed to support cachedChildSerializers logic.
Since this field creator doesn't provide genericGetter (because it's
static),
type param serializer can't be retrieved, and the whole contextual
serializer shouldn't be cached.

#KT-58067 Fixed
This commit is contained in:
Leonid Startsev
2023-04-18 18:11:58 +02:00
committed by Space Team
parent bf757d3163
commit fa8f38c2c8
4 changed files with 57 additions and 1 deletions
@@ -519,7 +519,7 @@ abstract class BaseIrGenerator(private val currentClass: IrClass, final override
compilerContext,
it
)
instantiate(argSer, it)!!
instantiate(argSer, it) ?: return null
})
)
}
@@ -0,0 +1,44 @@
// TARGET_BACKEND: JVM_IR
// WITH_STDLIB
import kotlinx.serialization.*
import kotlinx.serialization.json.*
import kotlinx.serialization.descriptors.*
import kotlinx.serialization.modules.*
import kotlinx.serialization.encoding.*
class SomeData<T>(val t: T)
@Serializable
class PagedData<T>(
@Contextual val someData: SomeData<T>,
)
class SomeDataSerializer<T>(val tSer: KSerializer<T>) : KSerializer<SomeData<T>> {
override val descriptor: SerialDescriptor = buildClassSerialDescriptor("SomeData")
override fun serialize(encoder: Encoder, value: SomeData<T>) {
encoder as JsonEncoder
val data = encoder.json.encodeToJsonElement(tSer, value.t)
val obj = buildJsonObject {
put("innerType", tSer.descriptor.serialName)
put("data", data)
}
encoder.encodeJsonElement(obj)
}
override fun deserialize(decoder: Decoder): SomeData<T> {
TODO("Not yet implemented")
}
}
fun box(): String {
val module = SerializersModule {
contextual(SomeData::class) { args -> SomeDataSerializer(args[0]) }
}
val json = Json { serializersModule = module }
val input = PagedData<String>(SomeData("foo_bar"))
val enc = json.encodeToString(input)
return if (enc != """{"someData":{"innerType":"kotlin.String","data":"foo_bar"}}""") enc else "OK"
}
@@ -57,6 +57,12 @@ public class SerializationFirLightTreeBlackBoxTestGenerated extends AbstractSeri
runTest("plugins/kotlinx-serialization/testData/boxIr/contextualFallback.kt");
}
@Test
@TestMetadata("contextualWithTypeParameters.kt")
public void testContextualWithTypeParameters() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/contextualWithTypeParameters.kt");
}
@Test
@TestMetadata("delegatedInterface.kt")
public void testDelegatedInterface() throws Exception {
@@ -55,6 +55,12 @@ public class SerializationIrBoxTestGenerated extends AbstractSerializationIrBoxT
runTest("plugins/kotlinx-serialization/testData/boxIr/contextualFallback.kt");
}
@Test
@TestMetadata("contextualWithTypeParameters.kt")
public void testContextualWithTypeParameters() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/contextualWithTypeParameters.kt");
}
@Test
@TestMetadata("delegatedInterface.kt")
public void testDelegatedInterface() throws Exception {