Skip to content

Commit 040501c

Browse files
committed
CSHARP-5587: FindOneAndUpdate should insert correct discriminator value on upsert
1 parent 79d97ed commit 040501c

File tree

2 files changed

+122
-4
lines changed

2 files changed

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

0 commit comments

Comments
 (0)