alirezanet/Gridify

Null value in Custom operator when value is an anonymous object

hottabych07 opened this issue · 1 comments

Version

2.7.1

Details

Hi, I have a custom operator that runs EF.Functions.JsonContains(from PostgreSql)

public class JsonContainsOperator : IGridifyOperator
{
    public string GetOperator() => "#=";
    public Expression<OperatorParameter> OperatorHandler()
    {
        return (prop, value) => EF.Functions.JsonContains(prop, new[] { new { Id = value } });
    }
}

And when I apply the filter, I get a NULL value in the value.

Example

var test = _context.Products.ApplyFiltering("Users #= 1") .ToQueryString();
SELECT i."Id", i."Name", i.Users
FROM View_Products AS i
WHERE i."Users" @> '[{"Id":null}]'

Here is my model, I use SQL View, Users is jsonb

public class ProductsView : Entity
{
    public string Name{ get; set; }
    [Column(TypeName = "jsonb")]
    public IEnumerable<User> Users { get; set; }
}

public class UserView
{
    public int Id { get; set; }
    public string Name { get; set; }
}

What could be the reason? Thank

Steps to reproduce

To reproduce, you can repeat my code above

Hi @hottabych07

Because in this case, you had a combination of anonymous object and array there was no proper way to detect what is the type of the value. this problem is solved in v2.7.2.

I also should add if you are only dealing with for example integer types, you should add your conversion to the operator but it is not necessary usually. e.g

public class JsonContainsOperator : IGridifyOperator
{
   public string GetOperator() => "#=";
   public Expression<OperatorParameter> OperatorHandler()
   {
      return (prop, value) => EF.Functions.JsonContains(prop, new[] { new { Id = int.Parse(value.ToString()!) } });
   }
}

Let me know if you faced another problem because this is a new edge case that I didn't test it before.
Thank you for the feedback.