Skip to content
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

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

Open
nacyolsa opened this issue Feb 16, 2023 · 0 comments
Labels

Comments

@nacyolsa
Copy link

nacyolsa commented Feb 16, 2023

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.

@zigzago zigzago added the bug label Apr 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants