Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/AutoFilterer/Attributes/ArraySearchFilterAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections;
using AutoFilterer.Extensions;
using System.Collections;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
Expand All @@ -14,17 +15,17 @@ public override Expression BuildExpression(ExpressionBuildContext context)
return Expression.Constant(true); // TODO: Make it better. Maybe return null? When null, it should be ignored and combined with another expressions.
}

var type = context.TargetProperty.PropertyType;
var prop = Expression.Property(context.ExpressionBody, context.TargetProperty.Name);
var type = context.TargetProperty.PropertyType.AsNonNullable();
var prop = Expression.Property(context.ExpressionBody, context.TargetProperty.Name).GetValueExpressionIfNullable();

var containsMethod = typeof(Enumerable).GetMethods().FirstOrDefault(x => x.Name == nameof(Enumerable.Contains)).MakeGenericMethod(type);

var containsExpression = Expression.Call(
method: containsMethod,
arguments: new Expression[]
{
Expression.Property(Expression.Constant(context.FilterObject), context.FilterProperty),
Expression.Property(context.ExpressionBody, context.TargetProperty)
Expression.Property(Expression.Constant(context.FilterObject), context.FilterProperty).GetValueExpressionIfNullable(),
Expression.Property(context.ExpressionBody, context.TargetProperty).GetValueExpressionIfNullable()
});

return containsExpression;
Expand Down
21 changes: 21 additions & 0 deletions tests/AutoFilterer.Tests/Attributes/ArraySearchAttributeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,27 @@ public void BuildExpression_ShouldGenerateQueryCorrect_WithAttribute(List<Prefer
Assert.True(actualResult.Contains(expected));
}

[Theory, AutoMoqData(count: 64)]
public void BuildExpression_ShouldGenerateQueryCorrect_Guid_WithoutAttribute(List<Preferences> data)
{
// Arrange
var queryable = data.AsQueryable();
var filter = new PreferencesFilter_ArraySearchWithoutAttribute_Guid
{
OrganizationUnitId = data.Take(3).Select(x => x.OrganizationUnitId.GetValueOrDefault()).ToArray()
};

// Act
var expectedResult = queryable.Where(x => filter.OrganizationUnitId.Contains(x.OrganizationUnitId.GetValueOrDefault()));
var actualResult = queryable.ApplyFilter(filter);

// Assert
Assert.Equal(expectedResult.Count(), actualResult.Count());

foreach (var expected in expectedResult)
Assert.True(actualResult.Contains(expected));
}

private static IEnumerable<T> Repeat<T>(Func<T> func, int times = 3)
{
for (int i = 0; i < times; i++)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using AutoFilterer.Attributes;
using AutoFilterer.Types;
using System;

namespace AutoFilterer.Tests.Environment.Dtos;

public class PreferencesFilter_ArraySearchWithoutAttribute_Guid : FilterBase
{
public Guid[] OrganizationUnitId { get; set; }
}
6 changes: 5 additions & 1 deletion tests/AutoFilterer.Tests/Environment/Models/Preferences.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using AutoFilterer.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand All @@ -13,4 +14,7 @@ public class Preferences
public string GivenName { get; set; }
public int SecurityLevel { get; set; }
public int? ReadLimit { get; set; }

[ArraySearchFilter]
public Guid? OrganizationUnitId { get; set; }
}
Loading