Skip to content

JAVA-5342 Fix encoding nullable generics #1317

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 1 commit into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -139,6 +139,18 @@ internal class DefaultBsonEncoder(
return true
}

override fun <T> encodeSerializableValue(serializer: SerializationStrategy<T>, value: T) {
deferredElementName?.let {
if (value != null || configuration.explicitNulls) {
encodeName(it)
super.encodeSerializableValue(serializer, value)
} else {
deferredElementName = null
}
}
?: super.encodeSerializableValue(serializer, value)
}

override fun <T : Any> encodeNullableSerializableValue(serializer: SerializationStrategy<T>, value: T?) {
deferredElementName?.let {
if (value != null || configuration.explicitNulls) {
Expand All @@ -158,7 +170,14 @@ internal class DefaultBsonEncoder(
override fun encodeDouble(value: Double) = writer.writeDouble(value)
override fun encodeInt(value: Int) = writer.writeInt32(value)
override fun encodeLong(value: Long) = writer.writeInt64(value)
override fun encodeNull() = writer.writeNull()
override fun encodeNull() {
deferredElementName?.let {
if (configuration.explicitNulls) {
encodeName(it)
}
}
writer.writeNull()
}

override fun encodeString(value: String) {
when (state) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import org.bson.BsonUndefined
import org.bson.codecs.DecoderContext
import org.bson.codecs.EncoderContext
import org.bson.codecs.configuration.CodecConfigurationException
import org.bson.codecs.kotlinx.samples.Box
import org.bson.codecs.kotlinx.samples.DataClassBsonValues
import org.bson.codecs.kotlinx.samples.DataClassContainsOpen
import org.bson.codecs.kotlinx.samples.DataClassContainsValueClass
Expand Down Expand Up @@ -76,6 +77,7 @@ import org.bson.codecs.kotlinx.samples.DataClassWithMutableMap
import org.bson.codecs.kotlinx.samples.DataClassWithMutableSet
import org.bson.codecs.kotlinx.samples.DataClassWithNestedParameterized
import org.bson.codecs.kotlinx.samples.DataClassWithNestedParameterizedDataClass
import org.bson.codecs.kotlinx.samples.DataClassWithNullableGeneric
import org.bson.codecs.kotlinx.samples.DataClassWithNulls
import org.bson.codecs.kotlinx.samples.DataClassWithPair
import org.bson.codecs.kotlinx.samples.DataClassWithParameterizedDataClass
Expand Down Expand Up @@ -202,6 +204,27 @@ class KotlinSerializerCodecTest {
assertRoundTrips(expectedNulls, dataClass, altConfiguration)
}

@Test
fun testDataClassWithNullableGenericsNotNull() {
val expected =
"""{
| "box": {"boxed": "String"}
|}"""
.trimMargin()

val dataClass = DataClassWithNullableGeneric(Box("String"))
assertRoundTrips(expected, dataClass)
}

@Test
fun testDataClassWithNullableGenericsNull() {
val expectedDefault = """{"box": {}}"""
val dataClass = DataClassWithNullableGeneric(Box(null))
assertRoundTrips(expectedDefault, dataClass)
val expectedNull = """{"box": {"boxed": null}}"""
assertRoundTrips(expectedNull, dataClass, altConfiguration)
}

@Test
fun testDataClassSelfReferential() {
val expected =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,7 @@ data class DataClassWithFailingInit(val id: String) {
}

@Serializable data class DataClassWithSequence(val value: Sequence<String>)

@Serializable data class Box<T>(val boxed: T)

@Serializable data class DataClassWithNullableGeneric(val box: Box<String?>)