[JAVA-5031] Kotlinx serialization decoding optional ObjectId fails to hydrate Created: 16/Jun/23  Updated: 28/Oct/23  Resolved: 20/Jun/23

Status: Closed
Project: Java Driver
Component/s: Kotlin
Affects Version/s: None
Fix Version/s: 4.10.0

Type: Bug Priority: Unknown
Reporter: Ross Lawley Assignee: Ross Lawley
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Documentation Changes: Not Needed
Documentation Changes Summary:

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?


 Description   

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.



 Comments   
Comment by Githook User [ 20/Jun/23 ]

Author:

{'name': 'Ross Lawley', 'email': 'ross@mongodb.com', 'username': 'rozza'}

Message: Kotlinx serialization decoding optional ObjectId / BsonValues fails to hydrate properly

Fixes element decoding logic and ensuring all element indexes are accounted for.

JAVA-5031
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/c5370c32ad4f496586c767f3c4eb63beab3612b2

Generated at Thu Feb 08 09:03:34 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.