Closed AlexCouch closed 1 year ago
Hey! This might just be the best, most detailed bug report I've ever gotten. Thanks for that! :P
Started poking around a little bit and I think the issue stems from here https://github.com/BenWoodworth/knbt/blob/70fa0864bca7684c6853844eb7bb61f425b5ebf7/src/commonMain/kotlin/NbtEncoder.kt#L91-L101
When I wrote this I didn't have my head fully wrapped around kotlinx.serialization's polymorphic serialization so didn't support it. And I thought this code would catch any polymorphism and fail with a nice friendly polite error... but (and I'm just now finding this out) there's a problem
Before we get to your error, execution gets to this point with serializer
being a SealedClassSerializer<Person>
, which extends AbstractPolymorphicSerializer
, but does not implement PolymorphicSerializer
. So it passes up that "not yet supported" case and gracefully delegates to the super class on line 100.
So that means my assumption here that descriptor.kind
is a StructureKind
fails:
https://github.com/BenWoodworth/knbt/blob/70fa0864bca7684c6853844eb7bb61f425b5ebf7/src/commonMain/kotlin/internal/DefaultNbtEncoder.kt#L26-L39
I pushed this commit to fix the polymorphic check: https://github.com/BenWoodworth/knbt/commit/be3621d3196ae3235dec105427fd6e0e969ba799
And with that your example now works, if you change father
and mother
to be Adult
s.
Other polymorphic serialization will still fail though since I haven't implemented support yet. I opened #13 so if you want support for that please toss your use cases (and perhaps test cases) so I have something to work towards :)
I'll post a new release when I get a chance, but until then knbt:0.11.0-SNAPSHOT
should have the changes I pushed soon!
I have found a problem regarding the poymorphic serialization in the default serializer. When encoding a serializable marked sealed class, it throws an exception with the following stack trace:
When looking at the line that threw the exception, it shows this:
The reason why it throws the error is because descriptor.kind is of type SerialKind, which is in fact a supertype of StructureKind, however, the type of descriptor.kind at this point is PolymorphicKind, which is not a subtype of StructureKind. They are on different parts of the hierarchy.
SerialKind | - PolymorphicKind | - ... |- StructureKind |- ...
My Nbt object setup looks like this:
However, this also crashes if I use the EmptySerializersModule.
I was able to reproduce it with the following code:
The okio code which is commented was the original code I used to reproduce it but I decided tried it for just a regular bytearray encode/decode call. Of course it would be same because this crash is in the default nbt encoder.