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

Improve Handling of Contextual Serializers in Updates & Filters

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: Kotlin
    • None
    • Java Drivers
    • None
    • None
    • None
    • None
    • None
    • None

      Currently, contextual serializers in Kotlinx Serialization work as expected when applied directly to fields in a Kotlin data class. However, when using the Updates/Filters API, the field is used directly (without being processed through the data class), which means the default serializer is used instead of the custom one.

      This occurs because kClass.serializer() always returns the default, compile-time generated serializer, even when a custom serializer is applied in the model definition.

      Example:

      @Serializable
      data class Foo(
          @SerialName("_id")
          @Contextual val id: ObjectId = ObjectId(),
          
          @Serializable(with = InstantAsBsonDateTime::class)
          val instant: Instant
      )
      
      val anotherNewInstant = Clock.System.now()
      collection.updateOne(Foo::id eq foo.id, Foo::instant set anotherNewInstant) // anotherNewInstant uses default serializer 

      We should come up with a convenient solution to ensure custom serializers are applied in update and filter operations.

      Potential approaches:

      • Introduce an intermediate object wrapper for fields so that serializers are respected.
      • Provide an API that allows explicitly specifying a serializer in update/filter operations.

       

            Assignee:
            Unassigned Unassigned
            Reporter:
            slav.babanin@mongodb.com Slav Babanin
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              None
              None
              None
              None