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
12 changes: 5 additions & 7 deletions src/AspNetCore.IQueryable.Extensions/Filter/ExpressionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@
internal static WhereClause GetCriteria(ICustomQueryable model, PropertyInfo propertyInfo)
{
bool isCollection = propertyInfo.IsPropertyACollection();
//if (!isCollection && propertyInfo.IsPropertyObject(model))
// return null;

var criteria = new WhereClause();

Expand All @@ -70,16 +68,16 @@
{
var data = (QueryOperatorAttribute)attr.First(a => a.GetType() == typeof(QueryOperatorAttribute));
criteria.UpdateAttributeData(data);
if (data.Operator != WhereOperator.Contains && isCollection)
throw new ArgumentException($"{propertyInfo.Name} - For array the only Operator available is Contains");
}
if (isCollection && (data.Operator != WhereOperator.Contains && data.Operator != WhereOperator.ContainsWithLikeForList))
throw new ArgumentException($"{propertyInfo.Name} - For array the only Operator available is Contains and ContainsWithLikeForList");

if (isCollection)
criteria.Operator = WhereOperator.Contains;
if (!isCollection && data.Operator == WhereOperator.ContainsWithLikeForList)
throw new ArgumentException($"{propertyInfo.Name} - LikeInList Operator is only available to string arrays");
}

var customValue = propertyInfo.GetValue(model, null);
if (customValue == null)
return null;

Check warning on line 80 in src/AspNetCore.IQueryable.Extensions/Filter/ExpressionFactory.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.

criteria.UpdateValues(propertyInfo);
return criteria;
Expand Down
41 changes: 34 additions & 7 deletions src/AspNetCore.IQueryable.Extensions/Filter/FiltersExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
Expand All @@ -24,10 +25,10 @@
{
if (model == null)
{
return null;

Check warning on line 28 in src/AspNetCore.IQueryable.Extensions/Filter/FiltersExtensions.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.
}

Expression lastExpression = null;

Check warning on line 31 in src/AspNetCore.IQueryable.Extensions/Filter/FiltersExtensions.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.

var operations = ExpressionFactory.GetOperators<TEntity>(model);
foreach (var expression in operations.Ordered())
Expand Down Expand Up @@ -64,7 +65,7 @@
}
}

return lastExpression != null ? Expression.Lambda<Func<TEntity, bool>>(lastExpression, operations.ParameterExpression) : null;

Check warning on line 68 in src/AspNetCore.IQueryable.Extensions/Filter/FiltersExtensions.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.
}


Expand Down Expand Up @@ -99,30 +100,32 @@
typeof(string).GetMethods()
.First(m => m.Name == "StartsWith" && m.GetParameters().Length == 1),
expression.FilterBy);
case WhereOperator.ContainsWithLikeForList:
return ContainsWithLikeForListExpression<TEntity>(expression);
default:
return Expression.Equal(expression.FieldToFilter, expression.FilterBy);
}
}

private static Expression LessThanOrEqualWhenNullable(Expression e1, Expression e2)
{
if (IsNullableType(e1.Type) && !IsNullableType(e2.Type))
e2 = Expression.Convert(e2, e1.Type);

else if (!IsNullableType(e1.Type) && IsNullableType(e2.Type))
e1 = Expression.Convert(e1, e2.Type);

return Expression.LessThanOrEqual(e1, e2);
}

private static Expression GreaterThanOrEqualWhenNullable(Expression e1, Expression e2)
{
if (IsNullableType(e1.Type) && !IsNullableType(e2.Type))
e2 = Expression.Convert(e2, e1.Type);

else if (!IsNullableType(e1.Type) && IsNullableType(e2.Type))
e1 = Expression.Convert(e1, e2.Type);

return Expression.GreaterThanOrEqual(e1, e2);
}

Expand Down Expand Up @@ -158,9 +161,33 @@

return Expression.Call(expression.FieldToFilter, methodToApplyContains, expression.FilterBy);
}

}

private static Expression ContainsWithLikeForListExpression<TEntity>(ExpressionParser expression)
{
Expression orExpression = null;

if (expression.FilterBy is ConstantExpression constantExpression && constantExpression.Value is IEnumerable<object> patterns)
{
foreach (var pattern in patterns)
{
var likePattern = Expression.Constant(pattern);
var containsMethod = typeof(string).GetMethod("Contains", new[] { typeof(string) });
var containsExpression = Expression.Call(expression.FieldToFilter, containsMethod, likePattern);

if (orExpression == null)
{
orExpression = containsExpression;
}
else
{
orExpression = Expression.OrElse(orExpression, containsExpression);
}
}
}

return orExpression;
}

}
}
5 changes: 3 additions & 2 deletions src/AspNetCore.IQueryable.Extensions/Filter/WhereOperator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ public enum WhereOperator
LessThanOrEqualTo,
Contains,
StartsWith,
LessThanOrEqualWhenNullable,
LessThanOrEqualWhenNullable,
GreaterThanOrEqualWhenNullable,
EqualsWhenNullable
EqualsWhenNullable,
ContainsWithLikeForList
}
}
11 changes: 7 additions & 4 deletions tests/RestFulTests/FilterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ public FilterTests()
[Fact]
public void Should_Filter_By_Has_Name_Attribute()
{
int startIndex = new Random().Next(0, _users.Last().FirstName.Length - 3);
var randomStr = _users.First(x => x.FirstName.Length > 4).FirstName.Substring(startIndex, 3);

var userSearch = new UserSearch()
{
Name = _users.Last().FirstName
Names = new() { randomStr }
};

var sortingByFieldName = _users.AsQueryable().Filter(userSearch);
Expand All @@ -49,7 +52,7 @@ public void Should_Apply_Allfilter_Based_In_Class()
{
var userSearch = new UserSearch()
{
Name = _users.Last().FirstName,
Names = new() { _users.Last().FirstName },
Username = _users.Last().Username
};
var sortingByFieldName = _users.AsQueryable().Filter(userSearch);
Expand All @@ -73,7 +76,7 @@ public void Should_Apply_Or_In_Filter()
{
var userSearch = new UserSearch()
{
Name = _users.First().FirstName,
Names = new() { _users.First().FirstName },
Ssn = _users.First().SocialNumber.Identification,
Username = _users.Last().Username
};
Expand All @@ -88,7 +91,7 @@ public void Should_Apply_Exclusive_Or_In_Filter()
{
var userSearch = new UserSearch()
{
Name = _users.First().FirstName,
Names = new() { _users.First().FirstName },
Username = _users.Last().Username
};
var sortingByFieldName = _users.AsQueryable().Filter(userSearch);
Expand Down
4 changes: 2 additions & 2 deletions tests/RestFulTests/Models/UserSearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public class UserSearch : IQueryPaging, IQuerySort
[QueryOperator(Operator = WhereOperator.GreaterThan)]
public DateTime? Birthday { get; set; }

[QueryOperator(Operator = WhereOperator.Contains, HasName = "Firstname")]
public string Name { get; set; }
[QueryOperator(Operator = WhereOperator.ContainsWithLikeForList, HasName = "Firstname")]
public List<string> Names { get; set; }

[QueryOperator(Operator = WhereOperator.Contains, HasName = "SocialNumber.Identification")]
public string Ssn { get; set; }
Expand Down
Loading