Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-5478

Serialization of sealed classes lacks type field

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 5.0.1
    • Component/s: Kotlin
    • None
    • Java Drivers
    • 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?

      Serialization of sealed classes lacks type fieldĀ _t.

      This code throws SerializationException on the last line:

      class MyTest {
      
          @Serializable
          sealed class MyClass {
             abstract val myInt: Int
             abstract val myString: String
          }
      
          @Serializable
          data class MyClass1(override val myInt: Int, override val myString: String, val myLong: Long) : MyClass()
      
          @Serializable
          data class MyClass2(override val myInt: Int, override val myString: String, val myInstant: Instant) : MyClass()
      
          @Test
          fun storeAndRetrieveSealedClasses() = runTest {
             val client = MongoClient.create("mongodb://localhost")
             val mongoDatabase = client.getDatabase("MyTest")
      
             val document1 : MyClass = MyClass1(0, "a", 3)
             val document2 : MyClass = MyClass2(0, "b", Instant.parse("2023-01-01T12:13:14Z"))
      
             // Sanity test
             assertThat(Json.decodeFromString<MyClass>(Json.encodeToString(document1))).isEqualTo(document1)
             assertThat(Json.decodeFromString<MyClass>(Json.encodeToString(document2))).isEqualTo(document2)
      
             val c = mongoDatabase.getCollection<MyClass>("Test")
      
             c.insertOne(document1)
             c.insertOne(document2)
      
             val result = c.find().toList() // This line throws
          }
      } 

      Thrown exception:

      kotlinx.serialization.SerializationException: Missing required discriminator field `_t` for polymorphic class: `MyTest.MyClass`. 

      Using versions:

      • org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3
      • org.mongodb:bson-kotlinx:5.1.0
      • org.mongodb:mongodb-driver-kotlin-coroutine:5.0.1

      As you can see in the beginning of the test, serialization to JSON correctly includes the discriminator field and is able to deserialize.

            Assignee:
            ross@mongodb.com Ross Lawley
            Reporter:
            jfontsaballs@gmail.com Jordi Font Saballs
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: