Skip to content

CSHARP-5658: Convert value type to match field serializer type in Equals filter translation#1981

Open
kyra-rk wants to merge 5 commits into
mongodb:mainfrom
kyra-rk:csharp-5658
Open

CSHARP-5658: Convert value type to match field serializer type in Equals filter translation#1981
kyra-rk wants to merge 5 commits into
mongodb:mainfrom
kyra-rk:csharp-5658

Conversation

@kyra-rk
Copy link
Copy Markdown
Contributor

@kyra-rk kyra-rk commented May 1, 2026

No description provided.

@kyra-rk kyra-rk requested a review from papafe May 1, 2026 20:46
@kyra-rk
Copy link
Copy Markdown
Contributor Author

kyra-rk commented May 1, 2026

Other filter translators have the same GetConstantValue<object> and SerializeValue pattern and could hit the same InvalidCastException for mismatched types:

  • CompareComparisonExpressionToFilterTranslator
  • BitMaskComparisonExpressionToFilterTranslator
  • HasFlagMethodToFilterTranslator
  • ContainsMethodToFilterTranslator
  • GetItemMethodToFilterFieldTranslator

If this fix looks good, it might be worth extracting the conversion into a shared helper and applying it to these as well.

@kyra-rk kyra-rk marked this pull request as ready for review May 1, 2026 20:50
@kyra-rk kyra-rk requested a review from a team as a code owner May 1, 2026 20:50
Copilot AI review requested due to automatic review settings May 1, 2026 20:50
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

Adds support for translating Equals filters when the constant value’s CLR type differs from the field serializer’s value type (notably numeric/nullable numeric scenarios), and introduces integration tests covering the regression.

Changes:

  • Update EqualsMethodToFilterTranslator to coerce constant values to the field serializer’s ValueType before serialization.
  • Add LINQ integration tests for Nullable<int> fields compared against ulong, int, and null, including a no-match case.

Reviewed changes

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

File Description
src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToFilterTranslators/MethodTranslators/EqualsMethodToFilterTranslator.cs Adds a pre-serialization constant conversion step based on the field serializer value type.
tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Translators/ExpressionToFilterTranslators/MethodTranslators/EqualsMethodToFilterTranslatorTests.cs New integration tests validating translation/output for Equals with numeric and null constants.

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

@kyra-rk kyra-rk added the bug Fixes issues or unintended behavior. label May 1, 2026
@kyra-rk kyra-rk marked this pull request as draft May 1, 2026 21:03
@papafe papafe requested a review from Copilot May 8, 2026 13:34
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

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

Comment on lines +74 to +75
var exception = Record.Exception(() => filter.Render(__args));
exception.Should().NotBeNull();
@papafe papafe requested a review from adelinowona May 8, 2026 13:45
@papafe papafe marked this pull request as ready for review May 8, 2026 13:45
@papafe
Copy link
Copy Markdown
Contributor

papafe commented May 19, 2026

Hey @adelinowona, I took over Kyra for this ticket. What do you think of the approach?

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.

3 participants