[CSHARP-3179] FilterDefinition don't respect GuidRepresentation Created: 11/Aug/20  Updated: 22/Jun/22

Status: Backlog
Project: C# Driver
Component/s: API
Affects Version/s: 2.11.0
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Hervé TINANT Assignee: Unassigned
Resolution: Unresolved Votes: 2
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

dotnet core 3.1 on Linux and Windows


Attachments: PNG File image-2021-03-11-14-30-27-628.png     PNG File image-2021-03-11-14-30-33-167.png    
Issue Links:
Related
related to CSHARP-3195 GuidRepresentation made obsolete whil... Backlog
Backwards Compatibility: Fully Compatible

 Description   

I replace BsonDefaults.GuidRepresentation = GuidRepresentation.Standard; by BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard)); to set all my app use Standard representation, 

 

But if I do Builders<MyEntity>.Filter.Eq("_id", myGuid);, the filter is translated with CSUUID(...) in place of UUID(...).

 

In more largest case, how I can specify Guid representation with this type of filter ? 



 Comments   
Comment by Robert Stam [ 11/Sep/20 ]

Currently V2 mode is the default in order to maintain backward compatibility. If you want to use the new V3 mode you have to opt in to it.

BsonDefaults.GuidRepresentationMode is marked as deprecated because in a future version of the driver we will remove support for V2 mode and at that point we will also remove the GuidRepresentationMode property from BsonDefaults.

Comment by Chad Kreimendahl [ 04/Sep/20 ]

Is V3 default? It seems when you set BsonDefaults that GuidRepresentationMode is flagged as deprecated, also.

Comment by Robert Stam [ 28/Aug/20 ]

While investigating further I have confirmed that even though the FilterDefinition is being rendered correctly, unless you work around it the Guid in the rendered filter will be coerced to the `BsonDefaults.GuidRepresentation` when the rendered filter is written to the wire, so there is in fact an issue.

The easiest workaround is to use the new V3 GuidRepresentationMode.

In fact, I would recommend that anyone using Guids investigate switching to the new V3 GuidRepresentationMode.

Comment by Hervé TINANT [ 14/Aug/20 ]

In fact what I see is ToJson result, but I inspect it just because some test of my repository class don't pass, so ... Anyway, I will perform some test next week with 

BsonDefaults.GuidRepresentation = GuidRepresentation.Unspecified; and come back to you.

Comment by Robert Stam [ 13/Aug/20 ]

You could also consider trying out the new V3 GuidRepresentation mode.

I also tested with:

BsonDefaults.GuidRepresentationMode = GuidRepresentationMode.V3;
BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));

Comment by Robert Stam [ 13/Aug/20 ]

I tried to reproduce this but it looks like the `Filter` builder is working correctly. Here's my test program:

namespace TestCSharp3179
{
    public class MyEntity
    {
        public Guid Id { get; set; }
    }
 
    public static class Program
    {
        public static void Main(string[] args)
        {
            BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));
 
            var myGuid = Guid.Parse("01020304-0506-0708-090a-0b0c0d0e0f10");
            var filter = Builders<MyEntity>.Filter.Eq(x => x.Id, myGuid); // or you can use "_id" instead of x => x.Id
 
            var documentSerializer = BsonSerializer.LookupSerializer<MyEntity>();
            var serializerRegistry = BsonSerializer.SerializerRegistry;
            var rendered = filter.Render(documentSerializer, serializerRegistry);
            var fieldName = rendered.GetElement(0).Name;
            var value = rendered[0].AsBsonBinaryData;
 
            Console.WriteLine($"fieldName = {fieldName}");
            Console.WriteLine($"subType = {value.SubType}, bytes = [{string.Join(", ", value.Bytes)}]");
        }
    }
}

and the output is:

fieldName = _id
subType = UuidStandard, bytes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] 

I suspect what you are seeing is the output of ToJson (which is also what you will see in the debugger because ToString calls ToJson) and ToJson continues to do what it always did in V2 GuidRepresentationMode which includes translating Guids to CSUUID format.

To get ToJson to not do any Guid translations add the following line to the beginning of your program:

BsonDefaults.GuidRepresentation = GuidRepresentation.Unspecified;

Let me know if this does not address your question or if you have follow up questions.

Comment by Hervé TINANT [ 11/Aug/20 ]

For now, I keep BsonDefaults.GuidRepresentation = GuidRepresentation.Standard; and it's work so not really urgent

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