[CSHARP-4609] InvalidCastException with LinqProvider V3 when passing string as DateTime Created: 11/Apr/23 Updated: 28/Oct/23 Resolved: 20/Apr/23 |
|
| Status: | Closed |
| Project: | C# Driver |
| Component/s: | LINQ3 |
| Affects Version/s: | None |
| Fix Version/s: | 2.19.2 |
| Type: | Bug | Priority: | Minor - P4 |
| Reporter: | James Kovacs | Assignee: | Robert Stam |
| Resolution: | Fixed | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||
| 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 |
|
when adding filter like this LinqProvider V2 works fine, but V3 version throws exception
So it seems on this line in DowncastingSerializer it throws exception because it simply tries to cast string to DateTime. Not sure if that is intended behavior in new Linq version or a bug? – created from https://www.mongodb.com/community/forums/t/invalidcastexception-with-linqprovider-v3-when-passing-string-as-datetime/221558/1 |
| Comments |
| Comment by Githook User [ 18/May/23 ] | |||||||||||||||||||||
|
Author: {'name': 'rstam', 'email': 'robert@robertstam.org', 'username': 'rstam'}Message: | |||||||||||||||||||||
| Comment by Githook User [ 20/Apr/23 ] | |||||||||||||||||||||
|
Author: {'name': 'rstam', 'email': 'robert@robertstam.org', 'username': 'rstam'}Message: | |||||||||||||||||||||
| Comment by Robert Stam [ 13/Apr/23 ] | |||||||||||||||||||||
|
This was not an intentional breaking change in LINQ3. LINQ3 assumes that you are comparing apples to apples. We were not expecting anyone to compare a `DateTime` to a `string`. The best/cleanest work around would be to change your code to compare apples to apples (`DateTime` to `DateTime` in this case). We are evaluating whether we should change LINQ3 to also support this somewhat unusual and surprising behavior from LINQ2.
| |||||||||||||||||||||
| Comment by James Kovacs [ 11/Apr/23 ] | |||||||||||||||||||||
|
We have a field expression of type object where the underlying type is DateTime or any value type. In LinqProviderAdapterV2.cs, we have the following code in TranslateExpressionToField:
The underlying serializer is a DateTimeSerializer, which is not an IBsonSerializer<object>. Thus fieldSerializer is null and the resulting valueSerializer is FieldValueSerializerHelper.ConvertIfPossibleSerializer<object, DateTime>. This serializer attempts to convert from a string to a DateTime. In LINQ3, we introduced the DowncastingSerializer, which resolves One solution is for ConvertExpressionToFilterFieldTranslator to detect a convert from a value type to object and simply return the existing field definition. Then the fieldSerializer is null as before and the correct FieldValueSerializerHelper.ConvertIfPossibleSerializer<object, DateTime> is created on the third line. Not the most elegant solution, but it replicates the code flow for value types prior to the introduction of DowncastingSerializer. The following is a repro of the issue:
|