-
Type: Question
-
Resolution: Works as Designed
-
Priority: Major - P3
-
None
-
Affects Version/s: None
-
Component/s: None
-
None
Summary
If having class with kotlin native serializer and also registered custom codec for the same class on mongodb client... The mongodb client will ignore custom codec and always use kotlin native serializer.
Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.0")
implementation("org.mongodb:mongodb-driver-kotlin-sync:4.10.1")
implementation("org.mongodb:bson-kotlinx:4.10.1")
How to Reproduce
@Serializable class Hashed(val hashed_bytes: ByteArray) class HashedCodec : Codec<Hashed> { override fun encode(writer: BsonWriter, value: Hashed, encoderContext: EncoderContext) { writer.writeBinaryData(BsonBinary(value.hashed_bytes)) } override fun decode(reader: BsonReader, decoderContext: DecoderContext): Hashed { return Hashed(reader) } override fun getEncoderClass(): Class<Hashed> = Hashed::class.java } val codecRegistry = CodecRegistries.fromRegistries( CodecRegistries.fromCodecs(HashedCodec(), EncryptedCodec()), MongoClientSettings.getDefaultCodecRegistry() ) val serverApi = ServerApi.builder().version(ServerApiVersion.V1).build() val settings = MongoClientSettings.builder().applyConnectionString(ConnectionString(db_url)).serverApi(serverApi).build() private val mongoClient = MongoClient.create(settings) val log = this.logger() val db = mongoClient.getDatabase(db_name).withCodecRegistry(codecRegistry)
Additional Background
Please provide any additional background information that may be helpful in diagnosing the bug.