Skip to content

Commit 6216289

Browse files
committed
CSHARP-4535: Support queries after casting IQueryable<Derived> to IQueryable<Base>.
1 parent dbaabb6 commit 6216289

File tree

3 files changed

+81
-3
lines changed

3 files changed

+81
-3
lines changed

src/MongoDB.Driver/Linq/Linq3Implementation/Misc/Symbol.cs

+6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16+
using System;
1617
using System.Linq.Expressions;
1718
using MongoDB.Bson.Serialization;
1819
using MongoDB.Driver.Core.Misc;
@@ -37,6 +38,11 @@ public Symbol(ParameterExpression parameter, string name, AstExpression ast, IBs
3738
_ast = Ensure.IsNotNull(ast, nameof(ast));
3839
_serializer = Ensure.IsNotNull(serializer, nameof(serializer));
3940
_isCurrent = isCurrent;
41+
42+
if (_serializer.ValueType != _parameter.Type)
43+
{
44+
throw new ArgumentException($"Value type {_serializer.ValueType} of serializer type {_serializer.GetType()} does not match parameter type {_parameter.Type}.");
45+
}
4046
}
4147

4248
// public properties

src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToFilterTranslators/ExpressionToFilterTranslator.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
* limitations under the License.
1414
*/
1515

16-
using System;
1716
using System.Linq;
1817
using System.Linq.Expressions;
1918
using MongoDB.Bson.Serialization;
19+
using MongoDB.Bson.Serialization.Serializers;
2020
using MongoDB.Driver.Linq.Linq3Implementation.Ast.Filters;
2121
using MongoDB.Driver.Linq.Linq3Implementation.Misc;
2222
using MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators;
@@ -56,9 +56,10 @@ public static AstFilter TranslateLambda(
5656
bool asRoot = false)
5757
{
5858
var parameterExpression = lambdaExpression.Parameters.Single();
59-
if (parameterSerializer.ValueType != parameterExpression.Type)
59+
60+
if (parameterSerializer.ValueType != parameterExpression.Type && parameterExpression.Type.IsAssignableFrom(parameterSerializer.ValueType))
6061
{
61-
throw new ArgumentException($"ValueType '{parameterSerializer.ValueType.FullName}' of parameterSerializer does not match parameter type '{parameterExpression.Type.FullName}'.", nameof(parameterSerializer));
62+
parameterSerializer = DowncastingSerializer.Create(parameterExpression.Type, parameterSerializer.ValueType, parameterSerializer);
6263
}
6364

6465
var parameterSymbol =
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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.Collections.Generic;
17+
using System.Linq;
18+
using MongoDB.Driver.TestHelpers;
19+
using FluentAssertions;
20+
using Xunit;
21+
22+
namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Jira;
23+
24+
public class CSharp4535Tests : LinqIntegrationTest<CSharp4535Tests.ClassFixture>
25+
{
26+
public CSharp4535Tests(ClassFixture fixture)
27+
: base(fixture)
28+
{
29+
}
30+
31+
[Fact]
32+
public void Empty_query_should_work()
33+
{
34+
var mongoEntityCollection = Fixture.Collection;
35+
var entityQueryable = (IQueryable<Entity>)mongoEntityCollection.AsQueryable();
36+
37+
var results = entityQueryable.ToArray();
38+
39+
results.Select(x => x.Id).Should().Equal(1);
40+
}
41+
42+
[Fact]
43+
public void Where_should_work()
44+
{
45+
var mongoEntityCollection = Fixture.Collection;
46+
var entityQueryable = (IQueryable<Entity>)mongoEntityCollection.AsQueryable();
47+
48+
var queryable = entityQueryable.Where(x => x.Id == 1);
49+
50+
var results = queryable.ToArray();
51+
52+
results.Select(x => x.Id).Should().Equal(1);
53+
}
54+
55+
public class Entity
56+
{
57+
public int Id { get; set; }
58+
}
59+
60+
public class MongoEntity : Entity
61+
{
62+
}
63+
64+
public sealed class ClassFixture : MongoCollectionFixture<MongoEntity>
65+
{
66+
protected override IEnumerable<MongoEntity> InitialData =>
67+
[
68+
new MongoEntity { Id = 1 }
69+
];
70+
}
71+
}

0 commit comments

Comments
 (0)