@SerialName annotation does not work with enums using kotlin-extensions

XMLWordPrintableJSON

    • Type: New Feature
    • Resolution: Fixed
    • Priority: Unknown
    • 5.6.0
    • Affects Version/s: 5.5.1
    • Component/s: Codecs, Kotlin
    • 🔵 Done
    • Java Drivers
    • Not Needed
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?
    • None
    • None
    • None
    • None
    • None
    • None

      Summary

      The `Filters` coming with `mongodb-driver-kotlin-extensions` work in conjunction with `kotlinx-serialization`'s `@SerialName` annotation for regular data classes. They don't work for enum classes though. 

      Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).

      org.mongodb:mongodb-driver-kotlin-extensions:5.5.1

      How to Reproduce

      package demo
      
      import com.mongodb.kotlin.client.coroutine.MongoClient
      import com.mongodb.kotlin.client.model.Filters.eq
      import kotlinx.coroutines.runBlocking
      import kotlinx.serialization.SerialName
      import kotlinx.serialization.Serializable
      
      @Serializable
      data class Present(val color: PresentColor, val content: String?)
      
      @Serializable
      enum class PresentColor {
          @SerialName("blue")
          Blue,
      
          @SerialName("red")
          Red,
      
          @SerialName("yellow")
          Yellow,
      }
      
      fun main(): Unit = runBlocking {
          val database = MongoClient.create().getDatabase("test")
          val collection = database.getCollection<Present>("presents")
      
          collection.drop()
      
          collection.insertOne(Present(color = PresentColor.Red, content = "toys"))
          collection.insertOne(Present(color = PresentColor.Yellow, content = null))
          collection.insertOne(Present(color = PresentColor.Blue, content = "chocolate"))
      
          collection.find(Present::content eq "toys").collect { println(it) } // ✅ prints Present(color=Red, content=toys)
          collection.find(Present::color eq PresentColor.Red).collect { println(it) } // ❌ does not print anything
      } 

      Additional Background

       

            Assignee:
            Ross Lawley
            Reporter:
            Richard Schielek
            Maxim Katcharov
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: