[JAVA-5122] Cannot serialize Array property Created: 28/Aug/23  Updated: 31/Jan/24

Status: Backlog
Project: Java Driver
Component/s: Codecs, Kotlin
Affects Version/s: 4.10.0
Fix Version/s: None

Type: Improvement Priority: Unknown
Reporter: Andreas Tollkötter Assignee: Ashni Mehta
Resolution: Unresolved Votes: 0
Labels: kotlin-quick-wins
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Quarter: FY24Q4
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   

When trying to serialize a class with an array property, e.g.

```kotlin
data class Foo(val myStrings: Array<String>)
```

the BSON serializer fails. This is due to this check in ChildCodecRegistry: https://github.com/mongodb/mongo-java-driver/blob/2fa9f4928c7badc2b022dfc007ce692e82dfe5cd/bson/src/main/org/bson/internal/ChildCodecRegistry.java#L75

`Class<Array<T>>.getTypeArguments` does not actually return 1 but the same as `Class<T>.getTypeArguments`.

The simplest fix can be to use `Class.isArray` to hardcode a fallback to one type parameter in that case.

This happens on the most recent version, 4.10.2.

This bug is actually somewhat important because Kotlin Multiplatform does not support `List`, the usually more natural choice, and requires you to use `Array` instead.



 Comments   
Comment by Tom Selander [ 18/Sep/23 ]

a.tollkoetter@gmx.de Thanks for your feedback, we'll look at prioritizing this ticket in an upcoming release.

Comment by Andreas Tollkötter [ 05/Sep/23 ]

Hi ross@mongodb.com. Thanks for your comment.

So this is going to be a "Won't fix", if I understand correctly? That is unfortunate. For me as an outsider, it seems like this is something you could add support for without breaking change, even if it hasn't been for several years.

Either way, thanks for the pointer to bson-kotlinx. I'll check that out and it sounds like it'll solve the issue for me.

Regarding KMM, I use it to share business logic between my frontend and backend, so my domain classes are restricted to use Array instead of List. MongoDB is only addressed from the JVM, so I don't expect to run into problems there.

Comment by Ross Lawley [ 05/Sep/23 ]

Hi a.tollkoetter@gmx.de,

After investigation I can confirm the issue is because there is no native array support for the Bson library which the bson-kotlin library uses. The bson library has been this way for over 10 years and as such its considered as "works as designed". However, there is the bson-kotlinx library that uses kotlinx serialization and because of that it will support arrays, so that could be an alternative.

Secondly, when you mentioned Kotlin Multiplatform (KMM) - how are you planning to use KMM? I ask because the underlying kotlin driver will not work on KMM due to some of the Java APIs it uses are also unavailable on KMM.

Ross

Comment by PM Bot [ 28/Aug/23 ]

Hi a.tollkoetter@gmx.de, thank you for reporting this issue! The team will look into it and get back to you soon.

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