Skip to content

Commit 6a20fb6

Browse files
authored
Kotlin. Support stored nulls for nullable fields (#1212)
JAVA-5134
1 parent 32179fa commit 6a20fb6

File tree

2 files changed

+5
-0
lines changed

2 files changed

+5
-0
lines changed

bson-kotlin/src/main/kotlin/org/bson/codecs/kotlin/DataClassCodec.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ internal data class DataClassCodec<T : Any>(
8585
if (logger.isTraceEnabled) {
8686
logger.trace("Found property not present in the DataClass: $fieldName")
8787
}
88+
} else if (propertyModel.param.type.isMarkedNullable && reader.currentBsonType == BsonType.NULL) {
89+
reader.readNull()
8890
} else {
8991
try {
9092
args[propertyModel.param] = decoderContext.decodeWithChildContext(propertyModel.codec, reader)

bson-kotlin/src/test/kotlin/org/bson/codecs/kotlin/DataClassCodecTest.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ class DataClassCodecTest {
126126
fun testDataClassWithNulls() {
127127
val dataClass = DataClassWithNulls(null, null, null)
128128
assertRoundTrips(emptyDocument, dataClass)
129+
130+
val withStoredNulls = BsonDocument.parse("""{"boolean": null, "string": null, "listSimple": null}""")
131+
assertDecodesTo(withStoredNulls, dataClass)
129132
}
130133

131134
@Test

0 commit comments

Comments
 (0)