Skip to content

Commit ff22548

Browse files
authored
CSHARP-4681: InvalidCastException when rendering projections (#1146)
1 parent 26fa40a commit ff22548

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

src/MongoDB.Driver/IFindFluentExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public static IFindFluent<TDocument, TNewProjection> Project<TDocument, TProject
5757
Ensure.IsNotNull(find, nameof(find));
5858
Ensure.IsNotNull(projection, nameof(projection));
5959

60-
return find.Project<TNewProjection>(new ExpressionProjectionDefinition<TDocument, TNewProjection>(projection, null));
60+
return find.Project<TNewProjection>(new FindExpressionProjectionDefinition<TDocument, TNewProjection>(projection));
6161
}
6262

6363
/// <summary>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 CSharp4681Tests : Linq3IntegrationTest
25+
{
26+
[Theory]
27+
[ParameterAttributeData]
28+
public void Find_projection_render_should_work(
29+
[Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider)
30+
{
31+
var collection = GetCollection(linqProvider);
32+
33+
var fluentFind = collection.Find(a => a.Id == "1").Project(a => a.Id);
34+
35+
var documentSerializer = collection.DocumentSerializer;
36+
var serializerRegistry = BsonSerializer.SerializerRegistry;
37+
var renderedProjection = fluentFind.Options.Projection.Render(documentSerializer, serializerRegistry, linqProvider);
38+
39+
renderedProjection.Document.Should().Be("{ _id : 1 }");
40+
41+
var result = fluentFind.Single();
42+
result.Should().Be("1");
43+
}
44+
45+
private IMongoCollection<A> GetCollection(LinqProvider linqProvider)
46+
{
47+
var collection = GetCollection<A>("test", linqProvider);
48+
CreateCollection(
49+
collection,
50+
new A { Id = "1" },
51+
new A { Id = "2" });
52+
return collection;
53+
}
54+
55+
private class A
56+
{
57+
public string Id { get; set; }
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)