Skip to content

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
OData:dev-10.xfrom
anasik:dev-10.x
Open

Fixes #1581: Nullable enum with 'in' operator throws when collection contains starts with a null value#1604
anasik wants to merge 4 commits into
OData:dev-10.xfrom
anasik:dev-10.x

Conversation

@anasik

@anasik anasik commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

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 null appeared before a real enum value in the in (...) list. The
reverse order (in ('Value', null)) already worked, which is why this slipped
through existing coverage.

Root cause

QueryBinder.BindCollectionConstantNode determines the CLR item type for the whole
in (...) list from the first element only. RetrieveClrTypeForConstant only
unwrapped Nullable<TEnum>TEnum when the value it was inspecting was non-null.
So when the first item was null, the CLR type stayed Nullable<TEnum> for the rest
of the loop, constantType.IsEnum came back false (always false for Nullable<T>),
and the next real item's ODataEnumValue got added directly into a List<TEnum?>
instead of being converted first.

Fix

QueryBinder.cs only — hoisted the Nullable<TEnum> unwrap into its own preceding
check 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

  • Unit: EnumInExpression_NullableEnum_WithNullValueFirst in FilterBinderTests.cs
    — the null-first ordering not covered by the existing
    EnumInExpression_NullableEnum_WithNullValue test.
  • E2E: nullable MaritalStatus property added to the Enums test 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.

@anasik

anasik commented Aug 1, 2026

Copy link
Copy Markdown
Contributor Author

@WanjohiSammy I'll cherry pick this onto main once this is merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant