-
Notifications
You must be signed in to change notification settings - Fork 620
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
2.0.20 with generatedSerializer and reference to interface: SealedClassSerializer init: subclassSerializers contains null #2759
Labels
Comments
Does this still present a problem if the custom serializers are not in the companion? There might be an initialisation loop here. |
Nope, I also tested it and it also does not work, I get the same error message. |
shanshin
added a commit
to JetBrains/kotlin
that referenced
this issue
Aug 6, 2024
…lizer feature with sealed classes When the heir of a sealed class contains this sealed class as a property, cyclic initialization occurs due to the fact that the SealedClassSerializer in the constructor accesses all passed subclass serializers. Initialization order: Parent Sealed class serializer \/ Create instance of SealedClassSerializer for Parent Sealed \/ Access subclass serializer in constructor of SealedClassSerializer \/ Call ChildClass.Compaion.generatedSerializer() \/ Init ChildClass \/ cache child serializers of ChildClass \/ Create instance of SealedClassSerializer for Parent Sealed ---> /\ Fixes Kotlin/kotlinx.serialization#2759
shanshin
added a commit
to JetBrains/kotlin
that referenced
this issue
Aug 6, 2024
…lizer feature with sealed classes When the heir of a sealed class contains this sealed class as a property, cyclic initialization occurs due to the fact that the SealedClassSerializer in the constructor accesses all passed subclass serializers. Initialization order: Parent Sealed class serializer \/ Create instance of SealedClassSerializer for Parent Sealed \/ Access subclass serializer in constructor of SealedClassSerializer \/ Call ChildClass.Compaion.generatedSerializer() \/ Init ChildClass \/ cache child serializers of ChildClass \/ Create instance of SealedClassSerializer for Parent Sealed ---> /\ Fixes Kotlin/kotlinx.serialization#2759
The fix does not work when using the interface in another type, eg in a Map: @Serializable
public sealed interface Schema {
@KeepGeneratedSerializer
@Serializable(with = OBJECT.CustomSerializer::class)
public data class OBJECT(
val properties: Map<String, Schema>,
) : Schema {
internal object CustomSerializer : KSerializer<OBJECT> by OBJECT.generatedSerializer()
}
}
@Test
fun decodeTest() {
Json.decodeFromString(Schema.serializer(), "")
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Describe the bug
This only happens if a child has a reference to the parent interface, in this case
SealedClassSerializer.subclassSerializers
has nullable entries, but it passes the init check because the array has the same length.To Reproduce
Expected behavior
No internal exception
Environment
The text was updated successfully, but these errors were encountered: