Skip to content

CSHARP-5998: RawBsonDocument refactoring/removal - #2085

Open
sanych-sun wants to merge 2 commits into
mongodb:mainfrom
sanych-sun:CSHARP-5998
Open

CSHARP-5998: RawBsonDocument refactoring/removal#2085
sanych-sun wants to merge 2 commits into
mongodb:mainfrom
sanych-sun:CSHARP-5998

Conversation

@sanych-sun

Copy link
Copy Markdown
Member

Part 2 of the RawBsonDocument deprecation work: remove RawBsonDocument from ChangeStreams.

Copilot AI review requested due to automatic review settings July 28, 2026 21:44
@sanych-sun
sanych-sun requested a review from a team as a code owner July 28, 2026 21:44
@sanych-sun
sanych-sun requested review from BorisDog and papafe and removed request for papafe July 28, 2026 21:44
@sanych-sun sanych-sun added the improvement Optimizations or refactoring (no new features or fixes). label Jul 28, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Removes RawBsonDocument usage from the Change Streams core operation/cursor path by having the aggregate operation return typed results (TResult) directly, and updates the associated unit tests to match.

Changes:

  • Refactor ChangeStreamOperation<TResult> to create/execute AggregateOperation<TResult> using _resultSerializer instead of RawBsonDocumentSerializer.
  • Refactor ChangeStreamCursor<TDocument> to wrap IAsyncCursor<TDocument> directly (removing raw BSON deserialization/disposal and the per-document resume token field).
  • Update Change Streams operation/cursor tests to align with the new typed cursor/serializer behavior.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
src/MongoDB.Driver/Core/Operations/ChangeStreamOperation.cs Switches change stream aggregation/resume to operate on TResult cursors and uses the configured result serializer.
src/MongoDB.Driver/Core/Operations/ChangeStreamCursor.cs Updates cursor implementation to consume IAsyncCursor<TDocument> directly and simplifies batch processing/token tracking.
tests/MongoDB.Driver.Tests/Core/Operations/ChangeStreamOperationTests.cs Adjusts expectations/reflector types to match the new aggregate result serializer and generic operation type.
tests/MongoDB.Driver.Tests/Core/Operations/ChangeStreamCursorTests.cs Partially updates tests for the new cursor constructor/signature and removes raw-deserialization-based assertions.
Comments suppressed due to low confidence (4)

tests/MongoDB.Driver.Tests/Core/Operations/ChangeStreamCursorTests.cs:137

  • This test still constructs IAsyncCursor<RawBsonDocument>, but the updated ChangeStreamCursor<BsonDocument> constructor requires IAsyncCursor<BsonDocument>.
            var cursor = new Mock<IAsyncCursor<RawBsonDocument>>().Object;

tests/MongoDB.Driver.Tests/Core/Operations/ChangeStreamCursorTests.cs:152

  • This test still constructs IAsyncCursor<RawBsonDocument>, but the updated ChangeStreamCursor<BsonDocument> constructor requires IAsyncCursor<BsonDocument>.
            var cursor = new Mock<IAsyncCursor<RawBsonDocument>>().Object;

tests/MongoDB.Driver.Tests/Core/Operations/ChangeStreamCursorTests.cs:167

  • This test still constructs IAsyncCursor<RawBsonDocument>, but the updated ChangeStreamCursor<BsonDocument> constructor requires IAsyncCursor<BsonDocument>.
            var cursor = new Mock<IAsyncCursor<RawBsonDocument>>().Object;

tests/MongoDB.Driver.Tests/Core/Operations/ChangeStreamCursorTests.cs:496

  • CreateSubject now takes IAsyncCursor<BsonDocument>, but several tests in this file still create/pass Mock<IAsyncCursor<RawBsonDocument>> (e.g., in the Dispose/DisposeAsync tests). Those call sites will not compile after the cursor type change and should be updated to IAsyncCursor<BsonDocument>.
        private ChangeStreamCursor<BsonDocument> CreateSubject(
            IAsyncCursor<BsonDocument> cursor = null,
            IReadBinding binding = null,
            ICoreSessionHandle session = null,
            IChangeStreamOperation<BsonDocument> changeStreamOperation = null,

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@@ -86,7 +81,6 @@ public void ChangeStreamOperation_should_have_expected_change_stream_operation_o
public void constructor_should_initialize_instance()
{
var cursor = new Mock<IAsyncCursor<RawBsonDocument>>().Object;
Comment on lines +433 to 436
var firstDocument = BsonDocument.Parse("{ _id : { resumeToken : 1 }, operationType : 'insert', ns : { db : 'db', coll : 'coll' }, documentKey : { _id : 1 }, fullDocument : { _id : 1 } }");
var firstBatch = new[] { ToRawDocument(firstDocument) };
mockCursor.Setup(c => c.MoveNext(It.IsAny<CancellationToken>())).Returns(true);
mockCursor.SetupGet(c => c.Current).Returns(firstBatch);
Comment on lines 198 to 202
if (hasMore)
{
try
{
_current = DeserializeDocuments(_cursor.Current);
}
finally
{
foreach (var rawDocument in _cursor.Current)
{
rawDocument.Dispose();
}
}
_current = _cursor.Current;
_postBatchResumeToken = ((ICursorBatchInfo)_cursor).PostBatchResumeToken;
}
Comment on lines 564 to 565
public static ChangeStreamCursor<TDocument>.ResumeValues GetResumeValues<TDocument>(this IChangeStreamCursor<TDocument> cursor) =>
(ChangeStreamCursor<TDocument>.ResumeValues)Reflector.Invoke(cursor, nameof(GetResumeValues));

@BorisDog BorisDog left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just need to double check that PostBatchResumeToken is always present.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

improvement Optimizations or refactoring (no new features or fixes).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants