-
Notifications
You must be signed in to change notification settings - Fork 75
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
Add updateOnlyNotNullProperties boolean in order to update only not null properties #115
Comments
On further investigation I think the issue is at override fun filterIdToBson(obj: Any): BsonDocument {
val bsonDocument = BsonDocument()
val bsonWriter = BsonDocumentWriter(bsonDocument)
codecRegistryWithNullSerialization // <--- HERE is always using WithNullSerialization
.get(obj.javaClass)
?.encode(
bsonWriter,
obj,
EncoderContext.builder().build()
)
bsonDocument.remove("_id")
return bsonDocument
} Maybe it needs to call |
codecRegistryWithNullSerialization was hardCoded. Trying to fix Litote#115
I've fixed the issue. Sending a pull request now. It was a mix of two things. So doing import org.bson.codecs.pojo.annotations.BsonId
import org.litote.kmongo.* //NEEDED! import KMongo extensions
import org.litote.kmongo.util.ObjectMappingConfiguration
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
class AppTest {
data class Document(
@BsonId val id: Int,
val field1: String,
val field2: String?
)
@Test fun testMongoBehaviours() {
val client = KMongo.createClient() //use coroutine extension
val database = client.getDatabase("test") //normal java driver usage
val col = database.getCollection<Document>() //KMongo extension method
ObjectMappingConfiguration.serializeNull = false
col.deleteMany()
run {
col.updateOne(Document(1, "Hello1", null), upsert())
val document = col.findOne(Document::id eq 1)
assertNotNull(document)
assertEquals("Hello1", document.field1)
assertEquals(null, document.field2)
}
run {
col.updateOne(Document(1, "Hello2", "World2"), upsert())
val document = col.findOne(Document::id eq 1)
assertNotNull(document)
assertEquals("Hello2", document.field1)
assertEquals("World2", document.field2)
}
run {
col.updateOne(Document(1, "Hello3", null), upsert())
val document = col.findOne(Document::id eq 1)
assertNotNull(document)
assertEquals("Hello3", document.field1)
assertEquals("World2", document.field2) // because it was null
}
}
} |
serializeNull = false
Hi,
I'm trying to perform partial updates based on the fact that
ObjectMappingConfiguration.serializeNull = false
. So i expect that my Document doesn't serialize field2 when it's value is null. Obviously i'm using kmongo-nativeAm I interpreting this feature wrong?? btw no documentation on this topic anywhere. Here is a Unit test I've made explaining the behavior
I get
The text was updated successfully, but these errors were encountered: