Skip to content

Commit 8334d8d

Browse files
committed
CSHARP-4466: MongoQueryable.AppendStage uses incorrect result serializer type.
1 parent a1c67e7 commit 8334d8d

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed

src/MongoDB.Driver/Linq/MongoQueryable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public static IMongoQueryable<TResult> AppendStage<TSource, TResult>(
9696
GetMethodInfo(AppendStage, source, stage, resultSerializer),
9797
Expression.Convert(source.Expression, typeof(IMongoQueryable<TSource>)),
9898
Expression.Constant(stage),
99-
Expression.Constant(resultSerializer, typeof(IBsonSerializer<TSource>))));
99+
Expression.Constant(resultSerializer, typeof(IBsonSerializer<TResult>))));
100100
}
101101

102102
/// <summary>
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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 MongoDB.Bson.Serialization;
19+
using MongoDB.Driver.Linq;
20+
using Xunit;
21+
22+
namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationTests.Jira
23+
{
24+
public class CSharp4466Tests : Linq3IntegrationTest
25+
{
26+
[Fact]
27+
public void AppendStage_with_null_resultSerializer_should_work()
28+
{
29+
var collection = CreateCollection();
30+
31+
var queryable =
32+
collection.AsQueryable()
33+
.AppendStage<C, D>("{ $project : { X : '$_id', _id : 0 } }", resultSerializer: null);
34+
35+
var stages = Translate(collection, queryable, out var outputSerializer);
36+
37+
AssertStages(stages, "{ $project : { X : '$_id', _id : 0 } }");
38+
outputSerializer.ValueType.Should().Be(typeof(D));
39+
40+
var results = queryable.ToList();
41+
results.Select(x => x.X).Should().Equal(1, 2);
42+
}
43+
44+
[Fact]
45+
public void AppendStage_with_resultSerializer_should_work()
46+
{
47+
var collection = CreateCollection();
48+
var resultSerializer = BsonSerializer.LookupSerializer<D>();
49+
50+
var queryable =
51+
collection.AsQueryable()
52+
.AppendStage<C, D>("{ $project : { X : '$_id', _id : 0 } }", resultSerializer);
53+
54+
var stages = Translate(collection, queryable, out var outputSerializer);
55+
56+
AssertStages(stages, "{ $project : { X : '$_id', _id : 0 } }");
57+
outputSerializer.Should().BeSameAs(resultSerializer);
58+
59+
var results = queryable.ToList();
60+
results.Select(x => x.X).Should().Equal(1, 2);
61+
}
62+
63+
private IMongoCollection<C> CreateCollection()
64+
{
65+
var collection = GetCollection<C>("C");
66+
67+
CreateCollection(
68+
collection,
69+
new C { Id = 1 },
70+
new C { Id = 2 });
71+
72+
return collection;
73+
}
74+
75+
private class C
76+
{
77+
public int Id { get; set; }
78+
}
79+
80+
private class D
81+
{
82+
public int X { get; set; }
83+
}
84+
}
85+
}

0 commit comments

Comments
 (0)