Fixes #1581: Nullable enum with 'in' operator throws when collection contains starts with a null value - #1604
Open
anasik wants to merge 4 commits into
Open
Fixes #1581: Nullable enum with 'in' operator throws when collection contains starts with a null value#1604anasik wants to merge 4 commits into
anasik wants to merge 4 commits into
Conversation
…alue but with the null in the first index of the tuple
… a null in the first position of the list.
Contributor
Author
|
@WanjohiSammy I'll cherry pick this onto main once this is merged |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1581.
$filter=SomeEnum in (null, 'Value')on a nullable enum property threw:System.ArgumentException: The value "Value" is not of type "System.Nullable`1[...]"
and cannot be used in this generic collection.
...whenever a
nullappeared before a real enum value in thein (...)list. Thereverse order (
in ('Value', null)) already worked, which is why this slippedthrough existing coverage.
Root cause
QueryBinder.BindCollectionConstantNodedetermines the CLR item type for the wholein (...)list from the first element only.RetrieveClrTypeForConstantonlyunwrapped
Nullable<TEnum>→TEnumwhen the value it was inspecting was non-null.So when the first item was
null, the CLR type stayedNullable<TEnum>for the restof the loop,
constantType.IsEnumcame backfalse(always false forNullable<T>),and the next real item's
ODataEnumValuegot added directly into aList<TEnum?>instead of being converted first.
Fix
QueryBinder.csonly — hoisted theNullable<TEnum>unwrap into its own precedingcheck that runs regardless of whether the value being inspected is null; the
value-conversion step still only runs when non-null. No other logic touched.
Tests
EnumInExpression_NullableEnum_WithNullValueFirstinFilterBinderTests.cs— the null-first ordering not covered by the existing
EnumInExpression_NullableEnum_WithNullValuetest.MaritalStatusproperty added to theEnumstest model/controller,with a test filtering
MaritalStatus in (null,'Married')through the real pipeline.Both reproduce the exact reported exception when the fix is reverted, and pass with it.