[CSHARP-4509] Support Convert calls to a base type in expression translators Created: 31/Jan/23  Updated: 28/Oct/23  Resolved: 16/Feb/23

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: None
Fix Version/s: 2.19.1

Type: Bug Priority: Major - P3
Reporter: Mirek Kukla Assignee: Robert Stam
Resolution: Fixed Votes: 0
Labels: LINQ3
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: File Program.cs    
Issue Links:
Duplicate
duplicates CSHARP-4499 Support Convert calls to a base type ... Closed
Documentation Changes: Not Needed
Documentation Changes Summary:

1. What would you like to communicate to the user about this feature?
2. Would you like the user to see examples of the syntax and/or executable code and its output?
3. Which versions of the driver/connector does this apply to?


 Description   

Summary

When migrating to LINQ provider v3, sorting no longer works in cases where the key selector expression has a compile time type of `System.Object` (but the appropriate runtime type).

We discovered this when migrating to driver version 2.19.0, where LINQ3 is the new default. If we manually set the LINQ provider to v2, things go back to working as expected.

How to Reproduce

See attached for a minimal console app that reproduces the behavior. Notable lines:

```
Expression<Func<DbJob, object>> selector = sortOrder switch

{     JobSortColumn.JobNumber => j => j.JobNumber,     JobSortColumn.DriverName => j => j.DriverName, }

;

jobsQueryable = jobsQueryable.OrderByDescending(selector);
var jobs = await jobsQueryable.ToListAsync(); // error if LINQ provider is v3
```

The resulting exception if LINQ3 is enabled:

```

Unhandled exception. MongoDB.Driver.Linq.ExpressionNotSupportedException: Expression not supported: Convert(j.JobNumber, Object) because conversion to System.Object is not supported.
   at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.ConvertExpressionToAggregationExpressionTranslator.Translate(TranslationContext context, UnaryExpression expression)
   at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.UnaryExpressionToAggregationExpressionTranslator.Translate(TranslationContext context, UnaryExpression expression)
   at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.ExpressionToAggregationExpressionTranslator.Translate(TranslationContext context, Expression expression)
   at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.ExpressionToAggregationExpressionTranslator.TranslateLambdaBody(TranslationContext context, LambdaExpression lambdaExpression, IBsonSerializer parameterSerializer, Boolean asRoot)
   at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToPipelineTranslators.OrderByMethodToPipelineTranslator.Translate(TranslationContext context, MethodCallExpression expression)
   at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToPipelineTranslators.ExpressionToPipelineTranslator.Translate(TranslationContext context, Expression expression)
   at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToExecutableQueryTranslators.ExpressionToExecutableQueryTranslator.Translate[TDocument,TOutput](MongoQueryProvider`1 provider, Expression expression)
   at MongoDB.Driver.Linq.Linq3Implementation.MongoQuery`2.ToCursorAsync(CancellationToken cancellationToken)
   at MongoDB.Driver.IAsyncCursorSourceExtensions.ToListAsync[TDocument](IAsyncCursorSource`1 source, CancellationToken cancellationToken)
   at Program.<Main>$(String[] args) in /Users/mirek/RiderProjects/ConsoleApp1/ConsoleApp1/Program.cs:line 21
   at Program.<Main>(String[] args)

```



 Comments   
Comment by Githook User [ 24/Mar/23 ]

Author:

{'name': 'rstam', 'email': 'robert@robertstam.org', 'username': 'rstam'}

Message: CSHARP-4509: Support Convert calls to a base type in expression translators.
Branch: v2.19.x
https://github.com/mongodb/mongo-csharp-driver/commit/24daa49a4addf66198901b529d15b7531bc88d7e

Comment by Githook User [ 16/Feb/23 ]

Author:

{'name': 'rstam', 'email': 'robert@robertstam.org', 'username': 'rstam'}

Message: CSHARP-4509: Support Convert calls to a base type in expression translators.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/2df43ecf335d3a96e8423b93718c70bd134fc81d

Comment by Robert Stam [ 01/Feb/23 ]

Thanks for reporting this issue, and for making it extra easy for me to reproduce by providing a minimal console app exhibiting the issue.

This is similar to CSHARP-4499 but different because that was in the filter language translators and this is in the aggregation language translators. The same technique used to fix CSHARP-4499 works to fix this issue also, but it's slightly different code because it's in a different part of the code base.

Comment by Robert Stam [ 01/Feb/23 ]

Similar to but slightly different from CSHARP-4499.

Comment by Mirek Kukla [ 31/Jan/23 ]

Apologies for the odd formatting, it seems I'm not able to make edits to my own ticket.

Generated at Wed Feb 07 21:48:26 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.