[CSHARP-4075] Matching creator not found in unwind with missing field that has default value Created: 23/Feb/22 Updated: 27/Oct/23 Resolved: 24/Feb/22 |
|
| Status: | Closed |
| Project: | C# Driver |
| Component/s: | Linq |
| Affects Version/s: | 2.14.1 |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Major - P3 |
| Reporter: | Joris Laperre | Assignee: | James Kovacs |
| Resolution: | Works as Designed | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Attachments: |
|
| Description |
| Comments |
| Comment by Joris Laperre [ 24/Feb/22 ] | ||||||||||||||
|
Hi James, Thanks for addressing this and taking the time to explain this in detail, much appreciated. Happy with both those workarounds too. Nothing further at this point, thanks again. | ||||||||||||||
| Comment by James Kovacs [ 24/Feb/22 ] | ||||||||||||||
|
Hi, joris@sequel-consulting.com, Thank you for filing this bug report. We have reproduced the issue and this appears to be expected behaviour. Let me explain in a bit more detail. When you construct a LINQ query, that query is translated into MQL and sent to the server. The server executes the MQL and returns the results as BSON. The driver then deserializes that BSON into POCOs (or BsonDocuments). In your example, you are deserializing the BSON into anonymous objects of type:
The C# compiler actually synthesizes a class during compile time to represent the anonymous type. In this case the compiler called it:
The driver will create a BsonClassMap on the fly to map the incoming BSON results into instances of the anonymous type. Note that we do not create intermediate instances of Movie nor do we use the BsonClassMap<Movie> during the deserialization process. That's why we don't have access to any default values set on the Movie class. The [BsonDefaultValue(0) attribute specifies a default value in the BsonClassMap<Movie>, it does not generate MQL for use by the server. The root cause of the problem is that you have no way to explicitly configure the BsonClassMap for an anonymous type because C# provides no way of specifying the anonymous type outside of the constructor. Option #1:
Option #2:
Here I have declared int? Runtime allowing me to specify a default value using Runtime = m.Runtime ?? 0. This translates into the following MQL (surrounding query not shown):
I have closed this ticket as "Works as Designed", but please let us know if you have any additional questions and we will be happy to answer them. Sincerely, | ||||||||||||||
| Comment by Joris Laperre [ 23/Feb/22 ] | ||||||||||||||
|
In BsonMemberMap.Reset(), I believe line 322 should read
instead of
| ||||||||||||||
| Comment by Joris Laperre [ 23/Feb/22 ] | ||||||||||||||
|
Stepped through it again, and I found that although DefaultValue in BsonMemberMap has a value, IsDefaultValueSpecified returns false, see attached screenshot. I think this is why the matching algorithm fails. |