Skip to content

Commit 6e6179f

Browse files
authored
CSHARP-4801: Accessing ChangeStreamDocument.FullDocumentBeforeChange throw if pre-image was already expired (#1183)
1 parent 88a63ea commit 6e6179f

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/MongoDB.Driver.Core/ChangeStreamDocument.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@ public TDocument FullDocumentBeforeChange
166166
// if TDocument is BsonDocument avoid deserializing it again to prevent possible duplicate element name errors
167167
if (typeof(TDocument) == typeof(BsonDocument) && BackingDocument.TryGetValue("fullDocumentBeforeChange", out var fullDocumentBeforeChange))
168168
{
169+
if (fullDocumentBeforeChange.IsBsonNull)
170+
{
171+
return default;
172+
}
173+
169174
return (TDocument)(object)fullDocumentBeforeChange.AsBsonDocument;
170175
}
171176
else

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,6 @@ public void FullDocument_should_allow_duplicate_elements()
326326
[Fact]
327327
public void FullDocument_should_return_null_when_not_present()
328328
{
329-
var value = new BsonDocument("x", 1234);
330329
var backingDocument = new BsonDocument { { "other", 1 } };
331330
var subject = CreateSubject(backingDocument: backingDocument);
332331

@@ -335,6 +334,32 @@ public void FullDocument_should_return_null_when_not_present()
335334
result.Should().BeNull();
336335
}
337336

337+
[Theory]
338+
[InlineData("{ other : 1 }")]
339+
[InlineData("{ other : 1, fullDocumentBeforeChange : null }")]
340+
public void FullDocumentBeforeChange_should_return_null_when_not_present(string changeDocument)
341+
{
342+
var backingDocument = BsonDocument.Parse(changeDocument);
343+
var subject = CreateSubject(backingDocument: backingDocument);
344+
345+
var result = subject.FullDocumentBeforeChange;
346+
347+
result.Should().BeNull();
348+
}
349+
350+
[Theory]
351+
[InlineData("{ other : 1, fullDocumentBeforeChange : { } }")]
352+
[InlineData("{ other : 1, fullDocumentBeforeChange : { a : 1 } }")]
353+
public void FullDocumentBeforeChange_should_return_document_when_present(string changeDocument)
354+
{
355+
var backingDocument = BsonDocument.Parse(changeDocument);
356+
var subject = CreateSubject(backingDocument: backingDocument);
357+
358+
var result = subject.FullDocumentBeforeChange;
359+
360+
result.Should().BeOfType<BsonDocument>();
361+
}
362+
338363
[Fact]
339364
public void OperationDescription_should_return_expected_result()
340365
{

0 commit comments

Comments
 (0)