Skip to content

Enums with custom value converter are converted to ints in $filter clause #1566

@Xriuk

Description

@Xriuk

This part:

// Convert to integers unless Enum type is required
if ((TypeHelper.IsEnum(leftUnderlyingType) || TypeHelper.IsEnum(rightUnderlyingType)) && binaryOperator != BinaryOperatorKind.Has)
{
Type enumType = TypeHelper.IsEnum(leftUnderlyingType) ? leftUnderlyingType : rightUnderlyingType;
Type enumUnderlyingType = Enum.GetUnderlyingType(enumType);
left = ConvertToEnumUnderlyingType(left, enumType, enumUnderlyingType);
right = ConvertToEnumUnderlyingType(right, enumType, enumUnderlyingType);
}

Arbitrarily converts enums to integers, which may or might not be the desired effect.

I have a custom ValueConverter applied to an enum in EF Core, which causes them to be saved as a hash of the enum name instead of their enum value. So Type eq 'MyType' currently becomes (int)$it.Type == 1 which does not make sense for my case because it should be $it.Type == TypeEnum.MyType (which later will be translated by EF Core into something like (int)$it.Type == 123456789).

This makes even less sense considering that enums themselves support also comparison operators like >, >=, <, <=, ... so why this decision was even made in the first place?

I think enum handling or conversions should be left to the underlying IQueryable QueryProvider, EF Core in this case, OData expressions should be translated as they are.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions