[JAVA-4140] BasicDBObject/Document toString methods throw exceptions when one of the fields is a UUID Created: 05/May/21  Updated: 28/Oct/23  Resolved: 15/Jun/21

Status: Closed
Project: Java Driver
Component/s: BSON
Affects Version/s: 4.0.0
Fix Version/s: 4.3.0

Type: Bug Priority: Major - P3
Reporter: Yannick Gladow Assignee: Jeffrey Yemin
Resolution: Fixed Votes: 0
Labels: external-user
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to JAVA-4051 BasicDBObject.equals throws exception... Closed
Backwards Compatibility: Minor Change
Documentation Changes: Not Needed

 Description   

Consider this code:

      var uuid = UUID.randomUUID()
      var dbo1 = new BasicDBObject("_id", uuid);
 
      dbo1.toString()

In the 3.x driver this will print the dbo, but in 4.x it will throw:

Exception in thread "main" org.bson.codecs.configuration.CodecConfigurationException: 
The uuidRepresentation has not been specified, so the UUID cannot be encoded.
	at org.bson.codecs.UuidCodec.encode(UuidCodec.java:72)
	at org.bson.codecs.UuidCodec.encode(UuidCodec.java:37)
	at org.bson.codecs.EncoderContext.encodeWithChildContext(EncoderContext.java:91)
	at com.mongodb.DBObjectCodec.writeValue(DBObjectCodec.java:246)
	at com.mongodb.DBObjectCodec.encode(DBObjectCodec.java:159)
	at com.mongodb.DBObjectCodec.encode(DBObjectCodec.java:67)
	at com.mongodb.BasicDBObject.toBson(BasicDBObject.java:225)
	at com.mongodb.BasicDBObject.equals(BasicDBObject.java:21

This is a breaking change. There are workarounds, first rendering toJson with a specific encoder or creating the bson with a UUID representation, but still tricky.

Would be great if toString and toJson could work right out of the box.



 Comments   
Comment by Githook User [ 15/Jun/21 ]

Author:

{'name': 'Jeff Yemin', 'email': 'jeff.yemin@mongodb.com', 'username': 'jyemin'}

Message: Use standard UUID representation for JSON generation (#730)

Change the behavior of the following methods:

  • Document.toJson
  • BasicDBObject.toJson
  • BasicDBObject.toString

to use standard UUID representation when generating JSON. Prior to this change,
an exception would be thrown when the document contains a UUID value.

JAVA-4140
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/37d4a45935281934b28335343de50c317a87c1e2

Comment by Jeffrey Yemin [ 14/Jun/21 ]

FYI, my plan is to render UUIDs as BSON binary, type 4 (Standard representation). We have to choose a representation, since we don't have access to MongoClientSettings to tell us what the application expects. By using type 4, at least it's unambiguous what the UUID actually is, even if applications may end up storing the UUID in MongoDB with a different UUID representation.

Comment by Jeffrey Yemin [ 10/May/21 ]

We plan to address this in the 4.3.0 release, coming in the next few months.

Comment by Yannick Gladow [ 10/May/21 ]

Hey, we are using `toString` in production in some log statements. Unfortunately I can not even grep for all the places where it is used.

Comment by Jeffrey Yemin [ 10/May/21 ]

yannick.gladow@commercetools.com, are you using the toString method in production, or is it only test code?

Comment by Yannick Gladow [ 10/May/21 ]

Okay great, this is hopefully the last major blocker for us updating to 4.x.x. Thanks!

Comment by Esha Bhargava [ 07/May/21 ]

yannick.gladow@commercetools.com Thanks for reporting this issue! We'll look into it soon and get back to you.

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