Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Look up the class discriminator value by its name in the PolymorphismDecoder #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jenssuhr
Copy link

Current state

The PolymorphismDecoder expects the first field in the BSON document (or the second, if the first one is the _id) to contain the class discriminator. If the discriminator field is at another position in the document, deserialization fails.

Example:
Parsing the following document

{
  "payload": {
    "_id": {
      "$oid": "5d1777814e8c7b408a6ada73"
    },
    "someData": "something",
    "___type": "com.github.jershell.kbson.models.polymorph.SMessage.DataWithObjectId"
  }
}

leads to

Serializer for subclass 'something' is not found in the polymorphic scope of 'SMessage'.
Check if class with serial name 'something' exists and serializer is registered in a corresponding SerializersModule.
To be registered automatically, class 'something' has to be '@Serializable', and the base class 'SMessage' has to be sealed and '@Serializable'.

Proposed solution

With this pull request, decodeElementIndex actively looks for a field with the configured class discriminator's name. It also allows removing the alreadyReadId parameters introduced in #19.
This change adds a few more read calls, but I think the tradeoff is worth it for a bit more robustness.

…Decoder

This allows correct deserialization of BSON documents where the discriminator is not one of the first two elements.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant