Skip to content

Commit 24daa49

Browse files
rstamDmitryLukyanov
authored andcommitted
CSHARP-4509: Support Convert calls to a base type in expression translators.
1 parent 6632df2 commit 24daa49

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToAggregationExpressionTranslators/ConvertExpressionToAggregationExpressionTranslator.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ public static AggregationExpression Translate(TranslationContext context, UnaryE
4949
return TranslateConvertUnderlyingTypeToEnum(expression, operandTranslation);
5050
}
5151

52+
if (IsConvertToBaseType(sourceType: operandExpression.Type, targetType: expressionType))
53+
{
54+
return TranslateConvertToBaseType(expression, operandTranslation);
55+
}
56+
5257
if (expressionType.IsConstructedGenericType && expressionType.GetGenericTypeDefinition() == typeof(Nullable<>))
5358
{
5459
var valueType = expressionType.GetGenericArguments()[0];
@@ -106,6 +111,11 @@ private static bool IsConvertEnumToUnderlyingType(UnaryExpression expression)
106111
targetType.IsSameAsOrNullableOf(underlyingType);
107112
}
108113

114+
private static bool IsConvertToBaseType(Type sourceType, Type targetType)
115+
{
116+
return sourceType.IsSubclassOf(targetType);
117+
}
118+
109119
private static bool IsConvertUnderlyingTypeToEnum(UnaryExpression expression)
110120
{
111121
var sourceType = expression.Operand.Type;
@@ -116,6 +126,16 @@ private static bool IsConvertUnderlyingTypeToEnum(UnaryExpression expression)
116126
sourceType.IsSameAsOrNullableOf(underlyingType);
117127
}
118128

129+
private static AggregationExpression TranslateConvertToBaseType(UnaryExpression expression, AggregationExpression operandTranslation)
130+
{
131+
var baseType = expression.Type;
132+
var derivedType = expression.Operand.Type;
133+
var derivedTypeSerializer = operandTranslation.Serializer;
134+
var downcastingSerializer = DowncastingSerializer.Create(baseType, derivedType, derivedTypeSerializer);
135+
136+
return new AggregationExpression(expression, operandTranslation.Ast, downcastingSerializer);
137+
}
138+
119139
private static AggregationExpression TranslateConvertToBsonValue(TranslationContext context, UnaryExpression expression, Expression operand)
120140
{
121141
// handle double conversions like `(BsonValue)(object)x.Anything`
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* Copyright 2010-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System;
17+
using System.Linq;
18+
using System.Linq.Expressions;
19+
using MongoDB.Driver.Linq;
20+
using Xunit;
21+
22+
namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationTests.Jira
23+
{
24+
public class CSharp4509Tests : Linq3IntegrationTest
25+
{
26+
[Theory]
27+
[InlineData(JobSortColumn.DriverName, "DriverName", LinqProvider.V2)]
28+
[InlineData(JobSortColumn.DriverName, "DriverName", LinqProvider.V3)]
29+
[InlineData(JobSortColumn.JobNumber, "JobNumber", LinqProvider.V2)]
30+
[InlineData(JobSortColumn.JobNumber, "JobNumber", LinqProvider.V3)]
31+
public void OrderByDescending_should_work(
32+
JobSortColumn sortOrder,
33+
string expectedSortField,
34+
LinqProvider linqProvider)
35+
{
36+
var collection = CreateCollection(linqProvider);
37+
38+
Expression<Func<DbJob, object>> selector = sortOrder switch
39+
{
40+
JobSortColumn.JobNumber => j => j.JobNumber,
41+
JobSortColumn.DriverName => j => j.DriverName,
42+
_ => throw new NotSupportedException()
43+
};
44+
45+
var queryable =
46+
collection.AsQueryable()
47+
.OrderByDescending(selector);
48+
49+
var stages = Translate(collection, queryable);
50+
AssertStages(stages, $"{{ $sort : {{ {expectedSortField} : -1 }} }}");
51+
}
52+
53+
private IMongoCollection<DbJob> CreateCollection(LinqProvider linqProvider)
54+
{
55+
var collection = GetCollection<DbJob>("jobs", linqProvider);
56+
return collection;
57+
}
58+
59+
public class DbJob
60+
{
61+
public int JobNumber { get; set; }
62+
public string DriverName { get; set; }
63+
}
64+
65+
public enum JobSortColumn { JobNumber, DriverName }
66+
}
67+
}

0 commit comments

Comments
 (0)