[CSHARP-3108] Deserialization throws No matching creator found exception Created: 20/May/20 Updated: 27/Oct/23 Resolved: 17/Jun/20 |
|
| Status: | Closed |
| Project: | C# Driver |
| Component/s: | BSON |
| Affects Version/s: | 2.10.3, 2.10.4 |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Major - P3 |
| Reporter: | vargese antony | Assignee: | Robert Stam |
| Resolution: | Works as Designed | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Environment: |
Windows 10 |
||
| Issue Links: |
|
||||||||||||||||||||||||
| Description |
|
When reading the data using C# driver 2.10.4, below exception is thrown if document has not all the attributes, "An error occurred while deserializing the property <property name> of class <clas name>: No matching creator found." When using the driver version 2.10.1, we do not see the issue Mongo DB document:
Sample program to reproduce:
|
| Comments |
| Comment by Nicola Cassolato [ 03/Sep/21 ] | |||||||||||||
|
Hi @Robert, we're upgrading a large project to the latest version of the MongoDb c# driver and the issues presented in this ticket is impacting us. We cannot use decorators in our classes (they are in a project without referenced to the mongdb driver) Is there a way to specifify BsonDefaultValue(null) for all properties in all classes without the decorator? Thanks, | |||||||||||||
| Comment by Robert Stam [ 17/Jun/20 ] | |||||||||||||
|
Thank you for reporting this. You are right that there is a change in behavior for classes like Address starting with version 2.10.2 of the C# driver. The change is because we now recognize this class as an immutable class and as such will now instantiate instances of this class during deserialization by calling the constructor (we used to use reflection to create an uninitialized object and to force values into the uninitialized instance by also using reflection to call the private setters, which is not the best approach because it bypasses any validation or additional initialization the constructor might wish to do). The exception is being thrown because the document does not have a value for the Street property and therefore we don't have all the needed values to pass to the constructor. You can tell the driver to use a default value for missing fields by using the [BsonDefaultValue] annotation, as in:
I'm sorry this change caused a breaking change for you, but I think this is the proper behavior and I hope the existence of a workaround that lets you explicitly specify which fields should be considered optional (by providing default values for them) will work for you. | |||||||||||||
| Comment by vargese antony [ 20/May/20 ] | |||||||||||||
|
One update: When using the driver version 2.10.0, we do not see the issue |