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

Validate return value of bsonClassMap.CreateInstance() in BsonClassMapSerializer

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Unknown Unknown
    • 2.18.0
    • Affects Version/s: 2.17.0
    • Component/s: None
    • None

      Summary

      BsonClassMapSerializer throws a NullReferenceException when BsonClassMap::CreateInstance() returns null, which may happen if one has configured a creator on the map and that creator fails silently returning nulls instead of object instances (think of DI containers)

      How to Reproduce

      Simulate a failing creator:

      BsonClassMap .LookupClassMap(typeof(MyModel)) .SetCreator(() => {
      //could be something like:
              //ServiceProvider.GetService(domainObjType)
      
              //that may...
              return null;
      }); 

      Execute a Find() on that type:

      var filter = SetupTheFilterDefinition();
      var results = collection.Find<MyModel>(filter).ToList(); 

       

       

       

            Assignee:
            boris.dogadov@mongodb.com Boris Dogadov
            Reporter:
            mspreafico@gmail.com Matteo Spreafico
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: