Litote / kmongo

[deprecated] KMongo - a Kotlin toolkit for Mongo
https://litote.org/kmongo/
Apache License 2.0
781 stars 74 forks source link

custom named Id field ignored #354

Closed NoZomIBK closed 2 years ago

NoZomIBK commented 2 years ago

I wanted to have my id field named simply id. But it seems the MongoId and the SerialName annotation do not really work. Using kmongo 4.6.1

This is my simple test code:

fun main(){
  val dashboard = TestData(value = "Foo")
  mongoDatabase.getCollection<TestData>().insertOne(dashboard)
}

data class TestData(@SerialName("_id") @MongoId val id: ObjectId = ObjectId(), val value: String)

My database is the latest docker image of mongo

document resulting in database is:

{
    "_id" : ObjectId("62c3207d6f25b061de6fd986"),
    "id" : ObjectId("62c3207ce7ac1437c6179739"),
    "value" : "Foo"
}

the id field is the actual field in my class, the _id is autogenerated

I also tried only SerialName or only MongoId, does not change anything.

zigzago commented 2 years ago

I do not reproduce the bug (see attached unit test). Do you use kmongo-serialization artefact? (as you use @SerialName I suppose, but ... ;) )

NoZomIBK commented 2 years ago

okay, that is actually the issue. Since some serialization obviously has to be done, even without the Serializable annotation on the database object, i did not expect anything like that.

maybe it would help to have a warning log message when objects with kotlin-serialize annotations are given to kmongo while no kmongo-serialization is used.

zigzago commented 2 years ago

Ok thank you ! https://litote.org/kmongo/quick-start/#object-mapping-engine