Fixed missing fallback serializer in contextual serializer

Fixes Kotlin/kotlinx.serialization#2158

Merge-request: KT-MR-8338
Merged-by: Sergey Shanshin <Sergey.Shanshin@jetbrains.com>
This commit is contained in:
Sergey.Shanshin
2023-01-18 15:47:26 +00:00
committed by Space Team
parent da24e5559b
commit d063db3ce0
4 changed files with 42 additions and 0 deletions
@@ -467,6 +467,14 @@ abstract class BaseIrGenerator(private val currentClass: IrClass, final override
typeArgs = listOf(kType)
}
contextSerializerId -> {
// don't create an instance if the serializer is being created for the cache
if (genericIndex == null && kType.genericIndex != null) {
// if context serializer parametrized by generic type (kType.genericIndex != null)
// and generic types are not allowed (always genericIndex == null for cache)
// then serializer can't be cached
return null
}
args = listOf(classReference(kType.classOrUpperBound()!!))
typeArgs = listOf(kType)
@@ -0,0 +1,22 @@
// TARGET_BACKEND: JVM_IR
// WITH_STDLIB
import kotlinx.serialization.*
import kotlinx.serialization.json.*
import kotlinx.serialization.encoding.*
@Serializable
data class Holder<T>(
val ok: Boolean,
@Contextual
val result: T?
)
fun box(): String {
val serializer = serializer<Holder<List<String>>>()
val instance = Holder(true, listOf("a", "b"))
val encoded = Json.encodeToString(serializer, instance)
if (encoded != """{"ok":true,"result":["a","b"]}""") return encoded
return "OK"
}
@@ -51,6 +51,12 @@ public class SerializationFirBlackBoxTestGenerated extends AbstractSerialization
runTest("plugins/kotlinx-serialization/testData/boxIr/contextualByDefault.kt");
}
@Test
@TestMetadata("contextualFallback.kt")
public void testContextualFallback() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/contextualFallback.kt");
}
@Test
@TestMetadata("enumsAreCached.kt")
public void testEnumsAreCached() throws Exception {
@@ -49,6 +49,12 @@ public class SerializationIrBoxTestGenerated extends AbstractSerializationIrBoxT
runTest("plugins/kotlinx-serialization/testData/boxIr/contextualByDefault.kt");
}
@Test
@TestMetadata("contextualFallback.kt")
public void testContextualFallback() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/contextualFallback.kt");
}
@Test
@TestMetadata("enumsAreCached.kt")
public void testEnumsAreCached() throws Exception {