Details
-
Bug
-
Resolution: Gone away
-
Major - P3
-
None
-
None
-
None
-
None
Description
Summary
As I reviewed the source code to fill some gaps in the documentation, it appeared to me that a null check may be missing on this line to turn the code into the below (see the null check in the if statement)
// process any left over values that weren't passed to the creator
|
foreach (var keyValuePair in values) |
{
|
var elementName = keyValuePair.Key; |
var value = keyValuePair.Value; |
|
|
var memberMap = _classMap.GetMemberMapForElement(elementName); |
if (memberMap != null && !memberMap.IsReadOnly) |
{
|
memberMap.Setter.Invoke(document, value);
|
}
|
}
|
For there may be situations in which there are leftover values for properties not having any memberMap defined. In these situations, with the current implementation the user would receive a NullReferenceException at runtime since GetMemberMapForElement will return null. If this situation is not allowed, a better approach would be throw a more specific exception but it seems to me that the ideal behavior would be to silently ignore the properties not having any memberMap.
Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).
Affects version 2.18 (latest release) and likely previous versions as well.
How to Reproduce
- Create a class without default constructor and having for example 5 properties while only 2 of them are set from the constructor, while the remaining three are set through methods.
- All properties have public getters and private setters
- Register the constructor as a creator with the driver
- Create a member map for 1 of the properties not being set in the constructor but none for the remaining 2 (and do not use automap)
- Create an instance of the class, set all properties and persist it in MongoDB using the driver
- Retrieve the record from the database to trigger deserialization.
Additional Background
Please note that the ability to have leftover values for instances created using creators is extremely useful (and even essential in our case) so I am by no mean asking for this feature to be removed.