Skip to content

Commit cacbcf8

Browse files
committed
CSHARP-5587: FindOneAndUpdate should insert correct discriminator value on upsert
1 parent e62da2b commit cacbcf8

File tree

2 files changed

+118
-4
lines changed

2 files changed

+118
-4
lines changed

src/MongoDB.Driver/FilteredMongoCollectionBase.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -304,22 +304,22 @@ public override Task<IAsyncCursor<TItem>> DistinctManyAsync<TItem>(IClientSessio
304304

305305
public override TProjection FindOneAndUpdate<TProjection>(FilterDefinition<TDocument> filter, UpdateDefinition<TDocument> update, FindOneAndUpdateOptions<TDocument, TProjection> options = null, CancellationToken cancellationToken = default(CancellationToken))
306306
{
307-
return _wrappedCollection.FindOneAndUpdate(CombineFilters(filter), update, options, cancellationToken);
307+
return _wrappedCollection.FindOneAndUpdate(CombineFilters(filter), AdjustUpdateDefinition(update, options.IsUpsert), options, cancellationToken);
308308
}
309309

310310
public override TProjection FindOneAndUpdate<TProjection>(IClientSessionHandle session, FilterDefinition<TDocument> filter, UpdateDefinition<TDocument> update, FindOneAndUpdateOptions<TDocument, TProjection> options = null, CancellationToken cancellationToken = default(CancellationToken))
311311
{
312-
return _wrappedCollection.FindOneAndUpdate(session, CombineFilters(filter), update, options, cancellationToken);
312+
return _wrappedCollection.FindOneAndUpdate(session, CombineFilters(filter), AdjustUpdateDefinition(update, options.IsUpsert), options, cancellationToken);
313313
}
314314

315315
public override Task<TProjection> FindOneAndUpdateAsync<TProjection>(FilterDefinition<TDocument> filter, UpdateDefinition<TDocument> update, FindOneAndUpdateOptions<TDocument, TProjection> options = null, CancellationToken cancellationToken = default(CancellationToken))
316316
{
317-
return _wrappedCollection.FindOneAndUpdateAsync(CombineFilters(filter), update, options, cancellationToken);
317+
return _wrappedCollection.FindOneAndUpdateAsync(CombineFilters(filter), AdjustUpdateDefinition(update, options.IsUpsert), options, cancellationToken);
318318
}
319319

320320
public override Task<TProjection> FindOneAndUpdateAsync<TProjection>(IClientSessionHandle session, FilterDefinition<TDocument> filter, UpdateDefinition<TDocument> update, FindOneAndUpdateOptions<TDocument, TProjection> options = null, CancellationToken cancellationToken = default(CancellationToken))
321321
{
322-
return _wrappedCollection.FindOneAndUpdateAsync(session, CombineFilters(filter), update, options, cancellationToken);
322+
return _wrappedCollection.FindOneAndUpdateAsync(session, CombineFilters(filter), AdjustUpdateDefinition(update, options.IsUpsert), options, cancellationToken);
323323
}
324324

325325
[Obsolete("Use Aggregation pipeline instead.")]
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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.Collections.Generic;
17+
using System.Linq;
18+
using MongoDB.Driver.TestHelpers;
19+
using FluentAssertions;
20+
using MongoDB.Bson.Serialization.Attributes;
21+
using MongoDB.Bson.Serialization.Serializers;
22+
using MongoDB.Driver.Linq;
23+
using Xunit;
24+
25+
namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Jira;
26+
27+
public class CSharp5587Tests : LinqIntegrationTest<CSharp5587Tests.ClassFixture>
28+
{
29+
public CSharp5587Tests(ClassFixture fixture)
30+
: base(fixture)
31+
{
32+
}
33+
34+
[Fact]
35+
public void FindOneAndUpdate_should_use_correct_discriminator()
36+
{
37+
var collection = Fixture.Collection;
38+
39+
var lion1 = new Lion { Id = 1, Name = "Lion1" };
40+
var updateDefinition1 = Builders<Lion>.Update
41+
.SetOnInsert(l => l.Id, 1)
42+
.Set(l => l.Name, lion1.Name);
43+
collection.OfType<Lion>().FindOneAndUpdate(
44+
f => f.Name == lion1.Name,
45+
updateDefinition1,
46+
new FindOneAndUpdateOptions<Lion> { IsUpsert = true });
47+
48+
var result = collection.AsQueryable().As(BsonDocumentSerializer.Instance).Single();
49+
result.Should().BeEquivalentTo(
50+
"""
51+
{
52+
_id : 1,
53+
_t : ["Animal", "Cat", "Lion"],
54+
Name : "Lion1"
55+
}
56+
""");
57+
}
58+
59+
[Fact]
60+
public void UpdateOne_should_use_correct_discriminator()
61+
{
62+
var collection = Fixture.Collection;
63+
64+
var lion2 = new Lion { Id = 2, Name = "Lion2" };
65+
var updateDefinition2 = Builders<Lion>.Update
66+
.SetOnInsert(l => l.Id, lion2.Id)
67+
.Set(l => l.Name, lion2.Name);
68+
collection.OfType<Lion>().UpdateOne(
69+
f => f.Name == lion2.Name,
70+
updateDefinition2,
71+
new UpdateOptions<Lion> { IsUpsert = true });
72+
73+
var result = collection.AsQueryable().As(BsonDocumentSerializer.Instance).Single();
74+
result.Should().BeEquivalentTo(
75+
"""
76+
{
77+
_id : 2,
78+
_t : ["Animal", "Cat", "Lion"],
79+
Name : "Lion2"
80+
}
81+
""");
82+
}
83+
84+
[BsonDiscriminator(RootClass = true)]
85+
[BsonKnownTypes(typeof(Cat), typeof(Dog))]
86+
public class Animal
87+
{
88+
public int Id { get; set; }
89+
}
90+
91+
[BsonKnownTypes(typeof(Lion), typeof(Tiger))]
92+
public class Cat : Animal
93+
{
94+
}
95+
96+
public class Dog : Animal
97+
{
98+
}
99+
100+
public class Lion : Cat
101+
{
102+
public string Name { get; set; }
103+
}
104+
public class Tiger : Cat
105+
{
106+
}
107+
108+
public sealed class ClassFixture : MongoCollectionFixture<Animal>
109+
{
110+
public override bool InitializeDataBeforeEachTestCase => true;
111+
112+
protected override IEnumerable<Animal> InitialData => null;
113+
}
114+
}

0 commit comments

Comments
 (0)