Skip to content

Commit 945ae83

Browse files
authored
CSHARP-4723: Linq V2 Projection when upgrading from 2.19.1 to 2.19.2 (#1144)
1 parent 1f07760 commit 945ae83

File tree

2 files changed

+98
-3
lines changed

2 files changed

+98
-3
lines changed

src/MongoDB.Driver/MongoCollectionImpl.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ private EstimatedDocumentCountOperation CreateEstimatedDocumentCountOperation(Es
987987
private FindOneAndDeleteOperation<TProjection> CreateFindOneAndDeleteOperation<TProjection>(FilterDefinition<TDocument> filter, FindOneAndDeleteOptions<TDocument, TProjection> options)
988988
{
989989
var projection = options.Projection ?? new ClientSideDeserializationProjectionDefinition<TDocument, TProjection>();
990-
var renderedProjection = projection.Render(_documentSerializer, _settings.SerializerRegistry, _linqProvider);
990+
var renderedProjection = projection.RenderForFind(_documentSerializer, _settings.SerializerRegistry, _linqProvider);
991991

992992
return new FindOneAndDeleteOperation<TProjection>(
993993
_collectionNamespace,
@@ -1010,7 +1010,7 @@ private FindOneAndDeleteOperation<TProjection> CreateFindOneAndDeleteOperation<T
10101010
private FindOneAndReplaceOperation<TProjection> CreateFindOneAndReplaceOperation<TProjection>(FilterDefinition<TDocument> filter, object replacementObject, FindOneAndReplaceOptions<TDocument, TProjection> options)
10111011
{
10121012
var projection = options.Projection ?? new ClientSideDeserializationProjectionDefinition<TDocument, TProjection>();
1013-
var renderedProjection = projection.Render(_documentSerializer, _settings.SerializerRegistry, _linqProvider);
1013+
var renderedProjection = projection.RenderForFind(_documentSerializer, _settings.SerializerRegistry, _linqProvider);
10141014

10151015
return new FindOneAndReplaceOperation<TProjection>(
10161016
_collectionNamespace,
@@ -1037,7 +1037,7 @@ private FindOneAndReplaceOperation<TProjection> CreateFindOneAndReplaceOperation
10371037
private FindOneAndUpdateOperation<TProjection> CreateFindOneAndUpdateOperation<TProjection>(FilterDefinition<TDocument> filter, UpdateDefinition<TDocument> update, FindOneAndUpdateOptions<TDocument, TProjection> options)
10381038
{
10391039
var projection = options.Projection ?? new ClientSideDeserializationProjectionDefinition<TDocument, TProjection>();
1040-
var renderedProjection = projection.Render(_documentSerializer, _settings.SerializerRegistry, _linqProvider);
1040+
var renderedProjection = projection.RenderForFind(_documentSerializer, _settings.SerializerRegistry, _linqProvider);
10411041

10421042
return new FindOneAndUpdateOperation<TProjection>(
10431043
_collectionNamespace,
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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.Driver.Linq;
18+
using MongoDB.TestHelpers.XunitExtensions;
19+
using Xunit;
20+
21+
namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Jira
22+
{
23+
public class CSharp4723Tests : Linq3IntegrationTest
24+
{
25+
[Theory]
26+
[ParameterAttributeData]
27+
public void Find_projection_in_findoneandupdate_should_work(
28+
[Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider)
29+
{
30+
var collection = GetCollection(linqProvider);
31+
32+
var update = Builders<A>.Update.Set("Value", "updated");
33+
var options = new FindOneAndUpdateOptions<A>
34+
{
35+
Projection = Builders<A>.Projection.Expression(x => x)
36+
};
37+
38+
var result = collection.FindOneAndUpdate<A, A>(x => x.Id == 1, update, options);
39+
40+
result.Id.Should().Be(1);
41+
result.Value.Should().Be("1");
42+
}
43+
44+
[Theory]
45+
[ParameterAttributeData]
46+
public void Find_projection_in_findoneandreplace_should_work(
47+
[Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider)
48+
{
49+
var collection = GetCollection(linqProvider);
50+
var options = new FindOneAndReplaceOptions<A, A>
51+
{
52+
Projection = Builders<A>.Projection.Expression(x => x)
53+
};
54+
55+
var result = collection.FindOneAndReplace<A, A>(x => x.Id == 1, new A { Id = 1, Value = "updated" }, options);
56+
57+
result.Id.Should().Be(1);
58+
result.Value.Should().Be("1");
59+
}
60+
61+
[Theory]
62+
[ParameterAttributeData]
63+
public void Find_projection_in_findoneanddelete_should_work(
64+
[Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider)
65+
{
66+
var collection = GetCollection(linqProvider);
67+
68+
var options = new FindOneAndDeleteOptions<A>
69+
{
70+
Projection = Builders<A>.Projection.Expression(x => x),
71+
};
72+
73+
var result = collection.FindOneAndDelete<A, A>(x => x.Id == 1, options);
74+
75+
result.Id.Should().Be(1);
76+
result.Value.Should().Be("1");
77+
}
78+
79+
private IMongoCollection<A> GetCollection(LinqProvider linqProvider)
80+
{
81+
var collection = GetCollection<A>("test", linqProvider);
82+
CreateCollection(
83+
collection,
84+
new A { Id = 1, Value = "1"});
85+
return collection;
86+
}
87+
88+
private class A
89+
{
90+
public int Id { get; set; }
91+
92+
public string Value { get; set; }
93+
}
94+
}
95+
}

0 commit comments

Comments
 (0)