[CSHARP-4781] DefaultIfEmpty using wrong default value Created: 06/Sep/23 Updated: 28/Oct/23 Resolved: 25/Sep/23 |
|
| Status: | Closed |
| Project: | C# Driver |
| Component/s: | LINQ3 |
| Affects Version/s: | 2.19.0, 2.19.1, 2.19.2, 2.20.0, 2.21.0 |
| Fix Version/s: | 2.22.0 |
| Type: | Bug | Priority: | Major - P3 |
| Reporter: | Aleš Hübl | Assignee: | Robert Stam |
| Resolution: | Fixed | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Backwards Compatibility: | Fully Compatible |
| Documentation Changes: | Not Needed |
| Documentation Changes Summary: | 1. What would you like to communicate to the user about this feature? |
| Description |
| Comments |
| Comment by Aleš Hübl [ 26/Sep/23 ] | |||||||||||||||||||||||||||
|
Thank you Robert, we will try the fixed version soon! | |||||||||||||||||||||||||||
| Comment by Githook User [ 25/Sep/23 ] | |||||||||||||||||||||||||||
|
Author: {'name': 'rstam', 'email': 'robert@robertstam.org', 'username': 'rstam'}Message: | |||||||||||||||||||||||||||
| Comment by Robert Stam [ 09/Sep/23 ] | |||||||||||||||||||||||||||
|
I've been reading up on DefaultIfEmpty and I now believe this is a bug. DefaultIfEmpty should create the default value by using `default(TSource)` instead of by `new TSource()`. If TSource is a reference type the default value will be null.
| |||||||||||||||||||||||||||
| Comment by Aleš Hübl [ 08/Sep/23 ] | |||||||||||||||||||||||||||
|
Hello,
I believe that this piece of code was causing the problem:
As for the workarounds that you suggested. Adding an empty constructor would cause warnings because there are non nullable fields in the class and we have nullability checks turned on. This could be mitigated by some pragma of course. As for the DefaultIfEmpty, I don't think that it is that what causes the problem as I call it on the grouping, not on the UnwoundDatesCollectionPlan itself, but maybe I'm mistaken.
Anyway, thanks for your help, I appreciate it a lot. | |||||||||||||||||||||||||||
| Comment by Robert Stam [ 07/Sep/23 ] | |||||||||||||||||||||||||||
|
This might not be a bug, but until I see your sample code to reproduce it I won't know for sure. The reason I say that it might not be a bug is that I see `DefaultIfEmpty` on your call stack, and `DefaultIfEmpty` has to instantiate an instance of the class to use as the default value if the sequence is empty, and it does this by calling the no-argument constructor. You can work around this by either:
| |||||||||||||||||||||||||||
| Comment by Robert Stam [ 07/Sep/23 ] | |||||||||||||||||||||||||||
|
Thank you for reporting this issue. I can try to reverse engineer how this error occurs, but if you could provide some sample code reproducing the issue that would speed things up. | |||||||||||||||||||||||||||
| Comment by PM Bot [ 06/Sep/23 ] | |||||||||||||||||||||||||||
|
Hi ales.hubl@gmail.com, thank you for reporting this issue! The team will look into it and get back to you soon. |