Skip to content

Commit 1f07760

Browse files
committed
CSHARP-4742: Find and Aggregate Project should work with identity projection x => x on all server versions.
1 parent 59eb17e commit 1f07760

File tree

4 files changed

+183
-4
lines changed

4 files changed

+183
-4
lines changed

src/MongoDB.Driver/Linq/Linq2Implementation/LinqProviderAdapterV2.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ internal override RenderedProjectionDefinition<TOutput> TranslateExpressionToPro
152152
IBsonSerializerRegistry serializerRegistry,
153153
ExpressionTranslationOptions translationOptions)
154154
{
155+
if (expression.Parameters.Count == 1 && expression.Body == expression.Parameters[0])
156+
{
157+
// handle x => x as a special case
158+
return new RenderedProjectionDefinition<TOutput>(null, (IBsonSerializer<TOutput>)inputSerializer);
159+
}
160+
155161
return AggregateProjectTranslator.Translate<TInput, TOutput>(expression, inputSerializer, serializerRegistry, translationOptions);
156162
}
157163
}

src/MongoDB.Driver/Linq/Linq3Implementation/LinqProviderAdapterV3.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,12 @@ private RenderedProjectionDefinition<TOutput> TranslateExpressionToProjection<TI
166166
Func<AggregationExpression, (IReadOnlyList<AstProjectStageSpecification>, IBsonSerializer)> projectionCreator,
167167
AstSimplifier simplifier)
168168
{
169+
if (expression.Parameters.Count == 1 && expression.Body == expression.Parameters[0])
170+
{
171+
// handle x => x as a special case
172+
return new RenderedProjectionDefinition<TOutput>(null, (IBsonSerializer<TOutput>)inputSerializer);
173+
}
174+
169175
expression = (Expression<Func<TInput, TOutput>>)PartialEvaluator.EvaluatePartially(expression);
170176
var context = TranslationContext.Create(expression, inputSerializer);
171177
var translation = ExpressionToAggregationExpressionTranslator.TranslateLambdaBody(context, expression, inputSerializer, asRoot: true);

src/MongoDB.Driver/PipelineStageDefinitionBuilder.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,16 +1265,17 @@ public static PipelineStageDefinition<TInput, TOutput> Project<TInput, TOutput>(
12651265
(s, sr, linqProvider) =>
12661266
{
12671267
var renderedProjection = projection.Render(s, sr, linqProvider);
1268-
BsonDocument document;
1268+
IEnumerable<BsonDocument> documents;
12691269
if (renderedProjection.Document == null)
12701270
{
1271-
document = new BsonDocument();
1271+
// renderedProjection.Document will be null for x => x (and perhaps other cases also in the future)
1272+
documents = Array.Empty<BsonDocument>();
12721273
}
12731274
else
12741275
{
1275-
document = new BsonDocument(operatorName, renderedProjection.Document);
1276+
documents = new[] { new BsonDocument(operatorName, renderedProjection.Document) };
12761277
}
1277-
return new RenderedPipelineStageDefinition<TOutput>(operatorName, document, renderedProjection.ProjectionSerializer);
1278+
return new RenderedPipelineStageDefinition<TOutput>(operatorName, documents, renderedProjection.ProjectionSerializer);
12781279
});
12791280

12801281
return stage;
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
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 FluentAssertions;
17+
using MongoDB.Bson.Serialization;
18+
using MongoDB.Driver.Linq;
19+
using MongoDB.TestHelpers.XunitExtensions;
20+
using Xunit;
21+
22+
namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Jira
23+
{
24+
public class CSharp4742Tests : Linq3IntegrationTest
25+
{
26+
[Theory]
27+
[ParameterAttributeData]
28+
public void Find_with_identity_projection_should_work(
29+
[Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider)
30+
{
31+
var collection = GetCollection(linqProvider);
32+
var projection = Builders<C>.Projection.Expression(x => x);
33+
34+
var find = collection
35+
.Find("{}")
36+
.Project(projection);
37+
38+
var renderedProjection = TranslateFindProjection(collection, find);
39+
renderedProjection.Should().BeNull();
40+
41+
var result = find.Single();
42+
result.Id.Should().Be(1);
43+
result.X.Should().Be(2);
44+
}
45+
46+
[Theory]
47+
[ParameterAttributeData]
48+
public void Aggregate_Project_with_identity_projection_should_work(
49+
[Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider)
50+
{
51+
var collection = GetCollection(linqProvider);
52+
var projection = Builders<C>.Projection.Expression(x => x);
53+
54+
var aggregate = collection
55+
.Aggregate()
56+
.Project(projection);
57+
58+
var stages = Translate(collection, aggregate);
59+
stages.Should().BeEmpty();
60+
61+
var result = aggregate.Single();
62+
result.Id.Should().Be(1);
63+
result.X.Should().Be(2);
64+
}
65+
66+
[Theory]
67+
[ParameterAttributeData]
68+
public void ExpressionProjectionDefinition_with_identity_projection_Render_should_work(
69+
[Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider)
70+
{
71+
var collection = GetCollection(linqProvider);
72+
var projection = new ExpressionProjectionDefinition<C, C>(x => x, translationOptions: null);
73+
var sourceSerializer = collection.DocumentSerializer;
74+
var serializerRegistry = BsonSerializer.SerializerRegistry;
75+
76+
var renderedProjection = projection.Render(sourceSerializer, serializerRegistry, linqProvider);
77+
78+
renderedProjection.Document.Should().BeNull();
79+
renderedProjection.ProjectionSerializer.Should().BeSameAs(sourceSerializer);
80+
}
81+
82+
[Theory]
83+
[ParameterAttributeData]
84+
public void ExpressionProjectionDefinition_with_identity_projection_RenderForFind_should_work(
85+
[Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider)
86+
{
87+
var collection = GetCollection(linqProvider);
88+
var projection = new ExpressionProjectionDefinition<C, C>(x => x, translationOptions: null);
89+
var sourceSerializer = collection.DocumentSerializer;
90+
var serializerRegistry = BsonSerializer.SerializerRegistry;
91+
92+
var renderedProjection = projection.RenderForFind(sourceSerializer, serializerRegistry, linqProvider);
93+
94+
renderedProjection.Document.Should().BeNull();
95+
if (linqProvider == LinqProvider.V2)
96+
{
97+
renderedProjection.ProjectionSerializer.ValueType.Should().Be(typeof(C));
98+
}
99+
else
100+
{
101+
renderedProjection.ProjectionSerializer.Should().BeSameAs(sourceSerializer);
102+
}
103+
}
104+
105+
[Theory]
106+
[ParameterAttributeData]
107+
public void FindExpressionProjectionDefinition_with_identity_projection_Render_should_work(
108+
[Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider)
109+
{
110+
var collection = GetCollection(linqProvider);
111+
var projection = new FindExpressionProjectionDefinition<C, C>(x => x);
112+
var sourceSerializer = collection.DocumentSerializer;
113+
var serializerRegistry = BsonSerializer.SerializerRegistry;
114+
115+
var renderedProjection = projection.Render(sourceSerializer, serializerRegistry, linqProvider);
116+
117+
renderedProjection.Document.Should().BeNull();
118+
if (linqProvider == LinqProvider.V2)
119+
{
120+
renderedProjection.ProjectionSerializer.ValueType.Should().Be(typeof(C));
121+
}
122+
else
123+
{
124+
renderedProjection.ProjectionSerializer.Should().BeSameAs(sourceSerializer);
125+
}
126+
}
127+
128+
[Theory]
129+
[ParameterAttributeData]
130+
public void FindExpressionProjectionDefinition_with_identity_projection_RenderForFind_should_work(
131+
[Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider)
132+
{
133+
var collection = GetCollection(linqProvider);
134+
var projection = new FindExpressionProjectionDefinition<C, C>(x => x);
135+
var sourceSerializer = collection.DocumentSerializer;
136+
var serializerRegistry = BsonSerializer.SerializerRegistry;
137+
138+
var renderedProjection = projection.RenderForFind(sourceSerializer, serializerRegistry, linqProvider);
139+
140+
renderedProjection.Document.Should().BeNull();
141+
if (linqProvider == LinqProvider.V2)
142+
{
143+
renderedProjection.ProjectionSerializer.ValueType.Should().Be(typeof(C));
144+
}
145+
else
146+
{
147+
renderedProjection.ProjectionSerializer.Should().BeSameAs(sourceSerializer);
148+
}
149+
}
150+
151+
private IMongoCollection<C> GetCollection(LinqProvider linqProvider)
152+
{
153+
var collection = GetCollection<C>("test", linqProvider);
154+
CreateCollection(
155+
collection,
156+
new C { Id = 1, X = 2 });
157+
return collection;
158+
}
159+
160+
private class C
161+
{
162+
public int Id { get; set; }
163+
public int X { get; set; }
164+
}
165+
}
166+
}

0 commit comments

Comments
 (0)