avro-kotlin / avro4k

Avro format support for Kotlin
Apache License 2.0
198 stars 37 forks source link

Unable to deserialize Map<String, List<ByteArray>> #255

Closed dmitrii-afanasyev closed 1 month ago

dmitrii-afanasyev commented 2 months ago

Describe the bug This is very similar to: https://github.com/avro-kotlin/avro4k/issues/76

Deserialization of the following class fails:

@Serializable
data class Test(
    val headers:  Map<String, List<ByteArray>>
)

Stack trace

Caused by: java.lang.ClassCastException: class java.nio.HeapByteBuffer cannot be cast to class org.apache.avro.generic.GenericArray (java.nio.HeapByteBuffer is in module java.base of loader 'bootstrap'; org.apache.avro.generic.GenericArray is in unnamed module of loader 'app')
    at com.github.avrokotlin.avro4k.decoder.ListDecoder.beginStructure(ListDecoder.kt:90)
    at kotlinx.serialization.internal.AbstractCollectionSerializer.merge(CollectionSerializers.kt:29)
    at kotlinx.serialization.internal.PrimitiveArraySerializer.deserialize(CollectionSerializers.kt:179)
    at com.github.avrokotlin.avro4k.decoder.ListDecoder.decodeSerializableValue(ListDecoder.kt:83)
    at kotlinx.serialization.encoding.AbstractDecoder.decodeSerializableValue(AbstractDecoder.kt:43)
    at kotlinx.serialization.encoding.AbstractDecoder.decodeSerializableElement(AbstractDecoder.kt:70)
    at kotlinx.serialization.encoding.CompositeDecoder$DefaultImpls.decodeSerializableElement$default(Decoding.kt:538)
    at kotlinx.serialization.internal.CollectionLikeSerializer.readElement(CollectionSerializers.kt:80)
    at kotlinx.serialization.internal.AbstractCollectionSerializer.readElement$default(CollectionSerializers.kt:51)
    at kotlinx.serialization.internal.AbstractCollectionSerializer.merge(CollectionSerializers.kt:36)
    at kotlinx.serialization.internal.AbstractCollectionSerializer.deserialize(CollectionSerializers.kt:43)
    at kotlinx.serialization.encoding.Decoder$DefaultImpls.decodeSerializableValue(Decoding.kt:257)
    at kotlinx.serialization.encoding.AbstractDecoder.decodeSerializableValue(AbstractDecoder.kt:16)
    at kotlinx.serialization.encoding.AbstractDecoder.decodeSerializableValue(AbstractDecoder.kt:43)
    at kotlinx.serialization.encoding.AbstractDecoder.decodeSerializableElement(AbstractDecoder.kt:70)
    at kotlinx.serialization.encoding.CompositeDecoder$DefaultImpls.decodeSerializableElement$default(Decoding.kt:538)
    at kotlinx.serialization.internal.MapLikeSerializer.readElement(CollectionSerializers.kt:111)
    at kotlinx.serialization.internal.MapLikeSerializer.readElement(CollectionSerializers.kt:84)
    at kotlinx.serialization.internal.AbstractCollectionSerializer.readElement$default(CollectionSerializers.kt:51)
    at kotlinx.serialization.internal.AbstractCollectionSerializer.merge(CollectionSerializers.kt:36)
    at kotlinx.serialization.internal.AbstractCollectionSerializer.deserialize(CollectionSerializers.kt:43)
    at kotlinx.serialization.encoding.Decoder$DefaultImpls.decodeSerializableValue(Decoding.kt:257)
    at kotlinx.serialization.encoding.AbstractDecoder.decodeSerializableValue(AbstractDecoder.kt:16)
    at kotlinx.serialization.encoding.AbstractDecoder.decodeSerializableValue(AbstractDecoder.kt:43)
    at kotlinx.serialization.encoding.AbstractDecoder.decodeNullableSerializableElement(AbstractDecoder.kt:78)

Expected behavior It should be possible to serialize/deserialize lists of bytearrays.

Desktop

Additional context I believe ListDecoder should also check for an element type and has custom logic for ByteArray/ByteBuffer, etc. like RecordDecoder does.

Chuckame commented 2 months ago

Hello, previously, in avro4k v1, a bytearray was handled as an array. Now in v2 it is handled as bytes/fixed, so I recommend you to migrate to v2. Also a lot of optimizations, compatibility and bug fixes has been shipped in v2. Please tell me if it works as expected after upgrading 🚀

Chuckame commented 1 month ago

@dmitrii-afanasyev did you try v2?

Chuckame commented 1 month ago

Closing this ticket as no answer sent, while there should already be a fix: migrating to v2. Don't hesitate to re-open this ticket if there is still issues.