Skip to content

Commit

Permalink
fix: custom convertor returning null throws NRE. #220 (#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
moxplod authored Sep 12, 2024
1 parent 7db1737 commit 8bee70a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Gridify/Builder/BaseQueryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ private static object AddIndexerNullCheck(LambdaExpression mapTarget, object que
value = convertor.Invoke(valueExpression.ValueToken.Text);

// handle the `null` keyword in value
if (mapper.Configuration.AllowNullSearch && op.Kind is SyntaxKind.Equal or SyntaxKind.NotEqual && value.ToString() == "null")
if (mapper.Configuration.AllowNullSearch && op.Kind is SyntaxKind.Equal or SyntaxKind.NotEqual && value?.ToString() == "null")
value = null;

// type fixer
Expand Down
19 changes: 19 additions & 0 deletions test/Gridify.Tests/GridifyExtensionsShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,25 @@ public void ApplyFiltering_NullHandlingUsingCustomConvertor()
Assert.Equal(expected, actual);
Assert.True(actual.Any());
}

[Fact] // issue #220
public void ApplyFiltering_CustomConvertor_ReturningNull_ShouldWork()
{
// create custom mapper
var gm = new GridifyMapper<TestClass>(q => q.AllowNullSearch = true).GenerateMappings();

// map any string to related property , also use Client convertor to handle custom scenarios
gm.AddMap("date", g => g.MyDateTime!, q => (q == "null" ? null : q)!);

var actual = _fakeRepository.AsQueryable()
.ApplyFiltering("date=null", gm)
.ToList();

var expected = _fakeRepository.Where(q => q.MyDateTime == null).ToList();
Assert.Equal(expected.Count, actual.Count);
Assert.Equal(expected, actual);
Assert.True(actual.Any());
}

[Fact]
public void ApplyFiltering_NullHandlingUsingMapper()
Expand Down

0 comments on commit 8bee70a

Please sign in to comment.