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

Can't use Kotlinx DateTime with Kotlin MongoDB Driver

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Trivial - P5 Trivial - P5
    • None
    • Affects Version/s: 4.11.0
    • Component/s: 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?

      So KMongo for Kotlin is deprecated because of the new official driver (which has been around since 2016)
       
      I'm trying to insert data to the database using an instance of data class that has Instant and LocalDateTime in it
       

      Unable to find source-code formatter for language: kotlin. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      @Serializable
      data class User(
          val email: String,
          val password: String,
          val isEmailVerified: Boolean,
          val isAccountActivated: Boolean,
          val role: UserRole,
          val pictureUrl: String = "",
          val emailVerification: TokenVerification,
          val forgotPasswordVerification: TokenVerification,
          val createdAt: Instant,
          val updatedAt: Instant,
          val data: UserData,
      )
      

       
      But I'm getting the following exception:
       

      org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for CodecCacheKey\{clazz=class kotlinx.datetime.Instant, types=null}.
      at org.bson.internal.ProvidersCodecRegistry.lambda$get$0(ProvidersCodecRegistry.java:87)
      at java.base/java.util.Optional.orElseGet(Optional.java:364)
      at org.bson.internal.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:80)
      at org.bson.internal.ChildCodecRegistry.get(ChildCodecRegistry.java:68)
      at org.bson.codecs.kotlin.DataClassCodec$Companion.getCodec(DataClassCodec.kt:226)
      at org.bson.codecs.kotlin.DataClassCodec$Companion.getCodec(DataClassCodec.kt:199)
      at org.bson.codecs.kotlin.DataClassCodec$Companion.create$bson_kotlin(DataClassCodec.kt:148)
      at org.bson.codecs.kotlin.DataClassCodecProvider.get(DataClassCodecProvider.kt:28)
      at org.bson.internal.ProvidersCodecRegistry.getFromCodecProvider(ProvidersCodecRegistry.java:95)
      at com.mongodb.KotlinCodecProvider.get(KotlinCodecProvider.java:83)
      at org.bson.internal.ProvidersCodecRegistry.getFromCodecProvider(ProvidersCodecRegistry.java:95)
      at org.bson.internal.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:70)
      at org.bson.internal.ProvidersCodecRegistry.getFromCodecProvider(ProvidersCodecRegistry.java:95)
      at org.bson.codecs.configuration.OverridableUuidRepresentationCodecProvider.get(OverridableUuidRepresentationCodecProvider.java:47)
      at org.bson.internal.ProvidersCodecRegistry.getFromCodecProvider(ProvidersCodecRegistry.java:95)
      at org.bson.internal.ProvidersCodecRegistry.lambda$get$0(ProvidersCodecRegistry.java:82)
      at java.base/java.util.Optional.orElseGet(Optional.java:364)
      at org.bson.internal.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:80)
      at org.bson.internal.ChildCodecRegistry.get(ChildCodecRegistry.java:68)
      at org.bson.codecs.kotlin.DataClassCodec$Companion.getCodec(DataClassCodec.kt:226)
      at org.bson.codecs.kotlin.DataClassCodec$Companion.getCodec(DataClassCodec.kt:199)
      at org.bson.codecs.kotlin.DataClassCodec$Companion.create$bson_kotlin(DataClassCodec.kt:148)
      at org.bson.codecs.kotlin.DataClassCodecProvider.get(DataClassCodecProvider.kt:28)
      at org.bson.internal.ProvidersCodecRegistry.getFromCodecProvider(ProvidersCodecRegistry.java:95)
      at com.mongodb.KotlinCodecProvider.get(KotlinCodecProvider.java:83)
      at org.bson.internal.ProvidersCodecRegistry.getFromCodecProvider(ProvidersCodecRegistry.java:95)
      at org.bson.internal.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:70)
      at org.bson.internal.ProvidersCodecRegistry.getFromCodecProvider(ProvidersCodecRegistry.java:95)
      at org.bson.codecs.configuration.OverridableUuidRepresentationCodecProvider.get(OverridableUuidRepresentationCodecProvider.java:47)
      at org.bson.internal.ProvidersCodecRegistry.getFromCodecProvider(ProvidersCodecRegistry.java:95)
      at org.bson.internal.ProvidersCodecRegistry.lambda$get$0(ProvidersCodecRegistry.java:82)
      at java.base/java.util.Optional.orElseGet(Optional.java:364)
      at org.bson.internal.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:80)
      at org.bson.internal.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:50)
      at com.mongodb.internal.operation.Operations.getCodec(Operations.java:746)
      at com.mongodb.internal.operation.Operations.bulkWrite(Operations.java:466)
      at com.mongodb.internal.operation.Operations.insertOne(Operations.java:392)
      at com.mongodb.internal.operation.AsyncOperations.insertOne(AsyncOperations.java:202)
      at com.mongodb.reactivestreams.client.internal.MongoOperationPublisher.lambda$insertOne$6(MongoOperationPublisher.java:261)
      at com.mongodb.reactivestreams.client.internal.MongoOperationPublisher.createWriteOperationMono(MongoOperationPublisher.java:446)
      at com.mongodb.reactivestreams.client.internal.MongoOperationPublisher.createSingleWriteRequestMono(MongoOperationPublisher.java:454)
      at com.mongodb.reactivestreams.client.internal.MongoOperationPublisher.insertOne(MongoOperationPublisher.java:261)
      at com.mongodb.reactivestreams.client.internal.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:367)
      at com.mongodb.kotlin.client.coroutine.MongoCollection.insertOne(MongoCollection.kt:627)
      at com.mongodb.kotlin.client.coroutine.MongoCollection.insertOne$default(MongoCollection.kt:626)
      

       
      I don't know how to create a codec for Kotlinx DateTime types and I can't find one, so can anyone tell me how to do it? thank you for your time and efforts.
       
      the official docs says everything that is Serializable can convert to BSON format in efficient way. is there any chance I'm wrong?

            Assignee:
            ross@mongodb.com Ross Lawley
            Reporter:
            ahmed4496.hnewa@gmail.com Ahmed Hnewa
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: