Skip to content

Commit f96b5a4

Browse files
committed
CSHARP-5463: Investigate whether calls to EnsureQueryableMethodHasNestedAsQueryableSource are actually needed.
1 parent 167afae commit f96b5a4

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static void EnsureQueryableMethodHasNestedAsQueryableSource(MethodCallExp
2727
if (expression.Method.DeclaringType == typeof(Queryable) &&
2828
sourceTranslation.Serializer is not INestedAsQueryableSerializer)
2929
{
30-
throw new ExpressionNotSupportedException(expression, because: "source serializer is not a NestedAsQueryableSerializer");
30+
throw new ExpressionNotSupportedException(expression, because: "source argument is an unsupported IQueryable type");
3131
}
3232
}
3333

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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.Collections;
18+
using System.Collections.Generic;
19+
using System.Linq;
20+
using System.Linq.Expressions;
21+
using MongoDB.Driver.TestHelpers;
22+
using FluentAssertions;
23+
using MongoDB.Driver.Linq;
24+
using Xunit;
25+
26+
namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Jira;
27+
28+
public class CSharp5463Tests : LinqIntegrationTest<CSharp5463Tests.ClassFixture>
29+
{
30+
public CSharp5463Tests(ClassFixture fixture)
31+
: base(fixture)
32+
{
33+
}
34+
35+
[Fact]
36+
public void Nested_AsQueryable_Contains_constant_should_be_partially_evaluated()
37+
{
38+
var collection = Fixture.Collection;
39+
var enumerable = new int[] { 1, 2, 3 };
40+
41+
var queryable = collection.AsQueryable()
42+
.Select(x => enumerable.AsQueryable().Contains(1));
43+
44+
var stages = Translate(collection, queryable);
45+
AssertStages(stages, "{ $project : { _v : { $in : [1, [1, 2, 3]] }, _id : 0 } }");
46+
47+
var result = queryable.Single();
48+
result.Should().Be(true);
49+
}
50+
51+
[Fact]
52+
public void NestedQueryable_Contains_constant_should_be_partially_evaluated()
53+
{
54+
var collection = Fixture.Collection;
55+
var nestedQueryable = new int[] { 1, 2, 3 }.AsQueryable();
56+
57+
var queryable = collection.AsQueryable()
58+
.Select(x => nestedQueryable.Contains(1)); // PartialEvaluator turns this into Select(x => true)
59+
60+
var stages = Translate(collection, queryable);
61+
AssertStages(stages, "{ $project : { _v : { $literal : true }, _id : 0 } }");
62+
63+
var result = queryable.Single();
64+
result.Should().Be(true);
65+
}
66+
67+
[Fact]
68+
public void NestedQueryable_Contains_field_should_throw()
69+
{
70+
var collection = Fixture.Collection;
71+
var nestedQueryable = new int[] { 1, 2, 3 }.AsQueryable();
72+
73+
var queryable = collection.AsQueryable()
74+
.Select(x => nestedQueryable.Contains(x.Id));
75+
76+
var exception = Record.Exception(() => Translate(collection, queryable));
77+
exception.Should().BeOfType<ExpressionNotSupportedException>();
78+
exception.Message.Should().Contain("source argument is an unsupported IQueryable type");
79+
}
80+
81+
public class C
82+
{
83+
public int Id { get; set; }
84+
}
85+
86+
public sealed class ClassFixture : MongoCollectionFixture<C>
87+
{
88+
protected override IEnumerable<C> InitialData =>
89+
[
90+
new C { Id = 1 }
91+
];
92+
}
93+
}

0 commit comments

Comments
 (0)