Skip to content

Commit a88dc2e

Browse files
committed
CSHARP-4746: PipelineUpdateDefinition cannot be combined with any other update definition.
1 parent 0349851 commit a88dc2e

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

src/MongoDB.Driver/UpdateDefinitionBuilder.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,6 +1403,7 @@ internal sealed class CombinedUpdateDefinition<TDocument> : UpdateDefinition<TDo
14031403
public CombinedUpdateDefinition(IEnumerable<UpdateDefinition<TDocument>> updates)
14041404
{
14051405
_updates = Ensure.IsNotNull(updates, nameof(updates)).ToList();
1406+
ThrowIfPipelineUpdateDefinitionIsCombinedWithOtherUpdates(_updates);
14061407
}
14071408

14081409
public override BsonValue Render(IBsonSerializer<TDocument> documentSerializer, IBsonSerializerRegistry serializerRegistry, LinqProvider linqProvider)
@@ -1430,6 +1431,14 @@ public override BsonValue Render(IBsonSerializer<TDocument> documentSerializer,
14301431
}
14311432
return document;
14321433
}
1434+
1435+
private void ThrowIfPipelineUpdateDefinitionIsCombinedWithOtherUpdates(List<UpdateDefinition<TDocument>> updates)
1436+
{
1437+
if (updates.Count > 1 && updates.Any(x => x is PipelineUpdateDefinition<TDocument>))
1438+
{
1439+
throw new InvalidOperationException("Pipeline update definitions cannot be combined with any other update definitions.");
1440+
}
1441+
}
14331442
}
14341443

14351444
internal sealed class BitwiseOperatorUpdateDefinition<TDocument, TField> : UpdateDefinition<TDocument>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 FluentAssertions;
18+
using Xunit;
19+
20+
namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Jira
21+
{
22+
public class CSharp4746Tests : Linq3IntegrationTest
23+
{
24+
[Fact]
25+
public void UpdateDefinitionBuilder_Combine_should_throw_when_PipelineUpdateDefinition_is_combined_with_another_update()
26+
{
27+
var pipeline = new EmptyPipelineDefinition<C>();
28+
var pipelineUpdate = Builders<C>.Update.Pipeline(pipeline);
29+
var setUpdate = Builders<C>.Update.Set(x => x.X, 2);
30+
31+
var exception = Record.Exception(() => new UpdateDefinitionBuilder<C>().Combine(pipelineUpdate, setUpdate));
32+
33+
exception.Should().BeOfType<InvalidOperationException>();
34+
}
35+
36+
[Fact]
37+
public void UpdateDefinitionBuilder_Combine_should_throw_when_an_update_is_combined_with_a_PipelineUpdateDefinition()
38+
{
39+
var setUpdate = Builders<C>.Update.Set(x => x.X, 2);
40+
var pipeline = new EmptyPipelineDefinition<C>();
41+
var pipelineUpdate = Builders<C>.Update.Pipeline(pipeline);
42+
43+
var exception = Record.Exception(() => new UpdateDefinitionBuilder<C>().Combine(setUpdate, pipelineUpdate));
44+
45+
exception.Should().BeOfType<InvalidOperationException>();
46+
}
47+
48+
private class C
49+
{
50+
public int Id { get; set; }
51+
public int X { get; set; }
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)