Litote / kmongo

[deprecated] KMongo - a Kotlin toolkit for Mongo

Home Page:https://litote.org/kmongo/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

org.bson.BsonInvalidOperationException: writeBinaryData can only be called when State is VALUE or INITIAL, not when State is NAME

nacyolsa opened this issue · comments

I have a function to ignore null properties from BSON Document:

fun <Collection : Any> Collection.ignoreNullProperties() = ClassMappingType.filterIdToBson(this, true)

It worked fine but recently I introduced something like below to generate serializers

@OptIn(ExperimentalSerializationApi::class)
@Serializer(forClass = PetDbo::class)
object PetDboSerializer : KSerializer<PetDbo> {

    override fun deserialize(decoder: Decoder): PetDbo =
        decoder.deserialize(schemaDeserializer = PetDboSchemaDeserializer)

}

@OptIn(ExperimentalSerializationApi::class)
@Serializer(forClass = PetDbo::class)
object PetDboGeneratedSerializer : KSerializer<PetDbo>

After that I started getting the following error

org.bson.BsonInvalidOperationException: writeBinaryData can only be called when State is VALUE or INITIAL, not when State is NAME
	at org.bson.AbstractBsonWriter.throwInvalidState(AbstractBsonWriter.java:746)
	at org.bson.AbstractBsonWriter.checkPreconditions(AbstractBsonWriter.java:701)
	at org.bson.AbstractBsonWriter.writeBinaryData(AbstractBsonWriter.java:367)
	at com.github.jershell.kbson.BsonEncoder.encodeUUID(BsonEncoder.kt:223)
	at com.github.jershell.kbson.UUIDSerializer.serialize(Serializers.kt:136)
	at com.github.jershell.kbson.UUIDSerializer.serialize(Serializers.kt:131)
	at kotlinx.serialization.encoding.Encoder$DefaultImpls.encodeSerializableValue(Encoding.kt:282)
	at kotlinx.serialization.encoding.AbstractEncoder.encodeSerializableValue(AbstractEncoder.kt:18)
	at kotlinx.serialization.encoding.AbstractEncoder.encodeSerializableElement(AbstractEncoder.kt:80)
	at com.project.commons.mongodb.example.repository.model.PetDboSerializer.serialize(PetDboSerializer.kt:36)
	at com.project.commons.mongodb.example.repository.model.PetDboSerializer.serialize(PetDboSerializer.kt:36)
	at kotlinx.serialization.encoding.Encoder$DefaultImpls.encodeSerializableValue(Encoding.kt:282)
	at kotlinx.serialization.encoding.AbstractEncoder.encodeSerializableValue(AbstractEncoder.kt:18)
	at org.litote.kmongo.serialization.SerializationCodec.encode(SerializationCodec.kt:62)
	at org.litote.kmongo.serialization.SerializationClassMappingTypeService.filterIdToBson(SerializationClassMappingTypeService.kt:56)
	at org.litote.kmongo.service.ClassMappingType.filterIdToBson(ClassMappingType.kt)

I use kmongo 4.8.0, kotlinx-serialization 1.5.0 but I tried also newer/older versions. The issue also occurs.