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

Parent classes @BsonId not being recognized on collection.save #128

Closed
sepatel opened this issue Apr 9, 2019 · 4 comments
Closed

Parent classes @BsonId not being recognized on collection.save #128

sepatel opened this issue Apr 9, 2019 · 4 comments

Comments

@sepatel
Copy link

sepatel commented Apr 9, 2019

Tested with version 3.6.2 and 3.10.1 that this is the case.

If you take a scenario like

abstract class ReportableData(@BsonId val id: Any) {
    var metadata: Map<String, Any?> = emptyMap()
    var updated: Date = Date()
    var dirty: Boolean = false
}

open class Ticket(id: String) : ReportableData(id) {
    var title: String? = null
    var status: String? = null
    var opened: Date? = null
    var closed: Date? = null
}

then if you go to save it with something like

val collection = db.getCollection<Ticket>()
    .withReadPreference(ReadPreference.primary())
val ticket = Ticket("apple").apply {
  title = "Testing"
  status = "Closed"
}
collection.save(ticket)

it will work the first time saving the item including the id correctly. The 2nd time you run the program it will error out saying that document with id "apple" already exists.

When you walk through the code in the save it seems that the
val id = KMongoUtil.getIdValue(document)
pulls back a null for the id. When you dig into what it is doing you see it goes to a function that does
val idProperty = ClassMappingType.findIdProperty(value.javaClass.kotlin)
and that is pulling back
for the idProperty a value correlated to val blah.blah.blah.Ticket.id: kotlin.String

However, the id on the Ticket object doesn't have the @BsonId on it. It should have gone to the parent object and pulled the id from it instead. It clearly works fine when it goes into the insertOne mode and lets the native mongo drivers save the document. It is just that the custom code for identifying the idProperty is not looking for the @BsonId annotated field for its identifier. And even though the Ticket's version of the "id" property is nothing more then a passthrough, it is getting tagged with it.

If I change the Ticket to say

open class Ticket(@BsonId override val id: String) : ReportableData(id) {
    var title: String? = null
    var status: String? = null
    var opened: Date? = null
    var closed: Date? = null
}

then the save call works as expected. Looks like the issue is with the logic in trying to find the Id property in that it assumes that "id" field is the id even though there is an annotated bsonid which is the real thing that gets used by the native java mongo driver. This creates for a contradiction of behavior.

@sepatel
Copy link
Author

sepatel commented Apr 9, 2019

I should probably point out that the .json extension also works correctly in marking the correct thing as _id

@zigzago
Copy link
Member

zigzago commented Apr 10, 2019

Thanks for reporting. The fix is in the snapshot.

@sepatel
Copy link
Author

sepatel commented Apr 11, 2019

Validated it works. Is there an ETA for when the next release might be coming up? Maybe something within the next 3 to 4 weeks?

@zigzago
Copy link
Member

zigzago commented Apr 11, 2019

As kotlin 1.3.30 is released today and mongo java driver 3.10.2 was released a few days ago, I release a 3.10.1 right now

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

No branches or pull requests

2 participants