Skip to content

Commit 6477cb5

Browse files
committed
CSHARP-1597: empty updates should throw exceptions so as not to inadvertantly overwrite entire documents.
1 parent 0e1b58b commit 6477cb5

File tree

6 files changed

+86
-0
lines changed

6 files changed

+86
-0
lines changed

src/MongoDB.Driver.Core.Tests/Core/Operations/FindOneAndUpdateOperationTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ public void Constructor_should_throw_when_update_is_null()
6161
act.ShouldThrow<ArgumentNullException>();
6262
}
6363

64+
[Test]
65+
public void Constructor_should_throw_when_update_is_empty()
66+
{
67+
Action act = () => new FindOneAndUpdateOperation<BsonDocument>(_collectionNamespace, _filter, new BsonDocument(), BsonDocumentSerializer.Instance, _messageEncoderSettings);
68+
69+
act.ShouldThrow<ArgumentException>();
70+
}
71+
6472
[Test]
6573
public void Constructor_should_throw_when_result_serializer_is_null()
6674
{
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/* Copyright 2016 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 MongoDB.Bson;
19+
using NUnit.Framework;
20+
21+
namespace MongoDB.Driver.Core.Operations
22+
{
23+
[TestFixture]
24+
public class UpdateRequestTests : OperationTestBase
25+
{
26+
[Test]
27+
public void Constructor_should_throw_when_filter_is_null()
28+
{
29+
Action act = () => new UpdateRequest(UpdateType.Update, null, new BsonDocument());
30+
31+
act.ShouldThrow<ArgumentNullException>();
32+
}
33+
34+
[Test]
35+
public void Constructor_should_throw_when_update_is_null()
36+
{
37+
Action act = () => new UpdateRequest(UpdateType.Update, new BsonDocument(), null);
38+
39+
act.ShouldThrow<ArgumentNullException>();
40+
}
41+
42+
[Test]
43+
public void Constructor_should_throw_when_update_is_empty_and_update_type_is_Update()
44+
{
45+
Action act = () => new UpdateRequest(UpdateType.Update, new BsonDocument(), new BsonDocument());
46+
47+
act.ShouldThrow<ArgumentException>();
48+
}
49+
50+
[Test]
51+
public void Constructor_should_not_throw_when_update_is_empty_and_update_type_is_not_Update()
52+
{
53+
Action act = () => new UpdateRequest(UpdateType.Replacement, new BsonDocument(), new BsonDocument());
54+
55+
act.ShouldNotThrow<ArgumentException>();
56+
}
57+
}
58+
}

src/MongoDB.Driver.Core.Tests/MongoDB.Driver.Core.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
<Compile Include="Core\Operations\AsyncCursorSourceEnumerableAdapterTests.cs" />
7474
<Compile Include="Core\Operations\AsyncCursorEnumerableOneTimeAdapterTests.cs" />
7575
<Compile Include="Core\Operations\CommandOperationBaseTests.cs" />
76+
<Compile Include="Core\Operations\UpdateRequestTests.cs" />
7677
<Compile Include="Core\Operations\GeoSearchOperationTests.cs" />
7778
<Compile Include="Core\Operations\GeoNearOperationTests.cs" />
7879
<Compile Include="Core\Operations\CreateCollectionOperationTests.cs" />

src/MongoDB.Driver.Core/Core/Operations/FindOneAndUpdateOperation.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ public FindOneAndUpdateOperation(CollectionNamespace collectionNamespace, BsonDo
5858
{
5959
_filter = Ensure.IsNotNull(filter, nameof(filter));
6060
_update = Ensure.IsNotNull(update, nameof(update));
61+
if (_update.ElementCount == 0)
62+
{
63+
throw new ArgumentException("Updates must have at least 1 update operator.", nameof(update));
64+
}
6165
_returnDocument = ReturnDocument.Before;
6266
}
6367

src/MongoDB.Driver.Core/Core/Operations/UpdateRequest.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16+
using System;
1617
using MongoDB.Bson;
1718
using MongoDB.Driver.Core.Misc;
1819

@@ -43,6 +44,10 @@ public UpdateRequest(UpdateType updateType, BsonDocument filter, BsonDocument up
4344
_updateType = updateType;
4445
_filter = Ensure.IsNotNull(filter, nameof(filter));
4546
_update = Ensure.IsNotNull(update, nameof(update));
47+
if (updateType == UpdateType.Update && _update.ElementCount == 0)
48+
{
49+
throw new ArgumentException("Updates must have at least 1 update operator.", nameof(update));
50+
}
4651
}
4752

4853
// properties

src/MongoDB.Driver.Tests/UpdateDefinitionBuilderTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,16 @@ public void Combine_with_overlapping_operators_and_duplicate_elements_using_exte
160160
Assert(update, "{$set: {a: 4, b: 2}}");
161161
}
162162

163+
[Test]
164+
public void Combine_with_no_updates()
165+
{
166+
var subject = CreateSubject<BsonDocument>();
167+
168+
var update = subject.Combine();
169+
170+
Assert(update, "{ }");
171+
}
172+
163173
[Test]
164174
public void CurrentDate()
165175
{

0 commit comments

Comments
 (0)