[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>
This commit is contained in:
Sergey.Shanshin
2023-05-09 09:59:11 +00:00
committed by Space Team
parent 5a95d919c7
commit c9bde57e44
6 changed files with 55 additions and 4 deletions
+29
View File
@@ -0,0 +1,29 @@
// 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"
}