-
Notifications
You must be signed in to change notification settings - Fork 660
Open
Labels
Description
Describe the bug
I previously reported a bug. Simply put, when a class with generics has a field whose type is a generic interface, and the implementing class of the interface also has generics, it would cause an error.
It has been fixed, but there's still a slight change that causes errors:
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
@Serializable
sealed interface I<out T>
@Serializable
data class A<out T>(val x: T) : I<T>
@Serializable
data class B<out T>(val x: List<I<T>>)
/*
The B class from the previous issue:
@Serializable
data class B<out T>(val x: I<T>)
This can now be deserialized normally, but when changing I<T> to List<I<T>>, it fails again
*/
fun main()
{
val a: I<Int> = A(42)
val b: B<Int> = B(listOf(a))
val json = Json { prettyPrint = true }
val jsonB = json.encodeToString(b)
// failed
runCatching { json.decodeFromString<B<Int>>(jsonB) }.onFailure(Throwable::printStackTrace).onSuccess(::println)
}
Exception in thread "main" kotlinx.serialization.SerializationException: Serializer for subclass 'Int' is not found in the polymorphic scope of 'I'.
Check if class with serial name 'Int' exists and serializer is registered in a corresponding SerializersModule.
To be registered automatically, class 'Int' has to be '@Serializable', and the base class 'I' has to be sealed and '@Serializable'.
Expected behavior
no error and successful serialization
Environment
- Kotlin version: 2.2.10
- Library version: 1.9.0
- Kotlin platforms: JVM
- Gradle version: 8.11