From a88dc2eb20dc6113a0f31e4e4ece95a222c7b67b Mon Sep 17 00:00:00 2001 From: rstam Date: Mon, 7 Aug 2023 15:36:06 -0700 Subject: [PATCH] CSHARP-4746: PipelineUpdateDefinition cannot be combined with any other update definition. --- src/MongoDB.Driver/UpdateDefinitionBuilder.cs | 9 ++++ .../Jira/CSharp4746Tests.cs | 54 +++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Jira/CSharp4746Tests.cs diff --git a/src/MongoDB.Driver/UpdateDefinitionBuilder.cs b/src/MongoDB.Driver/UpdateDefinitionBuilder.cs index e962526a1bf..01ba1c1642f 100644 --- a/src/MongoDB.Driver/UpdateDefinitionBuilder.cs +++ b/src/MongoDB.Driver/UpdateDefinitionBuilder.cs @@ -1403,6 +1403,7 @@ internal sealed class CombinedUpdateDefinition : UpdateDefinition> updates) { _updates = Ensure.IsNotNull(updates, nameof(updates)).ToList(); + ThrowIfPipelineUpdateDefinitionIsCombinedWithOtherUpdates(_updates); } public override BsonValue Render(IBsonSerializer documentSerializer, IBsonSerializerRegistry serializerRegistry, LinqProvider linqProvider) @@ -1430,6 +1431,14 @@ public override BsonValue Render(IBsonSerializer documentSerializer, } return document; } + + private void ThrowIfPipelineUpdateDefinitionIsCombinedWithOtherUpdates(List> updates) + { + if (updates.Count > 1 && updates.Any(x => x is PipelineUpdateDefinition)) + { + throw new InvalidOperationException("Pipeline update definitions cannot be combined with any other update definitions."); + } + } } internal sealed class BitwiseOperatorUpdateDefinition : UpdateDefinition diff --git a/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Jira/CSharp4746Tests.cs b/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Jira/CSharp4746Tests.cs new file mode 100644 index 00000000000..1daba405344 --- /dev/null +++ b/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Jira/CSharp4746Tests.cs @@ -0,0 +1,54 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; +using FluentAssertions; +using Xunit; + +namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Jira +{ + public class CSharp4746Tests : Linq3IntegrationTest + { + [Fact] + public void UpdateDefinitionBuilder_Combine_should_throw_when_PipelineUpdateDefinition_is_combined_with_another_update() + { + var pipeline = new EmptyPipelineDefinition(); + var pipelineUpdate = Builders.Update.Pipeline(pipeline); + var setUpdate = Builders.Update.Set(x => x.X, 2); + + var exception = Record.Exception(() => new UpdateDefinitionBuilder().Combine(pipelineUpdate, setUpdate)); + + exception.Should().BeOfType(); + } + + [Fact] + public void UpdateDefinitionBuilder_Combine_should_throw_when_an_update_is_combined_with_a_PipelineUpdateDefinition() + { + var setUpdate = Builders.Update.Set(x => x.X, 2); + var pipeline = new EmptyPipelineDefinition(); + var pipelineUpdate = Builders.Update.Pipeline(pipeline); + + var exception = Record.Exception(() => new UpdateDefinitionBuilder().Combine(setUpdate, pipelineUpdate)); + + exception.Should().BeOfType(); + } + + private class C + { + public int Id { get; set; } + public int X { get; set; } + } + } +}