Skip to content

Ktor client using KxS throws unhelpful error when trying to decode non-serializable class boxed in a generic container #4675

Open
@Kantis

Description

@Kantis

Given that I make any request that tries to decode something to a List<T>, where T is not serializable, Ktor-client will generate an error like this:

kotlinx.serialization.SerializationException: Serializer for class 'List' is not found.
Please ensure that class is marked as '@Serializable' and that the serialization compiler plugin is applied.

	at kotlinx.serialization.internal.Platform_commonKt.serializerNotRegistered(Platform.common.kt:90)
	at kotlinx.serialization.SerializersKt__SerializersKt.serializer(Serializers.kt:299)
	at kotlinx.serialization.SerializersKt.serializer(Unknown Source)
	at io.ktor.serialization.kotlinx.SerializerLookupKt.serializerForTypeInfo(SerializerLookup.kt:32)
	at io.ktor.serialization.kotlinx.KotlinxSerializationConverter.deserialize(KotlinxSerializationConverter.kt:66)
	at io.ktor.serialization.ContentConverterKt$deserialize$$inlined$map$1$2.emit(Emitters.kt:51)

Adding @Serializable to T fixed this in my case.

It's very frustrating and throws the user in the wrong direction. It seems to apply to all boxed types. For instance

data class Foo(val bar: Int)

@Serializable
data class Box<out T>(val value: T)

fun main() {
    client.get("").body<Box<Foo>>()
}

Will generate a similar error statingkotlinx.serialization.SerializationException: Serializer for class 'Box' is not found.
Once again, making Foo serializable would fix this


Ktor version: 3.1.0
KxS version: 1.8.0
Kotlin version: 2.1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions