Skip to content

Commit

Permalink
stop excluding null values when filtering using notEqual (#114)
Browse files Browse the repository at this point in the history
* stop excluding null values when filtering using notEqual
* add IgnoreNullsOnNotEqual config field, to enable/disable the new feature

Co-authored-by: AnasZakarneh <[email protected]>
  • Loading branch information
AnasZakarneh and AnasZakarneh authored May 24, 2021
1 parent 7542ec0 commit aaedf7a
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 9 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ Then you can add the configuration:
"CaseSensitive": "boolean: should property names be case-sensitive? Defaults to false",
"DefaultPageSize": "int number: optional number to fallback to when no page argument is given. Set <=0 to disable paging if no pageSize is specified (default).",
"MaxPageSize": "int number: maximum allowed page size. Set <=0 to make infinite (default)",
"ThrowExceptions": "boolean: should Sieve throw exceptions instead of silently failing? Defaults to false"
"ThrowExceptions": "boolean: should Sieve throw exceptions instead of silently failing? Defaults to false",
"IgnoreNullsOnNotEqual": "boolean: ignore null values when filtering using is not equal operator? Default to true"
}
}
```
Expand Down
3 changes: 2 additions & 1 deletion Sieve.Sample/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
},
"Sieve": {
"CaseSensitive": false,
"DefaultPageSize": 10
"DefaultPageSize": 10,
"IgnoreNullsOnNotEqual": true
},
"Logging": {
"Debug": {
Expand Down
4 changes: 3 additions & 1 deletion Sieve/Models/SieveOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ public class SieveOptions
public int MaxPageSize { get; set; } = 0;

public bool ThrowExceptions { get; set; } = false;

public bool IgnoreNullsOnNotEqual { get; set; } = true;
}
}
}
12 changes: 7 additions & 5 deletions Sieve/Services/SieveProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,13 @@ protected virtual IQueryable<TEntity> ApplyFiltering<TEntity>(TSieveModel model,
expression = Expression.Not(expression);
}

var filterValueNullCheck = GetFilterValueNullCheck(parameter, fullPropertyName, isFilterTermValueNull);
if (filterValueNullCheck != null)
if (expression.NodeType != ExpressionType.NotEqual || Options.Value.IgnoreNullsOnNotEqual)
{
expression = Expression.AndAlso(filterValueNullCheck, expression);
var filterValueNullCheck = GetFilterValueNullCheck(parameter, fullPropertyName, isFilterTermValueNull);
if (filterValueNullCheck != null)
{
expression = Expression.AndAlso(filterValueNullCheck, expression);
}
}

innerExpression = innerExpression == null
Expand Down Expand Up @@ -254,8 +257,7 @@ protected virtual IQueryable<TEntity> ApplyFiltering<TEntity>(TSieveModel model,
: result.Where(Expression.Lambda<Func<TEntity, bool>>(outerExpression, parameter));
}

private static Expression GetFilterValueNullCheck(Expression parameter, string fullPropertyName,
bool isFilterTermValueNull)
private static Expression GetFilterValueNullCheck(Expression parameter, string fullPropertyName, bool isFilterTermValueNull)
{
var (propertyValue, nullCheck) = GetPropertyValueAndNullCheckExpression(parameter, fullPropertyName);

Expand Down
27 changes: 26 additions & 1 deletion SieveUnitTests/General.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,24 @@ public class General
{
private readonly ITestOutputHelper _testOutputHelper;
private readonly SieveProcessor _processor;
private readonly SieveProcessor _nullableProcessor;
private readonly IQueryable<Post> _posts;
private readonly IQueryable<Comment> _comments;

public General(ITestOutputHelper testOutputHelper)
{
var nullableAccessor = new SieveOptionsAccessor();
nullableAccessor.Value.IgnoreNullsOnNotEqual = false;

_testOutputHelper = testOutputHelper;
_processor = new ApplicationSieveProcessor(new SieveOptionsAccessor(),
new SieveCustomSortMethods(),
new SieveCustomFilterMethods());

_nullableProcessor = new ApplicationSieveProcessor(nullableAccessor,
new SieveCustomSortMethods(),
new SieveCustomFilterMethods());

_posts = new List<Post>
{
new Post
Expand Down Expand Up @@ -180,10 +188,27 @@ public void CanFilterNullableInts()
};

var result = _processor.Apply(model, _posts);
var nullableResult = _nullableProcessor.Apply(model, _posts);

Assert.True(result.Count() == 2);
Assert.True(nullableResult.Count() == 2);
}

[Fact]
public void CanFilterNullableIntsWithNotEqual()
{
var model = new SieveModel()
{
Filters = "CategoryId!=1"
};

var result = _processor.Apply(model, _posts);
var nullableResult = _nullableProcessor.Apply(model, _posts);

Assert.True(result.Count() == 1);
Assert.True(nullableResult.Count() == 2);
}

[Theory]
[InlineData(@"Text@=*\,")]
[InlineData(@"Text@=*\, ")]
Expand Down
25 changes: 25 additions & 0 deletions SieveUnitTests/GeneralWithInterfaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,24 @@ public class GeneralWithInterfaces
{
private readonly ITestOutputHelper _testOutputHelper;
private readonly SieveProcessor _processor;
private readonly SieveProcessor _nullableProcessor;
private readonly IQueryable<IPost> _posts;
private readonly IQueryable<Comment> _comments;

public GeneralWithInterfaces(ITestOutputHelper testOutputHelper)
{
var nullableAccessor = new SieveOptionsAccessor();
nullableAccessor.Value.IgnoreNullsOnNotEqual = false;

_testOutputHelper = testOutputHelper;
_processor = new ApplicationSieveProcessor(new SieveOptionsAccessor(),
new SieveCustomSortMethods(),
new SieveCustomFilterMethods());

_nullableProcessor = new ApplicationSieveProcessor(nullableAccessor,
new SieveCustomSortMethods(),
new SieveCustomFilterMethods());

_posts = new List<IPost>
{
new Post
Expand Down Expand Up @@ -181,8 +189,25 @@ public void CanFilterNullableInts()
};

var result = _processor.Apply(model, _posts);
var nullableResult = _nullableProcessor.Apply(model, _posts);

Assert.True(result.Count() == 2);
Assert.True(nullableResult.Count() == 2);
}

[Fact]
public void CanFilterNullableIntsWithNotEqual()
{
var model = new SieveModel()
{
Filters = "CategoryId!=1"
};

var result = _processor.Apply(model, _posts);
var nullableResult = _nullableProcessor.Apply(model, _posts);

Assert.True(result.Count() == 1);
Assert.True(nullableResult.Count() == 2);
}

[Fact]
Expand Down

0 comments on commit aaedf7a

Please sign in to comment.