Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-5099

Driver is using Kotlin native serializer over custom Codec.

    • Type: Icon: Question Question
    • Resolution: Works as Designed
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

      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.

        1. Test.kt
          2 kB
        2. test.kt
          1 kB

            Assignee:
            ross@mongodb.com Ross Lawley
            Reporter:
            jar.fmf@gmail.com Uroš Jarc
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: