Search code examples
kotlinserializationcomputed-propertieskotlinx.serializationkmongo

Delegate issue with Mongo and Kotlin


I've recently been using Kmongo library and Kotlin together however I've made an issue on Kmongo but I'm unsure it is related to the library.

I'm trying to persist my data onto my mongo database (version 4.2.2)

@Serializable
data class Person(val firstname: String, val lastname: String){

   val fullName
     get() = "$lastname $firstname"
}

When I insert the data I sent only an object like this : val person = Person("John", "Doe") but when I do check on my mongo database

db.persons.find()
> { "_id" : ObjectId("5e2da298159243f9894d3834"), "firstname" : "John", "lastname" : "Doe", "fullName" : "Doe John" }

How can I prevent to get fullName saved in my database ?

EDIT:

I tried to use @Transient annotation on my variable but it didn't work, and I got an inspection message saying : Property does not have backing field which makes it non-serializable and therefore @Transient is redundant


Solution

  • At this day, the Kmongo library use Jackson in the background, to avoid a property to be parsed I had to use : https://www.concretepage.com/jackson-api/jackson-jsonignore-jsonignoreproperties-and-jsonignoretype

    so the code for my data class is now :

    @Serialiable
    @JsonIgnoreProperties("fullName")
    data class Person(...