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

Kotlinx serialization decoding optional ObjectId fails to hydrate

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 4.10.0
    • Affects Version/s: None
    • Component/s: Kotlin
    • None
    • 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?

      Handling of nullable ObjectId / Bson Value types fails if the type is the first in the data class and optional.

              @Serializable
              data class PaintOrder(
                  @SerialName("_id") // Use instead of @BsonId
                  @Contextual val id: ObjectId?,
                  val color: String,
                  val qty: Int,
                  @SerialName("brand")
                  val manufacturer: String = "Acme", // Use instead of @BsonProperty
              )
      
      
              val paintOrder = PaintOrder(ObjectId(), "red", 5, "Acme")
              val collection = database.getCollection<PaintOrder>("orders")
              val insertOneResult = collection.insertOne(paintOrder)
              println(insertOneResult) // returns with expected results (e.g. "AcknowledgedInsertOneResult{insertedId=BsonObjectId{value=648b20978b3dfb1f994a2164}}")
              val results = collection.find().firstOrNull()
              assertEquals(results, paintOrder)
      

      The following code is failing to hydrate the ObjectId even though its been decoded.

            Assignee:
            ross@mongodb.com Ross Lawley
            Reporter:
            ross@mongodb.com Ross Lawley
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: