[JAVA-5035] Serializing POJO with CharSequence fails with 4.10.0 driver Created: 22/Jun/23  Updated: 28/Oct/23  Resolved: 23/Jun/23

Status: Closed
Project: Java Driver
Component/s: Codecs, POJO
Affects Version/s: 4.10.0
Fix Version/s: 4.10.1

Type: Bug Priority: Major - P3
Reporter: Radovan Radic Assignee: Ross Lawley
Resolution: Fixed Votes: 0
Labels: regression
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: Zip Archive mongo-example.zip    
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   

Summary

Inserting POJO that has CharSequence field fails as of 4.10.0 library version. The error I am seeing is this:

kotlinx.serialization.SerializationException: Class 'String' is not registered for polymorphic serialization in the scope of 'CharSequence'.
To be registered automatically, class 'String' has to be '@Serializable', and the base class 'CharSequence' has to be sealed and '@Serializable'.
Alternatively, register the serializer for 'String' explicitly in a corresponding SerializersModule. 

How to Reproduce

Please look at the attached example: mongo-example.zip. Just run Main class and the error will occur.

Additional Background

As soon as I change dependency to 4.9.1 the error goes away. Also if I exclude org.mongodb:bson-kotlinx from gradle dependency. So, we should have a way to not have Kotlin serializer as this is just java project and Kotlin serializer is throwing this error which we cannot fix.



 Comments   
Comment by Radovan Radic [ 23/Jun/23 ]

Great ross@mongodb.com , thanks for quick resolution of the issue.

Radovan

Comment by Ross Lawley [ 23/Jun/23 ]

HI radicr@gmail.com,

I wanted to thank you again both for the ticket with excellent example and the fast response / testing of this release. Your example highlighted an issue both in the kotlinx based codec provider and the pom dependencies.

Just to let you know 4.10.1 has just been released which fixes both the scope of the kotlinx codec provider and corrects the pom dependencies.

All the best,

Ross

Comment by Githook User [ 23/Jun/23 ]

Author:

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

Message: Kotlin: Improve KotlinSerializerCodec (#1152)

Only support classes annotated as serializable.

JAVA-5035
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/4ab6048bc5f5d6593ee69814092a8b59f6c28391

Comment by Githook User [ 23/Jun/23 ]

Author:

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

Message: Kotlin codecs - simplify if logic readability

Remove the negative assertion checks to improve readability.

JAVA-5035
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/28b9727ee7d38001c9cec2ce0970765750f12e33

Comment by Githook User [ 23/Jun/23 ]

Author:

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

Message: Test: Update AggregatesTest to use List of Doubles

The kotlinx codec was picking up the double array as
it was serializable. However, it should be limited
to top level data, value or sealed kotlin classes.

JAVA-5035
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/d4130cc2be0f519ed7d5590b0d4b1dd0f7f427cc

Comment by Githook User [ 23/Jun/23 ]

Author:

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

Message: Kotlin: Limit the KotlinSerializerCodec to Data, Value or Sealed Classes

JAVA-5035
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/3be0d8ad13efd1402eff55330a4b1bc7ddc365e0

Comment by PM Bot [ 22/Jun/23 ]

Hi radicr@gmail.com, 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:34 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.