Improve Handling of Contextual Serializers in Updates & Filters

XMLWordPrintableJSON

    • Type: Improvement
    • Resolution: Unresolved
    • Priority: 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
            Reporter:
            Slav Babanin
            None
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: