Skip to content

Commit e37fd92

Browse files
committed
CSHARP-4061: Use correct LinqProvider in AggregateFluent ToString.
1 parent ba40db6 commit e37fd92

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

src/MongoDB.Driver/AggregateFluent.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,8 @@ public override IAggregateFluent<TNewResult> Unwind<TNewResult>(FieldDefinition<
270270

271271
public override string ToString()
272272
{
273-
return $"aggregate({_pipeline})";
273+
var linqProvider = Database.Client.Settings.LinqProvider;
274+
return $"aggregate({_pipeline.ToString(linqProvider)})";
274275
}
275276

276277
protected abstract IAggregateFluent<TNewResult> WithPipeline<TNewResult>(PipelineDefinition<TInput, TNewResult> pipeline);

src/MongoDB.Driver/PipelineDefinition.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,22 @@ public virtual RenderedPipelineDefinition<TOutput> Render(IBsonSerializer<TInput
100100

101101
/// <inheritdoc/>
102102
public override string ToString()
103+
{
104+
return ToString(LinqProvider.V2);
105+
}
106+
107+
/// <summary>
108+
/// Returns a <see cref="System.String" /> that represents this instance.
109+
/// </summary>
110+
/// <param name="linqProvider">The LINQ provider.</param>
111+
/// <returns>
112+
/// A <see cref="System.String" /> that represents this instance.
113+
/// </returns>
114+
public string ToString(LinqProvider linqProvider)
103115
{
104116
var serializerRegistry = BsonSerializer.SerializerRegistry;
105117
var inputSerializer = serializerRegistry.GetSerializer<TInput>();
106-
return ToString(inputSerializer, serializerRegistry);
118+
return ToString(inputSerializer, serializerRegistry, linqProvider);
107119
}
108120

109121
/// <summary>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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.Linq;
17+
using FluentAssertions;
18+
using Xunit;
19+
20+
namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationTests.Jira
21+
{
22+
public class CSharp4061Tests : Linq3IntegrationTest
23+
{
24+
[Fact]
25+
public void AggregateFluent_ToString_should_return_expected_result()
26+
{
27+
var collection = GetCollection<C>();
28+
var subject = collection.Aggregate()
29+
.Group(x => x.X, g => new { X = g.Key, Count = g.Count() });
30+
31+
var result = subject.ToString();
32+
33+
result.Should().Be("aggregate([{ \"$group\" : { \"_id\" : \"$X\", \"__agg0\" : { \"$sum\" : 1 } } }, { \"$project\" : { \"X\" : \"$_id\", \"Count\" : \"$__agg0\", \"_id\" : 0 } }])");
34+
}
35+
36+
private class C
37+
{
38+
public int Id { get; set; }
39+
public int X { get; set; }
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)