Skip to content

Fix bson-kotlinx encodeNullableSerializableValue null handling #1453

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

Merged
merged 4 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import org.bson.codecs.configuration.CodecConfigurationException
import org.bson.codecs.configuration.CodecRegistries.fromProviders
import org.bson.codecs.kotlin.samples.Box
import org.bson.codecs.kotlin.samples.DataClassEmbedded
import org.bson.codecs.kotlin.samples.DataClassLastItemDefaultsToNull
import org.bson.codecs.kotlin.samples.DataClassListOfDataClasses
import org.bson.codecs.kotlin.samples.DataClassListOfListOfDataClasses
import org.bson.codecs.kotlin.samples.DataClassListOfSealed
Expand All @@ -51,6 +52,7 @@ import org.bson.codecs.kotlin.samples.DataClassWithEnum
import org.bson.codecs.kotlin.samples.DataClassWithEnumMapKey
import org.bson.codecs.kotlin.samples.DataClassWithFailingInit
import org.bson.codecs.kotlin.samples.DataClassWithInvalidBsonRepresentation
import org.bson.codecs.kotlin.samples.DataClassWithListThatLastItemDefaultsToNull
import org.bson.codecs.kotlin.samples.DataClassWithMutableList
import org.bson.codecs.kotlin.samples.DataClassWithMutableMap
import org.bson.codecs.kotlin.samples.DataClassWithMutableSet
Expand Down Expand Up @@ -133,6 +135,20 @@ class DataClassCodecTest {
assertDecodesTo(withStoredNulls, dataClass)
}

@Test
fun testDataClassWithListThatLastItemDefaultsToNull() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a copy from bson-kotlinx - to ensure the bson-kotlin library also works as expected.

val expected =
"""{
| "elements": [{"required": "required"}, {"required": "required"}],
|}"""
.trimMargin()

val dataClass =
DataClassWithListThatLastItemDefaultsToNull(
listOf(DataClassLastItemDefaultsToNull("required"), DataClassLastItemDefaultsToNull("required")))
assertRoundTrips(expected, dataClass)
}

@Test
fun testDataClassWithNullableGenericsNotNull() {
val expected =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ data class DataClassWithDefaults(

data class DataClassWithNulls(val boolean: Boolean?, val string: String?, val listSimple: List<String?>?)

data class DataClassWithListThatLastItemDefaultsToNull(val elements: List<DataClassLastItemDefaultsToNull>)

data class DataClassLastItemDefaultsToNull(val required: String, val optional: String? = null)

data class DataClassSelfReferential(
val name: String,
val left: DataClassSelfReferential? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ internal class DefaultBsonEncoder(
if (value != null || configuration.explicitNulls) {
encodeName(it)
super.encodeNullableSerializableValue(serializer, value)
} else {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems worth it to add tests for the corresponding else clause in the encodeSerializableValue method just above (I removed the clause and no tests fails, so it's not covered).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't concoct a test that triggers this. That said it doesn't guarantee that there isn't a scenario where it could be triggered.

As we are skipping this value it makes sense to reset the defferred name if it exists.
However, I think I can clean up the usage.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jyemin I cleaned up the usage and encapsulated the handling of deferred element names.

deferredElementName = null
}
}
?: super.encodeNullableSerializableValue(serializer, value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import org.bson.codecs.kotlinx.samples.DataClassContainsOpen
import org.bson.codecs.kotlinx.samples.DataClassContainsValueClass
import org.bson.codecs.kotlinx.samples.DataClassEmbedded
import org.bson.codecs.kotlinx.samples.DataClassKey
import org.bson.codecs.kotlinx.samples.DataClassLastItemDefaultsToNull
import org.bson.codecs.kotlinx.samples.DataClassListOfDataClasses
import org.bson.codecs.kotlinx.samples.DataClassListOfListOfDataClasses
import org.bson.codecs.kotlinx.samples.DataClassListOfSealed
Expand Down Expand Up @@ -78,6 +79,7 @@ import org.bson.codecs.kotlinx.samples.DataClassWithEncodeDefault
import org.bson.codecs.kotlinx.samples.DataClassWithEnum
import org.bson.codecs.kotlinx.samples.DataClassWithEnumMapKey
import org.bson.codecs.kotlinx.samples.DataClassWithFailingInit
import org.bson.codecs.kotlinx.samples.DataClassWithListThatLastItemDefaultsToNull
import org.bson.codecs.kotlinx.samples.DataClassWithMutableList
import org.bson.codecs.kotlinx.samples.DataClassWithMutableMap
import org.bson.codecs.kotlinx.samples.DataClassWithMutableSet
Expand Down Expand Up @@ -255,6 +257,27 @@ class KotlinSerializerCodecTest {
assertRoundTrips(expectedNulls, dataClass, altConfiguration)
}

@Test
fun testDataClassWithListThatLastItemDefaultsToNull() {
val expectedWithOutNulls =
"""{
| "elements": [{"required": "required"}, {"required": "required"}],
|}"""
.trimMargin()

val dataClass =
DataClassWithListThatLastItemDefaultsToNull(
listOf(DataClassLastItemDefaultsToNull("required"), DataClassLastItemDefaultsToNull("required")))
assertRoundTrips(expectedWithOutNulls, dataClass)

val expectedWithNulls =
"""{
| "elements": [{"required": "required", "optional": null}, {"required": "required", "optional": null}],
|}"""
.trimMargin()
assertRoundTrips(expectedWithNulls, dataClass, BsonConfiguration(explicitNulls = true))
}

@Test
fun testDataClassWithNullableGenericsNotNull() {
val expected =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ data class DataClassWithDefaults(

@Serializable data class DataClassWithNulls(val boolean: Boolean?, val string: String?, val listSimple: List<String?>?)

@Serializable
data class DataClassWithListThatLastItemDefaultsToNull(val elements: List<DataClassLastItemDefaultsToNull>)

@Serializable data class DataClassLastItemDefaultsToNull(val required: String, val optional: String? = null)

@Serializable
data class DataClassSelfReferential(
val name: String,
Expand Down