[CSHARP-1669] Unable to cast object of type 'MongoDB.Bson.Serialization.Serializers.GuidSerializer' to type 'MongoDB.Bson.Serialization.IBsonSerializer`1[System.Nullable`1[System.Guid]]'. Created: 12/May/16  Updated: 05/Apr/19  Resolved: 17/May/16

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

Type: Task Priority: Minor - P4
Reporter: Chad Kreimendahl Assignee: Unassigned
Resolution: Won't Fix Votes: 0
Labels: question
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

When attempting to map to a guid within an Elem match, we are getting the above exception.

Example code:

            var elemFilters = new List<FilterDefinition<SPI>>
            {
                Builders<SPI>.Filter.Regex("ST", BuildRegex("^" + Regex.Escape(shortText) + "$"))
            };
            if (ignoreGuid.HasValue)
            {
                elemFilters.Add(Builders<SPI>.Filter.Ne(q => q.Guid, ignoreGuid));
            }
 
            var query = Builders<AP>.Filter.And(
                    Builders<AP>.Filter.Eq(p => p.AppId, appId),
                    Builders<AP>.Filter.ElemMatch(p => p.Items, Builders<SPI>.Filter.And(elemFilters))
                );
 
         Collection.Find(query).Any() // <--- error happens here



 Comments   
Comment by Chad Kreimendahl [ 17/May/16 ]

We've added logic around it to make it the equiv of checking for null or not, so that workaround is good enough for us.

Comment by Craig Wilson [ 16/May/16 ]

Hi Chad,

The issue is that you are using a nullable guid where the property type is an actual guid. While we could probably have the GuidSerializer implement IBsonSerializer<Nullable<Guid>> as well, we currently don't.

The "workaround" is actually quite simple. Change the line from

elemFilters.Add(Builders<SPI>.Filter.Ne(q => q.Guid, ignoreGuid));

to

elemFilters.Add(Builders<SPI>.Filter.Ne(q => q.Guid, ignoreGuid.Value));

Craig

Comment by Chad Kreimendahl [ 12/May/16 ]

Exception path:

System.InvalidCastException: Unable to cast object of type 'MongoDB.Bson.Serialization.Serializers.GuidSerializer' to type 'MongoDB.Bson.Serialization.IBsonSerializer`1[System.Nullable`1[System.Guid]]'.
at MongoDB.Driver.ExpressionFieldDefinition`2.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry)
at MongoDB.Driver.OperatorFilterDefinition`2.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry)
at MongoDB.Driver.AndFilterDefinition`1.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry)
at MongoDB.Driver.ElementMatchFilterDefinition`2.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry)
at MongoDB.Driver.AndFilterDefinition`1.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry)
at MongoDB.Driver.MongoCollectionImpl`1.CreateFindOperation[TProjection](FilterDefinition`1 filter, FindOptions`2 options)
at MongoDB.Driver.MongoCollectionImpl`1.FindSync[TProjection](FilterDefinition`1 filter, FindOptions`2 options, CancellationToken cancellationToken)
at MongoDB.Driver.FindFluent`2.ToCursor(CancellationToken cancellationToken)
at MongoDB.Driver.IAsyncCursorSourceExtensions.Any[TDocument](IAsyncCursorSource`1 source, CancellationToken cancellationToken)

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