When the value is null and we do not have explicitNulls = true, the deferred element is not written so the deferredElementName should be cleared. The
encodeSerializableValue method does this, but
encodeNullableSerializableValue does not. This leads to the an error when trying to encode the next element.
Example
@Serializable data class Doc( val elements: List<Element> ) @Serializable data class Element( val required: String, val optional: String? = null, ) fun main() { val doc = Doc(listOf(Element(required = "some value"), Element(required = "some other value"))) println("This is the workaround - it works") KotlinSerializerCodec.create<Doc>(bsonConfiguration = BsonConfiguration(explicitNulls = true))!! .encode(BsonBinaryWriter(BasicOutputBuffer()), doc, EncoderContext.builder().build()) println("This fails") KotlinSerializerCodec.create<Doc>()!! .encode(BsonBinaryWriter(BasicOutputBuffer()), doc, EncoderContext.builder().build()) }
Output:
This is the workaround - it works
This fails
Exception in thread "main" org.bson.BsonInvalidOperationException: WriteName can only be called when State is NAME, not when State is VALUE
at org.bson.AbstractBsonWriter.throwInvalidState(AbstractBsonWriter.java:747)
at org.bson.AbstractBsonWriter.writeName(AbstractBsonWriter.java:531)
at org.bson.codecs.kotlinx.DefaultBsonEncoder.encodeName(BsonEncoder.kt:208)
at org.bson.codecs.kotlinx.DefaultBsonEncoder.encodeSerializableValue(BsonEncoder.kt:145)
at kotlinx.serialization.encoding.AbstractEncoder.encodeSerializableElement(AbstractEncoder.kt:80)
at kotlinx.serialization.internal.CollectionLikeSerializer.serialize(CollectionSerializers.kt:69)
at kotlinx.serialization.encoding.Encoder$DefaultImpls.encodeSerializableValue(Encoding.kt:279)
at kotlinx.serialization.encoding.AbstractEncoder.encodeSerializableValue(AbstractEncoder.kt:18)
at org.bson.codecs.kotlinx.DefaultBsonEncoder.encodeSerializableValue(BsonEncoder.kt:151)
at kotlinx.serialization.encoding.AbstractEncoder.encodeSerializableElement(AbstractEncoder.kt:80)