-
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.
- is related to
-
JAVA-5784 Kotlinx datetime serializer is not applied to updateOne operation
-
- Investigating
-