[CSHARP-4888] System.ArgumentException: Value type of serializer is "YourEnumType" and does not match member type System.Int32 Created: 19/Dec/23  Updated: 19/Jan/24  Resolved: 19/Jan/24

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

Type: Bug Priority: Minor - P4
Reporter: Roberto Pérez Assignee: Robert Stam
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Documentation Changes: Not Needed
Documentation Changes Summary:

1. What would you like to communicate to the user about this feature?
2. Would you like the user to see examples of the syntax and/or executable code and its output?
3. Which versions of the driver/connector does this apply to?


 Description   

Summary

The latest MongoDB.Driver for C# switched the default LINQ Provider from 2 to 3. After trying to upgrade and use version 3 the code that used to work in V2 fails in V3.

Server versions: 5.0.13 Windows and 5.0.10 Ubuntu

How to Reproduce

    internal class Program7
    internal class Program7
    {
        public class Car
        {
            public string LicensePlate { get; set; }
            public CarType CarType { get; set; }
        }
 
        public enum CarType
        {
            Undefined = 0,
            Sport = 1,
            Suv = 2
        }
 
        internal static void Main2(string[] args)
        {
            foreach (var linqProvider in new []
                     {
                         LinqProvider.V2, 
                         LinqProvider.V3
                     })
            {
                var builder = new MongoUrlBuilder
                {
                    DatabaseName = "Sample",
                    Server = new MongoServerAddress("localhost", 27017)
                };
                var mongoClientSettings = MongoClientSettings.FromUrl(builder.ToMongoUrl());
                mongoClientSettings.LinqProvider = linqProvider;
                var mongoClient = new MongoClient(mongoClientSettings);
                var mongoDatabase = mongoClient.GetDatabase(builder.DatabaseName);
 
                var carsCollections = mongoDatabase.GetCollection<Car>("Cars");
                carsCollections.DeleteMany(c => true);
                carsCollections.InsertOne(new Car
                {
                    LicensePlate = "CAL-" + Guid.NewGuid().ToString().Substring(6),
                    CarType = CarType.Sport
                });
 
                var result = carsCollections.AsQueryable()
                    .GroupBy(c => 0)
                    .Select(g => new
                    {
                        SportCarCount = g.Sum(c => c.CarType == CarType.Sport ? 1 : 0),
                        SuvCarCount = g.Sum(c => c.CarType == CarType.Suv ? 1 : 0)
                    })
                    .FirstOrDefault();
 
                Console.WriteLine($"{linqProvider} - SportCarCount: {result.SportCarCount}");
                Console.WriteLine($"{linqProvider} - SuvCarCount: {result.SuvCarCount}");
 
                Console.WriteLine();
                Console.WriteLine();
            }
        }
    }

Execution output:

V2 - SportCarCount: 1
V2 - SuvCarCount: 0

 

Unhandled Exception: System.ArgumentException: Value type of serializer is Sample.Program7+CarType and does not match member type System.Int32.
Parameter name: serializer
   at MongoDB.Bson.Serialization.BsonMemberMap.SetSerializer(IBsonSerializer serializer)
   at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.MemberInitExpressionToAggregationExpressionTranslator.Translate(TranslationContext context, Expression expression, NewExpression newExpression, IReadOnlyList`1 bindings)
   at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.NewExpressionToAggregationExpressionTranslator.Translate(TranslationContext context, NewExpression expression)
   at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.ExpressionToAggregationExpressionTranslator.Translate(TranslationContext context, Expression expression)
   at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.ExpressionToAggregationExpressionTranslator.TranslateLambdaBody(TranslationContext context, LambdaExpression lambdaExpression, IBsonSerializer parameterSerializer, Boolean asRoot)
   at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToPipelineTranslators.SelectMethodToPipelineTranslator.Translate(TranslationContext context, MethodCallExpression expression)
   at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToPipelineTranslators.ExpressionToPipelineTranslator.Translate(TranslationContext context, Expression expression)
   at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToExecutableQueryTranslators.FirstMethodToExecutableQueryTranslator`1.Translate[TDocument](MongoQueryProvider`1 provider, TranslationContext context, MethodCallExpression expression)
   at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToExecutableQueryTranslators.ExpressionToExecutableQueryTranslator.TranslateScalar[TDocument,TResult](MongoQueryProvider`1 provider, Expression expression)
   at MongoDB.Driver.Linq.Linq3Implementation.MongoQueryProvider`1.Execute[TResult](Expression expression)
   at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source)
   at Sample.Program7.Main2(String[] args) in Program7.cs:line 49
   at Sample.Program.Main(String[] args) in Program.cs:line 29

 

This is the generated pipeline in the V2 version:

[{
        "$group": {
            "_id": 0,
            "__agg0": {
                "$sum": {
                    "$cond": [{
                            "$eq": [
                                "$CarType",
                                1
                            ]
                        },
                        1,
                        0
                    ]
                }
            },
            "__agg1": {
                "$sum": {
                    "$cond": [{
                            "$eq": [
                                "$CarType",
                                2
                            ]
                        },
                        1,
                        0
                    ]
                }
            }
        }
    }, {
        "$project": {
            "SportCarCount": "$__agg0",
            "SuvCarCount": "$__agg1",
            "_id": 0
        }
    }, {
        "$limit": 1
    }
]



 Comments   
Comment by Robert Stam [ 19/Dec/23 ]

Thank you for reporting this issue. Using the code you have provided I am able to reproduce the issue. Thanks for providing a good standalone repro. I am investigating and hope to provide a fix soon.

Comment by PM Bot [ 19/Dec/23 ]

Hi suikevil, 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:41 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.