-
Type:
New Feature
-
Resolution: Unresolved
-
Priority:
Unknown
-
None
-
Affects Version/s: None
-
Component/s: Kotlin
-
None
-
None
-
Java Drivers
-
None
-
None
-
None
-
None
-
None
-
None
By default, the MongoDB Kotlin driver using kotlinx-serialization will use the "_t" field as discriminator when writing sealed interfaces and sealed classes.
This behavior can be changed by specifying a discriminator field in the BSON configuration. By changing it from "_t" to e.g. "type", every sealed interface will use the "type" field discriminator. Sometimes, you want to use different discriminator fields on different documents/classes. Let's say for one class I want the discriminator to be "type", for another class "kind" and for yet another class "isoCountryCode".
The Json format of kotlinx-serialization or the Bson format of jershell/kbson offer @JsonClassDiscriminator and @BsonClassDiscriminator. Also non-kotlinx-serialization libraries like the mongodb-bson library offer an @BsonDiscriminator annotation. The official Kotlin driver should also offer an annotation for setting the discriminator per class.