Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-4140

BasicDBObject/Document toString methods throw exceptions when one of the fields is a UUID

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 4.3.0
    • Affects Version/s: 4.0.0
    • Component/s: BSON
    • Labels:
    • Minor Change
    • Not Needed

      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.

            Assignee:
            jeff.yemin@mongodb.com Jeffrey Yemin
            Reporter:
            yannick.gladow@commercetools.com Yannick Gladow
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: