alirezanet/Gridify

Adding a filter on a row which contains null data causes a null reference exception

riccardorestagno opened this issue · 2 comments

Version

2.8.4

Details

When adding a filter on a string which contains null data, GridifyQueryable throws a null reference exception. Here is an example of the crash:

public PersonQueryResult<PersonUIModel> RunGetPerson(string Id, PagingRequest pagingRequest)
    {
        IEnumerable<PersonUIModel> persons= _context.Person
           .Where(ct => ct.Id== Id)
           .AsEnumerable()
           .Select(ct => new PersonUIModel
           {
               Id = ct.Id,
               FirstName = ct.FirstName,
               LastName = ct.LastName,
           });

        QueryablePaging<PersonUIModel> results = persons.AsQueryable().ApplyPageFiltering(pagingRequest, false);

        return new PersonQueryResult<PersonUIModel>()
        {
            TotalCount = results.Count,
            Transactions = results.Query.ToList()
        };
    }
    public static QueryablePaging<T> ApplyPageFiltering<T>(this IQueryable<T> query, PagingRequest pagingRequest, bool ignorePaging)
    {
        GridifyMapper<T> gridifyMapper = new GridifyMapper<T>(config => config.IgnoreNotMappedFields = true).GenerateMappings() as GridifyMapper<T>;

        return query.GridifyQueryable(PagingRequestConverter.ToQueryFilter(pagingRequest, ignorePaging), gridifyMapper);
    }

image

Workaround: Add null coalescing operator in query to default to string.Empty

FirstName = ct.FirstName ?? string.Empty,

Steps to reproduce

  • Add row in database which contains null string value
  • Add paging request filter to filter on said value (Ex: GridifyQuery.Filter = "FirstName=*ABC)
  • Result: Object reference not set to instance of object
  • Expected: Value not returned as per filter

Hi @riccardorestagno,

Can you please check this discussion, I think you can find another workaround to solve it for now.
This is the default behavior since your filter query gets converted directly to a linq expression, so if you don't add the null check you get the exception.

FYI I'm planning to add a new feature to the next versions for adding the null checks automatically.

Thanks for the info! My workaround works as well but I'll consider adding null checks in the expressions directly instead of adding a null coalescing operator in all my query properties.