Description
Bug report
Describe the bug
When there's a null validation in Where
(not sure if that affects other cascading methods), it throws a NullReferenceException.
In the case below, it throws when Get
is executed.
To Reproduce
Steps to reproduce the behavior, please provide code snippets or a repository:
await client
.From<TTable>()
.Select("*")
.Where(x => requestModel.FilterPredicate == null || requestModel.FilterPredicate(x)) // Boom!
.Get()
Stack trace:
System.NullReferenceException: Object reference not set to an instance of an object.
at Supabase.Postgrest.Table`1.PrepareFilter(IPostgrestQueryFilter filter)
at Supabase.Postgrest.Table`1.PrepareFilter(IPostgrestQueryFilter filter)
at System.Linq.Enumerable.SelectListIterator`2.MoveNext()
at Supabase.Postgrest.Table`1.GenerateUrl()
at Supabase.Postgrest.Table`1.Send[TU](HttpMethod method, Object data, Dictionary`2 headers, CancellationToken cancellationToken, Boolean isInsert, Boolean isUpdate, Boolean isUpsert)
at Supabase.Postgrest.Table`1.Get(CancellationToken cancellationToken)
Expected behavior
Where
is supposed to evaluate the nullability of the FilterPredicate
property and execute it if not null.
Debugging
In this screenshot, I followed the stack trace to see if I could understand the problem.
I noticed that visitor.Filter
is filled with two Criteria
: One is an "Equals" operation, the other is null.
My assumption is that this Criteria[1] is null because FilterPredicate
is, in fact, null.
When any of the Criteria instances are null, though, it throws a NRE when it attempts to GenerateUrl()
: (Table.cs:654)
The PrepareFilter method does not validate for a null IPostgrestQueryFilter filter
, which happens when it is executed in the first recursive call, at line 786:
Workaround
Remove the null validation from the lazy call and do it outside Where
:
var query = client
.From<TTable>()
.Select("*");
if (requestModel.FilterPredicate != null)
{
query = query.Where(x => requestModel.FilterPredicate(x)); // Works
}
var result = await query.Get();
Let me know if it's the expected behavior.
Thanks!
System information
- OS: Windows