SerializationException when using MapSerializer
I am using Avro.default.toRecord(serializer, data) with a MapSerializer(String.serializer(), String.serializer(). As it returns kotlinx.serialization.SerializationException: Unsupported root element passed to root record encoder I investigated and found the following snippet in
override fun beginStructure(descriptor: SerialDescriptor): CompositeEncoder {
return when (descriptor.kind) {
is StructureKind.CLASS -> RecordEncoder(schema, serializersModule, callback)
is PolymorphicKind -> UnionEncoder(schema,serializersModule,callback)
else -> throw SerializationException("Unsupported root element passed to root record encoder")
}
}
It seems that StructureKind.CLASS is the culprit. It contains an object of that type, but the kotlin is-comparator expects a type.
As a minimal example for reproduction, you can try this:
import kotlinx.serialization.SerializationException
import kotlinx.serialization.builtins.MapSerializer
import kotlinx.serialization.descriptors.PolymorphicKind
import kotlinx.serialization.descriptors.StructureKind
import kotlinx.serialization.builtins.serializer
println(
when (MapSerializer(String.serializer(), String.serializer()).descriptor.kind) {
is StructureKind -> "StructureKind.CLASS"
is PolymorphicKind -> "PolymorphicKind"
else -> throw SerializationException("Unsupported root element passed to root record encoder")
}
)
Is this a restriction of AVro itself? Not allowing a Map as root element?
Currently avro4k only supports data classes as roots. But it should be possible to also support all other types. Do you want to give it a try and provide a pr?
Closing stale issue. By the way, we are planning to handle non-record values
Closing this issue, as it will be covered by #187 and #114