Skip to content

Commit e7a00db

Browse files
committed
CSHARP-1265: Empty update documents should throw an exception.
1 parent 6898d4d commit e7a00db

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,16 @@ public async Task ExecuteAsync_with_more_inserts_than_maxBatchCount()
393393
list.Should().HaveCount(4);
394394
}
395395

396+
[Test]
397+
public void ExecuteAsync_with_an_empty_update_document_should_throw()
398+
{
399+
var requests = new[] { new UpdateRequest(UpdateType.Update, BsonDocument.Parse("{x: 1}"), new BsonDocument()) };
400+
var subject = new BulkMixedWriteOperation(_collectionNamespace, requests, _messageEncoderSettings);
401+
402+
Func<Task> act = () => ExecuteOperationAsync(subject);
403+
act.ShouldThrow<BsonSerializationException>();
404+
}
405+
396406
[Test]
397407
[RequiresServer("EnsureTestData")]
398408
public async Task ExecuteAsync_with_one_update_against_a_matching_document()

src/MongoDB.Driver.Core.Tests/Core/WireProtocol/Messages/Encoders/BinaryEncoders/UpdateMessageBinaryEncoderTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using FluentAssertions;
1919
using MongoDB.Bson;
2020
using MongoDB.Bson.IO;
21+
using MongoDB.Driver.Core.Operations.ElementNameValidators;
2122
using MongoDB.Driver.Core.WireProtocol.Messages;
2223
using MongoDB.Driver.Core.WireProtocol.Messages.Encoders.BinaryEncoders;
2324
using NUnit.Framework;
@@ -152,5 +153,17 @@ public void WriteMessage_should_write_a_message()
152153
bytes.Should().Equal(__testMessageBytes);
153154
}
154155
}
156+
157+
[Test]
158+
public void WriteMessage_should_throw_if_the_update_message_is_empty_when_using_the_UpdateElementNameValidator()
159+
{
160+
var message = new UpdateMessage(__requestId, __collectionNamespace, __query, new BsonDocument(), UpdateElementNameValidator.Instance, false, false);
161+
using (var stream = new MemoryStream())
162+
{
163+
var subject = new UpdateMessageBinaryEncoder(stream, __messageEncoderSettings);
164+
Action act = () => subject.WriteMessage(message);
165+
act.ShouldThrow<BsonSerializationException>();
166+
}
167+
}
155168
}
156169
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,13 @@ private void SerializeUpdate(BsonBinaryWriter bsonWriter, BsonDocument update, U
122122
bsonWriter.PushElementNameValidator(ElementNameValidatorFactory.ForUpdateType(updateType));
123123
try
124124
{
125+
var position = bsonWriter.BaseStream.Position;
125126
var context = BsonSerializationContext.CreateRoot(bsonWriter);
126127
BsonDocumentSerializer.Instance.Serialize(context, update);
128+
if (updateType == UpdateType.Update && bsonWriter.BaseStream.Position == position + 8)
129+
{
130+
throw new BsonSerializationException("Update documents cannot be empty.");
131+
}
127132
}
128133
finally
129134
{

src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/BinaryEncoders/UpdateMessageBinaryEncoder.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
using MongoDB.Bson.Serialization;
2525
using MongoDB.Bson.Serialization.Serializers;
2626
using MongoDB.Driver.Core.Misc;
27+
using MongoDB.Driver.Core.Operations.ElementNameValidators;
2728

2829
namespace MongoDB.Driver.Core.WireProtocol.Messages.Encoders.BinaryEncoders
2930
{
@@ -126,8 +127,13 @@ private void WriteUpdate(BsonBinaryWriter binaryWriter, BsonDocument update, IEl
126127
binaryWriter.PushElementNameValidator(updateValidator);
127128
try
128129
{
130+
var position = binaryWriter.BaseStream.Position;
129131
var context = BsonSerializationContext.CreateRoot(binaryWriter);
130132
BsonDocumentSerializer.Instance.Serialize(context, update);
133+
if (updateValidator is UpdateElementNameValidator && binaryWriter.BaseStream.Position == position + 5)
134+
{
135+
throw new BsonSerializationException("Update documents cannot be empty.");
136+
}
131137
}
132138
finally
133139
{

0 commit comments

Comments
 (0)