Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-5188

GetType with collection type throws NullReferenceException

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 3.0.0
    • Affects Version/s: None
    • Component/s: LINQ3
    • None
    • Fully Compatible
    • Dotnet Drivers
    • Not Needed
    • Hide

      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?

      Show
      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?

      Typically you would use this with a polymorphic collection, but it fails the same way when querying for the collection's type.

      using System;
      using MongoDB.Bson;
      using MongoDB.Driver;
      using MongoDB.Driver.Linq;
      
      var client = new MongoClient();
      var db = client.GetDatabase("test");
      var coll = db.GetCollection<Animal>("zoo");
      
      var query = coll.AsQueryable().Where(e => e.GetType() == typeof(Animal));
      Console.WriteLine(query);
      
      record Animal(ObjectId Id);
      

      Stack trace:

      System.NullReferenceException: Object reference not set to an instance of an object.
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.ExpressionTranslators.GetTypeComparisonExpressionToFilterTranslator.Translate(TranslationContext context, BinaryExpression expression, MethodCallExpression getTypeExpression, Expression typeConstantExpression) in /Users/james/Dropbox/code/mongodb/mongo-csharp-driver/src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToFilterTranslators/ExpressionTranslators/GetTypeComparisonExpressionToFilterTranslator.cs:line 51
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.ExpressionTranslators.ComparisonExpressionToFilterTranslator.Translate(TranslationContext context, BinaryExpression expression) in /Users/james/Dropbox/code/mongodb/mongo-csharp-driver/src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToFilterTranslators/ExpressionTranslators/ComparisonExpressionToFilterTranslator.cs:line 61
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.ExpressionToFilterTranslator.TranslateUsingQueryOperators(TranslationContext context, Expression expression) in /Users/james/Dropbox/code/mongodb/mongo-csharp-driver/src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToFilterTranslators/ExpressionToFilterTranslator.cs:line 97
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.ExpressionToFilterTranslator.Translate(TranslationContext context, Expression expression, Boolean exprOk) in /Users/james/Dropbox/code/mongodb/mongo-csharp-driver/src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToFilterTranslators/ExpressionToFilterTranslator.cs:line 37
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.ExpressionToFilterTranslator.TranslateLambda(TranslationContext context, LambdaExpression lambdaExpression, IBsonSerializer parameterSerializer, Boolean asRoot) in /Users/james/Dropbox/code/mongodb/mongo-csharp-driver/src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToFilterTranslators/ExpressionToFilterTranslator.cs:line 70
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToPipelineTranslators.WhereMethodToPipelineTranslator.Translate(TranslationContext context, MethodCallExpression expression) in /Users/james/Dropbox/code/mongodb/mongo-csharp-driver/src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToPipelineTranslators/WhereMethodToPipelineTranslator.cs:line 39
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToPipelineTranslators.ExpressionToPipelineTranslator.Translate(TranslationContext context, Expression expression) in /Users/james/Dropbox/code/mongodb/mongo-csharp-driver/src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToPipelineTranslators/ExpressionToPipelineTranslator.cs:line 73
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToExecutableQueryTranslators.ExpressionToExecutableQueryTranslator.Translate[TDocument,TOutput](MongoQueryProvider`1 provider, Expression expression) in /Users/james/Dropbox/code/mongodb/mongo-csharp-driver/src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToExecutableQueryTranslators/ExpressionToExecutableQueryTranslator.cs:line 32
         at MongoDB.Driver.Linq.Linq3Implementation.MongoQuery`2.ToString() in /Users/james/Dropbox/code/mongodb/mongo-csharp-driver/src/MongoDB.Driver/Linq/Linq3Implementation/MongoQuery.cs:line 109
      

      The root cause is in GetTypeComparisonExpressionToFilterTranslator line 49. If the nominalType matches the actualType and the hierarchy wasn't configured with a RootClass (e.g. [BsonDiscriminator(RootClass = true)]), then GetDiscriminator returns null and we throw on line 51 discrminatorValue.IsBsonArray).

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            james.kovacs@mongodb.com James Kovacs
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: