Skip to content

Cannot use "contains" on nullable enums #543

@tuxinal

Description

@tuxinal

I have a scenario in my codebase where I need to check if a nullable enum property is in a list determined from an external source

Code sample
/*
 Dotnet version: 8.0.412
 Redis Version: 8.0.3
 Redis OM version: 1.0.1
*/
using Redis.OM;
using Redis.OM.Modeling;

var provider = new RedisConnectionProvider("redis://localhost:6379");
provider.Connection.CreateIndex(typeof(BillCache));
var billCollection = provider.RedisCollection<BillCache>();

billCollection.Insert(new BillCache { Id = 1, ItemType = ItemType.Item0 });
billCollection.Insert(new BillCache { Id = 2, ItemType = ItemType.Item1 });
billCollection.Insert(new BillCache { Id = 3, ItemType = ItemType.Item2 });
billCollection.Insert(new BillCache { Id = 4, ItemType = ItemType.Item3 });
billCollection.Insert(new BillCache { Id = 5, ItemType = null });

ItemType[] matches = [ItemType.Item0, ItemType.Item1, ItemType.Item2];

var query = billCollection.Where(x =>
    matches.Contains((ItemType)x.ItemType!)
);
Console.WriteLine($"Redis query: {query.ToQueryString()}");
var response = query.ToList();

[Document(StorageType = StorageType.Hash, IndexName = "bill-idx")]
class BillCache
{
    [Indexed]
    [RedisIdField]
    public int Id { get; set; }
    [Indexed]
    public ItemType? ItemType { get; set; }
}

enum ItemType
{
    Item0 = 0,
    Item1 = 1,
    Item2 = 2,
    Item3 = 3,
}

The query string output in this scenario is "FT.SEARCH" "bill-idx" "(Item0|Item1|Item2:{\@ItemType})" "LIMIT" "0" "100" which results in a syntax error.
The expected output would be "FT.SEARCH" "bill-idx" "(@ItemType:[0 0]|@ItemType:[1 1]|@ItemType:[2 2])" "DIALECT" "2" "LIMIT" "0" "100"
it seems that 1. the left and right side of the condition are flipped and 2. the enums aren't being parsed as integers

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions