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

NullReferenceException when using certain projections

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 2.19.2
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None
    • Fully Compatible
    • 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?

      Summary

      I found a case in which using AsQueryable() throws NRE. Driver version: 2.19.1.

      How to Reproduce

      I have this model:

      class Model
      {
          public Guid NotId { get; set; }
          public string Name { get; set; }
      } 

      The query is:

      var collection = database.GetCollection<Model>("models");
      var query = await collection.AsQueryable()
          .GroupBy(x => x.Name)
          .Select(x => new Model
          {
              NotId = Guid.Empty,
              Name = x.Key
          })
          .ToListAsync(); // throws NRE

      Additional Background

      I am using V3 guid serialization:

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

      Workarounds

      I found two workarounds:

      1. Modify the projection to return anonymous type instead of Model
      2. Change the return type of Model.NotId to Guid?

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            suiden@gmail.com Dan Marinescu
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: