avro-kotlin / avro4k

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

Adding support for sealed interfaces and mixed polymorphic class structures #111

Closed thake closed 3 years ago

williamboxhall commented 3 years ago

Feel free to copy over my updated unit test here https://github.com/avro-kotlin/avro4k/pull/112/files#diff-4d18e413ddb29de46daf613ae38923daede47284df555e94b9c4e6b64a83b846 to have coverage over sealed subclasses

williamboxhall commented 3 years ago

@thake I pulled down this branch locally and added my aforementioned test and it still fails in the same way:

Contextual or default serializer not found for kotlinx.serialization.Sealed<SealedChildTwo>(com.github.avrokotlin.avro4k.schema.UnsealedChildTwo: com.github.avrokotlin.avro4k.schema.UnsealedChildTwo)
java.lang.IllegalArgumentException: Contextual or default serializer not found for kotlinx.serialization.Sealed<SealedChildTwo>(com.github.avrokotlin.avro4k.schema.UnsealedChildTwo: com.github.avrokotlin.avro4k.schema.UnsealedChildTwo) 
    at com.github.avrokotlin.avro4k.schema.SchemaForKt.schemaFor(SchemaFor.kt:189)
    at com.github.avrokotlin.avro4k.schema.ClassSchemaFor.buildField(ClassSchemaFor.kt:78)
    at com.github.avrokotlin.avro4k.schema.ClassSchemaFor.dataClassSchema(ClassSchemaFor.kt:63)
    at com.github.avrokotlin.avro4k.schema.ClassSchemaFor.schema(ClassSchemaFor.kt:43)
    at com.github.avrokotlin.avro4k.schema.UnionSchemaFor.schema(UnionSchemaFor.kt:22)
    at com.github.avrokotlin.avro4k.Avro.schema(Avro.kt:264)
    at com.github.avrokotlin.avro4k.Avro.schema(Avro.kt:269)
    at com.github.avrokotlin.avro4k.schema.PolymorphicClassSchemaTest$1$1.invokeSuspend(PolymorphicClassSchemaTest.kt:38)

the problem appears to be that the SerialialDescriptor coming out of kotlinx.serialization has not captured the underlying KClass (in this example should be SealedChildTwo::class) and so it's not able to derive the correct serializer. See my hacky workaround for an example of how to fix this but I pray there is a better way: https://github.com/avro-kotlin/avro4k/pull/112/files#diff-3dee9bfb579ea948c548eb6f42948261ae123238b3e08eaca553153bf95fa518

thake commented 3 years ago

@williamboxhall I added the test case to this PR and made a small fix for it. It should work now. Can you have another look?

williamboxhall commented 3 years ago

Thanks heaps for merging this @thake - were you also thinking of following it up with a quick 1.5.0 release?

thake commented 3 years ago

@williamboxhall the release is being built. Should be available on maven central within one hour.

williamboxhall commented 3 years ago

Amazing thanks heaps @thake