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

kotlinx.serialization: Id that wraps an ObjectId is not properly deserialized for an inherited class #249

Closed
zigzago opened this issue Nov 28, 2020 · 2 comments · Fixed by jershell/kbson#20
Labels

Comments

@zigzago
Copy link
Member

zigzago commented Nov 28, 2020

interface SchedulerJob {
    val id: Id<SchedulerJob>
    val action: String
}

@Serializable
data class UnmuteSchedulerJob(
    @Contextual @SerialName("_id")
   override val id: Id<SchedulerJob> = newId(),
   override val action:String
)

-> id is correctly persisted as ObjectId
-> but is deserialized as StringId (insead of WrappedObjectId)

it means that job.id != col.findOne()?.id

Workarounds:

  • store your Ids as String ( IdGenerator.defaultGenerator = ObjectIdToStringGenerator )
  • Or when comparing or searching by ids, ensure that ids are properly mapped (ie job.id == col.findOne()?.id?.run{ WrappedObjectId(toString()) } )
  • Or declare ObjectId instead of Id<*>
@dondish
Copy link

dondish commented Nov 28, 2020

I don't think it causes an exception though right?

@zigzago
Copy link
Member Author

zigzago commented Nov 28, 2020

I don't reproduce any exception with 4.2.2 KMongo version. If you get an exception with 4.2.2, please create a new ticket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants