Skip to content

CSHARP-6043: NullReferenceException in IEnumerableSerializerBase.Serialize when projecting nullable array with conditional Select#2012

Merged
papafe merged 2 commits into
mongodb:mainfrom
papafe:charp6043
May 27, 2026
Merged

CSHARP-6043: NullReferenceException in IEnumerableSerializerBase.Serialize when projecting nullable array with conditional Select#2012
papafe merged 2 commits into
mongodb:mainfrom
papafe:charp6043

Conversation

@papafe

@papafe papafe commented May 26, 2026

Copy link
Copy Markdown
Contributor

The NRE happened during LINQ3 query translation when the translator serialized the null constant from the ternary's true branch through a IEnumerableSerializerBase.Serialize that didn't expect null.

The BSON-layer EnumerableSerializerBase already handles null but the LINQ path uses its own dedicated IEnumerableSerializerBase and that one was missing the null guards. So the fix is to bring the LINQ path serializer in par with the BSON layer one.

…alize when projecting nullable array with conditional Select
@papafe papafe added the bug Fixes issues or unintended behavior. label May 26, 2026
@papafe papafe marked this pull request as ready for review May 26, 2026 17:12
@papafe papafe requested a review from a team as a code owner May 26, 2026 17:12
@papafe papafe requested review from ajcvickers and Copilot May 26, 2026 17:12

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

Fixes null handling in Linq3 enumerable serialization to prevent NullReferenceException when serializing projections that can evaluate to null (e.g., nullable arrays under conditional Select), and adds targeted regression tests.

Changes:

  • Handle BSON null in IEnumerableSerializerBase.Deserialize by consuming null and returning default.
  • Handle null values in IEnumerableSerializerBase.Serialize by writing BSON null instead of iterating.
  • Add unit tests verifying null serialize/deserialize behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Serializers/IEnumerableSerializerBaseTests.cs Adds regression tests ensuring null enumerables round-trip as BSON null.
src/MongoDB.Driver/Linq/Linq3Implementation/Serializers/IEnumerableSerializerBase.cs Adds explicit BSON null handling for serialize/deserialize paths.

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

Comment on lines 70 to +77
public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, TEnumerable value)
{
var writer = context.Writer;
if (value == null)
{
writer.WriteNull();
return;
}
@BorisDog BorisDog requested a review from sanych-sun May 26, 2026 19:52

@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 + question about testing.

var result = serializer.Deserialize(context, new BsonDeserializationArgs { NominalType = typeof(IEnumerable<int>) });

result.Should().BeNull();
}

@BorisDog BorisDog May 26, 2026

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.

Would be good to have some limited end-to-end testing. By "e2e" here I mean serializing and deserializing a real POCO, using a real expression (without DB).
But not sure where we place such tests now, any ideas @sanych-sun?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It probably should be a Theory with bytes on one side and expected value on the another. We might need such for all serializers.

@sanych-sun sanych-sun left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@papafe papafe merged commit 08fe6b0 into mongodb:main May 27, 2026
35 checks passed
@papafe papafe deleted the charp6043 branch May 27, 2026 17:09
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