[CSHARP-4272] Validate return value of bsonClassMap.CreateInstance() in BsonClassMapSerializer Created: 27/Jul/22  Updated: 19/Aug/22  Resolved: 19/Aug/22

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 2.17.0
Fix Version/s: 2.18.0

Type: Bug Priority: Unknown
Reporter: Matteo Spreafico Assignee: Boris Dogadov
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

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(); 

 

 

 



 Comments   
Comment by Githook User [ 19/Aug/22 ]

Author:

{'name': 'BorisDog', 'email': 'BorisDog@users.noreply.github.com', 'username': 'BorisDog'}

Message: CSHARP-4272: Validate return value of bsonClassMap.CreateInstance() in BsonClassMapSerializer (#867)

  • CSHARP-4272: Avoids null reference exceptions when BsonClassMap::CreateInstance() returns null
  • CSHARP-4272: Validate return value of bsonClassMap.CreateInstance() in BsonClassMapSerializer

Co-authored-by: Spreafico Matteo <matteo.spreafico@guess.eu>
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/c91a16661d4079bd75a94b0c9e63aa809a835dbb

Comment by Boris Dogadov [ 28/Jul/22 ]

Hi mspreafico@gmail.com thank you for your PR, it looks like a good fix.
Please follow this ticket for further updates.

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