Skip to content

Commit

Permalink
fixes #319 ObjectMappingConfiguration.serializeNull = false & save/re…
Browse files Browse the repository at this point in the history
…place persist null
  • Loading branch information
zigzago committed Feb 12, 2022
1 parent cf887db commit b745653
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import org.litote.kmongo.util.KMongoUtil
import org.litote.kmongo.util.PairProjection
import org.litote.kmongo.util.SingleProjection
import org.litote.kmongo.util.TripleProjection
import org.litote.kmongo.util.UpdateConfiguration
import org.litote.kmongo.util.pairProjectionCodecRegistry
import org.litote.kmongo.util.singleProjectionCodecRegistry
import org.litote.kmongo.util.tripleProjectionCodecRegistry
Expand Down Expand Up @@ -403,12 +404,14 @@ fun <T> MongoCollection<T>.updateOne(
* @param filter a document describing the query filter
* @param update the update object
* @param options the options to apply to the update operation
* @param updateOnlyNotNullProperties if true do not change null properties
*/
fun <T> MongoCollection<T>.updateOne(
filter: String,
update: Any,
options: UpdateOptions = UpdateOptions()
): Publisher<UpdateResult> = updateOne(KMongoUtil.toBson(filter), KMongoUtil.setModifier(update), options)
options: UpdateOptions = UpdateOptions(),
updateOnlyNotNullProperties: Boolean = UpdateConfiguration.updateOnlyNotNullProperties
): Publisher<UpdateResult> = updateOne(KMongoUtil.toBson(filter), KMongoUtil.setModifier(update, updateOnlyNotNullProperties), options)

/**
* Update a single document in the collection according to the specified arguments.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,23 @@ class PersistingNotNullTest : KMongoBaseTest<NullableFriend>() {
assertFalse(doc.containsKey("address"))
}

@Category(JacksonMappingCategory::class)
@Test
fun testSaveNullFieldForJacksonMapping() {
val document = NullableFriend("Joe")
col.save(document)
val doc = database.getCollection("nullableFriend").findOne()!!

assertTrue(doc.containsKey("name"))
assertFalse(doc.containsKey("address"))

col.save(document)
val doc2 = database.getCollection("nullableFriend").findOne()!!

assertTrue(doc2.containsKey("name"))
assertFalse(doc2.containsKey("address"))
}

@Category(NativeMappingCategory::class)
@Test
fun testInsertNullFieldForNativeMapping() {
Expand All @@ -82,6 +99,23 @@ class PersistingNotNullTest : KMongoBaseTest<NullableFriend>() {
assertTrue(doc.containsKey("address"))
}

@Category(NativeMappingCategory::class)
@Test
fun testSaveNullFieldForNativeMapping() {
val document = NullableFriend("Joe")
col.save(document)
val doc = database.getCollection("nullableFriend").findOne()!!

assertTrue(doc.containsKey("name"))
assertTrue(doc.containsKey("address"))

col.save(document)
val doc2 = database.getCollection("nullableFriend").findOne()!!

assertTrue(doc2.containsKey("name"))
assertTrue(doc2.containsKey("address"))
}

@Category(SerializationMappingCategory::class)
@Test
fun testInsertNullFieldForKotlinxSerializationMapping() {
Expand All @@ -91,4 +125,21 @@ class PersistingNotNullTest : KMongoBaseTest<NullableFriend>() {
assertTrue(doc.containsKey("name"))
assertFalse(doc.containsKey("address"))
}

@Category(SerializationMappingCategory::class)
@Test
fun testSaveNullFieldForKotlinxSerializationMapping() {
val document = NullableFriend("Joe")
col.save(document)
val doc = database.getCollection("nullableFriend").findOne()!!

assertTrue(doc.containsKey("name"))
assertFalse(doc.containsKey("address"))

col.save(document)
val doc2 = database.getCollection("nullableFriend").findOne()!!

assertTrue(doc2.containsKey("name"))
assertFalse(doc2.containsKey("address"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@
package org.litote.kmongo.coroutine

import kotlinx.coroutines.runBlocking
import org.bson.Document
import org.junit.Test
import org.litote.kmongo.model.Friend
import org.litote.kmongo.util.ObjectMappingConfiguration
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue

/**
*
Expand All @@ -44,6 +48,22 @@ class ReactiveStreamsInsertTest : KMongoReactiveStreamsCoroutineBaseTest<Friend>
}
}

@Test
fun `can insert one in ClientSession and persists null`() = runBlocking {
mongoClient.startSession().use {
col.insertOne(it, newFriend())
assertEquals(1, col.countDocuments(it))
val doc = database.getCollection<Document>("friend").findOne()!!

assertTrue(doc.containsKey("name"))
if (ObjectMappingConfiguration.serializeNull) {
assertTrue(doc.containsKey("coordinate"))
} else {
assertFalse(doc.containsKey("coordinate"))
}
}
}

@Test
fun `can insert many`() = runBlocking {
col.insertMany(listOf(newFriend(), newFriend()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ object KMongoUtil {
json.map { toBson(it) }
}

fun filterIdToBson(obj: Any, filterNullProperties: Boolean = false): BsonDocument =
fun filterIdToBson(obj: Any, filterNullProperties: Boolean = !ObjectMappingConfiguration.serializeNull): BsonDocument =
ClassMappingType.filterIdToBson(obj, filterNullProperties)

fun formatJson(json: String): String {
Expand Down

0 comments on commit b745653

Please sign in to comment.