Skip to content

Commit 7b9f51e

Browse files
authored
CSHARP-5119: FullDocument of the ChangeStreamDocument<TDocument> should not throw on BsonNull (#1341)
1 parent 25c24f6 commit 7b9f51e

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/MongoDB.Driver.Core/ChangeStreamDocument.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ public TDocument FullDocument
144144
// if TDocument is BsonDocument avoid deserializing it again to prevent possible duplicate element name errors
145145
if (typeof(TDocument) == typeof(BsonDocument) && BackingDocument.TryGetValue("fullDocument", out var fullDocument))
146146
{
147+
if (fullDocument.IsBsonNull)
148+
{
149+
return default;
150+
}
151+
147152
return (TDocument)(object)fullDocument.AsBsonDocument;
148153
}
149154
else

tests/MongoDB.Driver.Core.Tests/ChangeStreamDocumentTests.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,10 +323,12 @@ public void FullDocument_should_allow_duplicate_elements()
323323
secondElement.Value.Should().Be(2);
324324
}
325325

326-
[Fact]
327-
public void FullDocument_should_return_null_when_not_present()
326+
[Theory]
327+
[InlineData("{ other : 1 }")]
328+
[InlineData("{ other : 1, fullDocument : null }")]
329+
public void FullDocument_should_return_null_when_not_present(string changeDocument)
328330
{
329-
var backingDocument = new BsonDocument { { "other", 1 } };
331+
var backingDocument = BsonDocument.Parse(changeDocument);
330332
var subject = CreateSubject(backingDocument: backingDocument);
331333

332334
var result = subject.FullDocument;

0 commit comments

Comments
 (0)