-
Notifications
You must be signed in to change notification settings - Fork 666
Closed
Description
Describe the bug
KClass.serializerOrNull() in kotlinx.serialization returns null when T is sealed interface.
SomeSealedInterface::class.serializerOrNull() must be same as SomeSealedInterface.serializer().
This bug makes difference result between inline and non-inline functions.
I suspect here is a problem point.
In this code, check jClass.isInterface before invokeSerializerOnCompanion.
However, after support sealed interface in kotlin 1.6.20, interface could be has serialzer() method in companion object.
So that, simple fix is changing order of those.
But it could make another problem. I don't know details.
To Reproduce
import kotlinx.serialization.*
import kotlinx.serialization.json.*
@Serializable
sealed class AA
@Serializable
class BB: AA()
@Serializable
class CC: AA()
@Serializable
sealed interface A
@Serializable
class B : A
@Serializable
class C : A
@OptIn(InternalSerializationApi::class)
fun main(args: Array<String>) {
A::class.serializerOrNull() // null
A.serializer() // SealedClassSerializer
println(A.serializer() == A::class.serializerOrNull()) // false
AA.serializer() == AA::class.serializerOrNull() // true
println(Json.encodeToString(A.serializer(), B())) // {"type":"B"}
println(Json.encodeToString<A>(B())) // kotlinx.serialization.SerializationException: Serializer for class 'A' is not found.
}Expected behavior
true
{"type":"B"}
{"type":"B"}
Environment
- Kotlin version: 1.6.20-M1
- Library version: 1.3.2
- Kotlin platforms: JVM
- Gradle version: 7.1
- IDE version (if bug is related to the IDE) [e.g. IntellijIDEA 2019.1, Android Studio 3.4]
- Other relevant context [e.g. OS version, JRE version, ... ]
Rattenkrieg and kodebach