1 / 1
Apr 8

Hello, we are using the Mongo kotlin library to serialize a hierarchy of objects. These are sealed classes and we want to define a specific discriminator for these objects.

@OptIn(ExperimentalSerializationApi::class) @JsonClassDiscriminator("type") @Serializable sealed class TestInterface( @Contextual val _id: ObjectId ) @Serializable @SerialName("TestClass1") class TestClass1 : TestInterface(ObjectId()) @Serializable @SerialName("TestClass2") class TestClass2 : TestInterface(ObjectId())

When inserting instances of these objects in a collection, a discriminator field _t is written instead of type.
According to the library code, the BsonEncoder class does not seem to support the kotlinx annotation and only looks at the global configuration:

is PolymorphicKind -> { writer.writeStartDocument() writer.writeName(configuration.classDiscriminator) isPolymorphic = true }

Is this analysis correct ? is there a way to define a specific discriminator for our objects ? possibly without using the kotlinx serialization ?

Thanks