Skip to content

CSHARP-6066: Fix NullReferenceException in FilteredMongoCollectionBase.FindOneAndUpdate when options is null#2026

Open
flibustier7seas wants to merge 2 commits into
mongodb:mainfrom
flibustier7seas:fix/filtered-collection-null-options-IsUpsert
Open

CSHARP-6066: Fix NullReferenceException in FilteredMongoCollectionBase.FindOneAndUpdate when options is null#2026
flibustier7seas wants to merge 2 commits into
mongodb:mainfrom
flibustier7seas:fix/filtered-collection-null-options-IsUpsert

Conversation

@flibustier7seas
Copy link
Copy Markdown

@flibustier7seas flibustier7seas commented Jun 3, 2026

Summary

Fixes https://jira.mongodb.org/browse/CSHARP-6066

All four FindOneAndUpdate / FindOneAndUpdateAsync overloads in FilteredMongoCollectionBase access options.IsUpsert without a null check, despite the options parameter defaulting to null. This causes a NullReferenceException when callers rely on the default value, e.g. collection.OfType<Derived>().FindOneAndUpdate(filter, update).

Fix

Replace options.IsUpsert with options?.IsUpsert ?? false in all four overloads. This is consistent with how IsUpsert defaults to false in FindOneAndUpdateOptions, and matches the null-tolerant pattern used elsewhere in the same file (e.g. MapReduce does options = options ?? new ...).

Testing

Added FindOneAndUpdate_should_not_throw_when_options_is_null unit test covering both sync and async paths.

@flibustier7seas flibustier7seas requested a review from a team as a code owner June 3, 2026 10:06
@flibustier7seas flibustier7seas requested a review from papafe June 3, 2026 10:06
@papafe papafe added the bug Fixes issues or unintended behavior. label Jun 4, 2026
@papafe papafe self-assigned this Jun 4, 2026
@papafe papafe requested a review from Copilot June 4, 2026 10:12
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes a NullReferenceException in FilteredMongoCollectionBase.FindOneAndUpdate* overloads when callers pass null (or rely on the default null) for FindOneAndUpdateOptions, by making IsUpsert access null-safe. This aligns the filtered collection wrapper behavior with the underlying driver implementations where IsUpsert defaults to false.

Changes:

  • Updated all 4 FindOneAndUpdate / FindOneAndUpdateAsync overloads to use options?.IsUpsert ?? false when adjusting the update definition.
  • Added a unit test ensuring OfType-filtered collections do not throw when options is null (sync and async entrypoints).

Reviewed changes

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

File Description
src/MongoDB.Driver/FilteredMongoCollectionBase.cs Prevents NRE by making IsUpsert access null-safe in FindOneAndUpdate* wrapper overloads.
tests/MongoDB.Driver.Tests/OfTypeMongoCollectionTests.cs Adds regression coverage to ensure FindOneAndUpdate* does not throw when options are null.

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

}
});

exception.Should().BeNull();
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.

We shouldn't check for null exception.
@papafe do you think we can add this as a rule for review agents?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

We shouldn't check for null exception.

Fixed

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

Labels

bug Fixes issues or unintended behavior.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants