CSHARP-6066: Fix NullReferenceException in FilteredMongoCollectionBase.FindOneAndUpdate when options is null#2026
Conversation
…e.FindOneAndUpdate when options is null
There was a problem hiding this comment.
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/FindOneAndUpdateAsyncoverloads to useoptions?.IsUpsert ?? falsewhen adjusting the update definition. - Added a unit test ensuring
OfType-filtered collections do not throw whenoptionsisnull(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(); |
There was a problem hiding this comment.
We shouldn't check for null exception.
@papafe do you think we can add this as a rule for review agents?
There was a problem hiding this comment.
We shouldn't check for null exception.
Fixed
Summary
Fixes https://jira.mongodb.org/browse/CSHARP-6066
All four
FindOneAndUpdate/FindOneAndUpdateAsyncoverloads inFilteredMongoCollectionBaseaccessoptions.IsUpsertwithout a null check, despite the options parameter defaulting to null. This causes aNullReferenceExceptionwhen callers rely on the default value, e.g.collection.OfType<Derived>().FindOneAndUpdate(filter, update).Fix
Replace
options.IsUpsertwithoptions?.IsUpsert ?? falsein all four overloads. This is consistent with howIsUpsertdefaults to false inFindOneAndUpdateOptions, 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_nullunit test covering both sync and async paths.