Files
kotlin-fork/plugins/kotlinx-serialization/testData/boxIr/caching.kt
T
Sergey.Shanshin c9bde57e44 [KxSerialization] Fix NPE if sealed class has self-referencing property
Fixes Kotlin/kotlinx.serialization#2294

If the serializable class contains itself as a property, and its serializer is not an object (for example, for a sealed class), in this case there is a race in the initialization of the serializer and child serializers.

To avoid this the serialization class should not cache its own serializer as a child.

Merge-request: KT-MR-10019
Merged-by: Sergey Shanshin <Sergey.Shanshin@jetbrains.com>
2023-05-09 09:59:11 +00:00

30 lines
630 B
Kotlin
Vendored

// TARGET_BACKEND: JVM_IR
// WITH_STDLIB
import kotlinx.serialization.*
// Serializers for classes containing themselves as properties should be cached correctly
@Serializable
class Plain(val p: Plain? = null)
@Serializable
sealed class Sealed(val p: Sealed? = null)
@Serializable
open class Open(val p: Open? = null)
@Serializable
abstract class Abstract(val p: Abstract? = null)
fun box(): String {
// A correctly cached class must be initialized correctly in order to exclude cyclic nesting of caches
Plain.serializer()
Sealed.serializer()
Open.serializer()
Abstract.serializer()
return "OK"
}