[CSHARP-4784] List<dynamic> does not work when Guids. Created: 10/Sep/23  Updated: 18/Sep/23

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

Type: Bug Priority: Unknown
Reporter: Steven Rothwell Assignee: Boris Dogadov
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

I have a thread in the MongoDB forum for this here: https://www.mongodb.com/community/forums/t/dynamic-variables-are-causing-issues/242663/4

To put the details here; I’m using MongoDB.Driver 2.21.0 in my .NET 7 API. I am trying to use reflection to dynamically return data.

When I use `
Builders<BsonDocument>.Filter.In(field, values)`, if `field` property is a string or int, `values` can be 

new List<dynamic> { "some", "strings", "here" }

 or 

new List<dynamic> { 1, 2, 3 }

 and it correctly returns any documents that contain those values.  But, if `field` is a Guid and `values` is 

new List<dynamic> { Guid.Parse("6cd6f392-8271-49bb-8564-e584ddf48890"), Guid.Parse("c7b1ebaf-4ac1-4fe0-b066-1282e072585a") })

, then I get this error:

MongoDB.Bson.BsonSerializationException: GuidSerializer cannot serialize a Guid when GuidRepresentation is Unspecified.

 
Even though in my Program.cs, I have the Guid representation set.

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

If `values` is

new List<Guid> { Guid.Parse("6cd6f392-8271-49bb-8564-e584ddf48890"), Guid.Parse("c7b1ebaf-4ac1-4fe0-b066-1282e072585a") });

It correctly returns any documents that have that Guid in the `field`.  The only difference is `List<dynamic>` vs `List<Guid>`.  But in my case, it needs to be dynamic as I am using input to figure out what documents to filter out.
 



 Comments   
Comment by Steven Rothwell [ 14/Sep/23 ]

I really appreciate you guys looking into this and providing a workaround!

Comment by Boris Dogadov [ 11/Sep/23 ]

Hi stevens.rothwell@gmail.com 
We have successfully reproduced this issue and will be investigating it.

Meanwhile to workaround this problem, please add this configuration to your startup code:

var discriminatorConvention = BsonSerializer.LookupDiscriminatorConvention(typeof(object));
var objectSerializer = new ObjectSerializer(discriminatorConvention, GuidRepresentation.Standard);
BsonSerializer.RegisterSerializer(objectSerializer);

Comment by PM Bot [ 10/Sep/23 ]

Hi stevens.rothwell@gmail.com, thank you for reporting this issue! The team will look into it and get back to you soon.

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