Litote / kmongo

[deprecated] KMongo - a Kotlin toolkit for Mongo
https://litote.org/kmongo/
Apache License 2.0
781 stars 75 forks source link

replaceOneById doesn't use a custom CodecRegistry #389

Open piglovesyou opened 1 year ago

piglovesyou commented 1 year ago

replaceOneById -> replaceOneWithoutId -> KMongoUtil.filterIdToBson -> SerializationClassMappingTypeService::filterIdToBson

I think in this line , it should access a custom codec registry that a user registered by MongoCollection::withCodecRegistry(registry). Because of this, it fails to find a codec and fails serialization.

zigzago commented 1 year ago

Thank you for reporting. Could you provide a reproducible use case ? I infer it does not work for for a collection class that use a custom codec ? For which mapping engine ?

piglovesyou commented 1 year ago

Thanks. Yes, when a collection with a custom codec registry calls .replaceOne() (or any method depending on KMongoUtil.filterIdToBson()), it ignores the registered codec registry; it uses a KMongo internal shared codec registry in SerializationClassMappingTypeService instead. Not sure about mapping engines, our project uses Kotlinx Serialization for serialization, but I don't think it affects the root cause.

zigzago commented 1 year ago

Workaround: add the codec in MongoClientSettings.codecRegistry inKMongo.createClient(MongoClientSettings). Needs a major refactoring to fix the bug because the KMongoUtil.filterIdToBson() is stateless