[JAVA-5296] Can't use Kotlinx DateTime with Kotlin MongoDB Driver Created: 20/Jan/24 Updated: 25/Jan/24 Resolved: 25/Jan/24 |
|
| Status: | Closed |
| Project: | Java Driver |
| Component/s: | None |
| Affects Version/s: | 4.11.0 |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Trivial - P5 |
| Reporter: | Ahmed Hnewa | Assignee: | Ross Lawley |
| Resolution: | Works as Designed | Votes: | 0 |
| Labels: | bug, kotlin, question | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||
| Documentation Changes Summary: | 1. What would you like to communicate to the user about this feature? |
||||
| Description |
|
So KMongo for Kotlin is deprecated because of the new official driver (which has been around since 2016)
|
| Comments |
| Comment by Ross Lawley [ 25/Jan/24 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Closing as "works as designed" - I have opened a documentation ticket to improve the documentation for this. Many Thanks, Ross | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Ross Lawley [ 25/Jan/24 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
This is because the Instant class uses the InstantIso8601Serializer by default. So to use an alternative it must be set by the data class. Ross | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Ahmed Hnewa [ 25/Jan/24 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
It's like you said, but it would be helpful to add another library that support the kotlinx date time | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Ahmed Hnewa [ 25/Jan/24 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
The problem is that kotlinx bson is not storing Instant as Date instead it store it as a String | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Ahmed Hnewa [ 25/Jan/24 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
While the error is now gone but I have another error: @Serializable Caused by: org.bson.BsonInvalidOperationException: Reading field 'expiresAt' failed expected STRING type but found: DATE_TIME. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Ross Lawley [ 25/Jan/24 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
Thats correct, the driver will detect if its available and automatically register it. Ross | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Ahmed Hnewa [ 24/Jan/24 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Hi, thank you for the response!! By using bson kotlinx, we only have to add the dependency? The docs didn't cover how to register it, it only covers how to customize it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Ross Lawley [ 24/Jan/24 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Thanks for the ticket. To support kotlinx.serialization you will need to add the bson-kotlinx dependency to your project. See the Serialization section of the documentation. Alternatively, you will need to add a custom codec to the registry to handle kotlinx.datetime.Instant. The example code I ran (using bson-koltinx) was:
Produces:
Notice Kotlinx serialization defaults the serialization to a String and not to a BsonDateTime! To convert an Instant to a BsonDateTime you will need to add your own KSerializer like so:
Notice we can use the BsonEncoder to handle the conversion of BsonDateTime values. Update the dataclass to use the custom serializer:
And then the output is:
You can see from the extended Json that createdAt is now a Bson DateTime and not a String. I hope that helps, Ross | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Ahmed Hnewa [ 21/Jan/24 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
I found a workaround this and published it on https://stackoverflow.com/a/77852080/18519412, but still we should be able to use the classes that is Serializable in bson format as the docs said that | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by PM Bot [ 20/Jan/24 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Hi ahmed4496.hnewa@gmail.com, thank you for reporting this issue! The team will look into it and get back to you soon. |