kmongo icon indicating copy to clipboard operation
kmongo copied to clipboard

replaceOneById doesn't use a custom CodecRegistry

Open piglovesyou opened this issue 2 years ago • 3 comments

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.

piglovesyou avatar Dec 24 '22 17:12 piglovesyou

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 ?

zigzago avatar Jan 10 '23 06:01 zigzago

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.

piglovesyou avatar Jan 10 '23 12:01 piglovesyou

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

zigzago avatar Apr 11 '23 13:04 zigzago