Skip to content

CSHARP-5964: Wrap Mql.Hash result serializer in BsonValueCSharpNullSerializer#1968

Open
kyra-rk wants to merge 8 commits intomongodb:mainfrom
kyra-rk:csharp-5964
Open

CSHARP-5964: Wrap Mql.Hash result serializer in BsonValueCSharpNullSerializer#1968
kyra-rk wants to merge 8 commits intomongodb:mainfrom
kyra-rk:csharp-5964

Conversation

@kyra-rk
Copy link
Copy Markdown
Contributor

@kyra-rk kyra-rk commented Apr 23, 2026

No description provided.

@kyra-rk kyra-rk added the improvement Optimizations or refactoring (no new features or fixes). label Apr 23, 2026
return classMap;
}

private static IBsonSerializer CoerceSourceSerializerToMemberSerializer(BsonMemberMap memberMap, IBsonSerializer sourceSerializer)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

CoerceSourceSerializerToMemberSerializer is only called from CreateNewExpressionSerializer, which seems to be called by the serializer finder visitor when it comes across an unrecognized type, eg. when a LINQ projection results build a new type during runtime eg. .Select(x => new { NewField = Mql.SomeCommand(x.OriginalField) }). Does that sound right?

@kyra-rk
Copy link
Copy Markdown
Contributor Author

kyra-rk commented Apr 24, 2026

General context for reviewer: the default class-map registration wraps BsonBinaryData with a BsonValueCSharpNullSerializer<> (here), but the new-expression path doesn't: CoerceSourceSerializerToMemberSerializer deliberately returns the source expression's serializer (not the class-map's), which is what loses the csharp null wrapping. The fix I added is to wrap any BsonValue with the BsonValueCSharpNullSerializer. It honestly feels a bit hacky, so if you have thoughts on how to do this better, please let me know!

My fix is to wrap BsonBinaryData with BsonValueCSharpNullSerializer<> in DeduceHashMethodSerializers so Mql.Hash results match the class-map shape.

@kyra-rk kyra-rk changed the title CSHARP-5964: Investigate a way to read into BsonBinaryData property if it's missed in the stored document CSHARP-5964: Wrap Mql.Hash result serializer in BsonValueCSharpNullSerializer Apr 27, 2026
@kyra-rk kyra-rk requested a review from sanych-sun April 27, 2026 18:23
@kyra-rk kyra-rk marked this pull request as ready for review April 27, 2026 20:27
@kyra-rk kyra-rk requested a review from a team as a code owner April 27, 2026 20:27
Copilot AI review requested due to automatic review settings April 27, 2026 20:27
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

Updates LINQ3 MQL Hash serializer deduction so Mql.Hash(...) results can safely round-trip to C# null (e.g., when the $hash operator yields BSON null due to missing/null input), and adjusts/extends tests to cover the behavior.

Changes:

  • Wrap the Mql.Hash result serializer in BsonValueCSharpNullSerializer<BsonBinaryData> during serializer deduction.
  • Update LINQ3 translator/serializer-finder unit tests to expect the wrapped serializer type.
  • Extend integration coverage to include a document with missing Data and validate projections (including named-type projections) return null hashes.

Reviewed changes

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

Show a summary per file
File Description
tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Translators/ExpressionToAggregationExpressionTranslators/MethodTranslators/HashMethodToAggregationExpressionTranslatorTests.cs Updates expected serializer type for Mql.Hash translations to the C#-null-capable wrapper.
tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/SerializerFinders/MqlTests.cs Updates serializer-finder expectation for Mql.Hash to the wrapper serializer type.
tests/MongoDB.Driver.Tests/Linq/Integration/MqlHashTests.cs Adds missing-Data fixture doc and validates projected hash values include null (including named-type projection).
src/MongoDB.Driver/Linq/Linq3Implementation/SerializerFinders/SerializerFinderVisitMethodCall.cs Wraps BsonBinaryDataSerializer with BsonValueCSharpNullSerializer for Mql.Hash deduction.
src/MongoDB.Driver/Linq/Linq3Implementation/SerializerFinders/SerializerFinderNewExpressionSerializerCreator.cs Adds new using directives (currently unused).

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

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
new() { Id = 1, Data = new BsonBinaryData([0x01, 0x02]) },
new() { Id = 1, Data = new BsonBinaryData([0x01, 0x02])},
new() { Id = 2, Data = new BsonBinaryData(Guid.Parse("E4A10FB8-7A83-494C-9710-29BBFFB1C262"), GuidRepresentation.Standard) },
new() { Id = 4 },
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.

Minor: It could be 3, so no one will ask "is there any reasons to skip values here"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Oooh, yes agreed. I'll change it!

}

[Fact]
public async Task MqlHash_in_projection_builder_named_type()
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.

Why do we need this new test?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

While investigating the ticket I added this test in because I was curious if giving the projection a named type would affect the behaviour, eg. new ContainerHash { Hash = Mql.Hash(...) }. It doesn't, so I'm happy to just remove it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ok I think it's actually more confusing than it is helpful, so will remove it!

@kyra-rk kyra-rk requested a review from sanych-sun April 29, 2026 18:31
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