Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with Nullable Guid Parsing #452

Open
ThomasKolarik opened this issue Jun 18, 2024 · 1 comment
Open

Issue with Nullable Guid Parsing #452

ThomasKolarik opened this issue Jun 18, 2024 · 1 comment

Comments

@ThomasKolarik
Copy link

ThomasKolarik commented Jun 18, 2024

Hello,

In version 0.4.2 there was a fix to parsing nullable enums (#315). However, I am running into a similar issue with nullable guids.

Here is a code sample to demonstrate what is going on, on my end.

    [Document(StorageType = StorageType.Json)]
    public class ItemEntry
    {
        [RedisIdField]
        public Guid Id { get; set; }

        public string Data { get; set; }
    }
    
    private readonly IRedisCollection<ItemEntry> items;
    
    public async Task<ItemEntry> FetchItemAsync(Guid? itemId)
    {
        var items = await this.items.Where(a => a.Id == itemId).ToListAsync();
        
        return items.FirstOrDefault();
    }

Which when called will error out with the follow exception,

  System.ArgumentException:
     at System.Enum.ValidateRuntimeType (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
     at System.Enum.TryParse (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
     at System.Enum.TryParse (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
     at Redis.OM.Common.ExpressionTranslator.TranslateBinaryExpression (Redis.OM, Version=0.6.0.0, Culture=neutral, PublicKeyToken=null)
     at Redis.OM.Common.ExpressionTranslator.BuildQueryFromExpression (Redis.OM, Version=0.6.0.0, Culture=neutral, PublicKeyToken=null)
     at Redis.OM.Searching.RedisCollectionEnumerator`1..ctor (Redis.OM, Version=0.6.0.0, Culture=neutral, PublicKeyToken=null)
     at Redis.OM.Searching.RedisCollection`1.GetAsyncEnumerator (Redis.OM, Version=0.6.0.0, Culture=neutral, PublicKeyToken=null)
     at Redis.OM.Searching.RedisCollection`1+<ToListAsync>d__40.MoveNext (Redis.OM, Version=0.6.0.0, Culture=neutral, PublicKeyToken=null))

As you can see in the error log, I am currently using version 0.6.0.

@slorello89
Copy link
Member

Hi @ThomasKolarik,

This is a bit of an odd error (it looks like it's encountering some of the enum parsing logic which is unexpected). However this is actually an erroneous query. For several reasons:

  1. The Id field is not indexed (you need to mark it as indexed in order to be able to do the LINQ style queries on it)
  2. A nullable GUID is not a valid field to do a search on even if it were indexed. RediSearch has no concept (at the moment) of a nulled out field, hence it will not be able to interpret it.
  3. You are using the GUID as your ID field, so you can just do an Id Search.

e.g.

    [Document(StorageType = StorageType.Json)]
    public class ItemEntry
    {
        [RedisIdField]
        [Indexed]
        public Guid Id { get; set; }

        public string Data { get; set; }
    }
    
    private readonly IRedisCollection<ItemEntry> items;
    
    public async Task<ItemEntry> FetchItemAsync(Guid? itemId)
    {
        if(itemId.HasValue)
            return await this.items.FirstOrDefaultAsync(a => a.Id == itemId.Value);
        else
            return default;
        // OR
        return await this.items.FindByIdAsync(itemId.Value.ToString());
    }

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

No branches or pull requests

2 participants