[CSHARP-3253] ToString in BsonDocument ignores subType and always shows the GUID with CSUUID subType even though the value in UUID Created: 09/Nov/20  Updated: 27/Oct/23  Resolved: 09/Nov/20

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Dmitry Lukyanov (Inactive) Assignee: Unassigned
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

It's really visible here in debugging: https://github.com/mongodb/mongo-csharp-driver/blob/master/src/MongoDB.Driver/Encryption/ExplicitEncryptionLibMongoCryptController.cs#L59 where wrappedKeyDocument shows that _id is in CSUUID form. But if we check the _id value itself, it will be in UUID format.

Simplified test is:

            var @string = $"{{ '_id' : UUID('3fdabe71-f829-4469-9c5c-15cc4881c50c') }}";
            var bsonDocument = BsonDocument.Parse(@string);
            var stringDocument = bsonDocument.ToString();
            stringDocument.Should().Contain("CSUUID"); // pass
            stringDocument.Should().NotContain("3fdabe71-f829-4469-9c5c-15cc4881c50c"); // fail, pay attention that guid numbers are the same in both places

NOTE: bytes in both cases are the same, the only difference is subType



 Comments   
Comment by Robert Stam [ 09/Nov/20 ]

This behavior is as expected.

In `GuidRepresentationMode.V2` all Guids are converted on output to the configured representation for the JsonWriter, which by default is `CSharpLegacy` (or whatever `BsonDefaults.GuidRepresentation` is set to if it has been changed).

If you don't want the JSON representation to reflect the default `GuidRepresentation` you can configure the JSON writer to not convert all Guids on output by setting the writer's `GuidRepresentation` to `Unspecified`

var stringDocument = bsonDocument.ToJson(new JsonWriterSettings { GuidRepresentation = Unspecified });

Also, the string form in `CSUUID("...")` reflects the actual Guid value, not the byte order in the underlying byte array, so the last test should be:

stringDocument.Should().Contain("3fdabe71-f829-4469-9c5c-15cc4881c50c"); // instead of NotContain

As an alternative to setting the `JsonWriterSettings.GuidRepresentation` to `Unspecified` you could instead set `BsonDefaults.GuidRepresentationMode` to `V3` (but not that that's a global setting).

Generated at Wed Feb 07 21:44:49 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.