mirror of
https://github.com/Biarity/Sieve.git
synced 2024-11-24 22:42:30 +01:00
Fix issue where sorting or filtering a collection fails on accesssing null members.
This commit is contained in:
parent
d86e35f77c
commit
5c2ef3773e
@ -1,36 +1,62 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Linq.Expressions;
|
using System.Linq.Expressions;
|
||||||
using System.Reflection;
|
|
||||||
|
|
||||||
namespace Sieve.Extensions
|
namespace Sieve.Extensions
|
||||||
{
|
{
|
||||||
public static partial class LinqExtentions
|
public static partial class LinqExtentions
|
||||||
{
|
{
|
||||||
public static IQueryable<TEntity> OrderByDynamic<TEntity>(this IQueryable<TEntity> source, string fullPropertyName, PropertyInfo propertyInfo,
|
public static IQueryable<TEntity> OrderByDynamic<TEntity>(
|
||||||
bool desc, bool useThenBy)
|
this IQueryable<TEntity> source,
|
||||||
|
string fullPropertyName,
|
||||||
|
bool desc,
|
||||||
|
bool useThenBy)
|
||||||
{
|
{
|
||||||
string command = desc ?
|
var lambda = GenerateLambdaWithSafeMemberAccess<TEntity>(fullPropertyName);
|
||||||
(useThenBy ? "ThenByDescending" : "OrderByDescending") :
|
|
||||||
(useThenBy ? "ThenBy" : "OrderBy");
|
|
||||||
var type = typeof(TEntity);
|
|
||||||
var parameter = Expression.Parameter(type, "p");
|
|
||||||
|
|
||||||
dynamic propertyValue = parameter;
|
var command = desc
|
||||||
if (fullPropertyName.Contains("."))
|
? (useThenBy ? "ThenByDescending" : "OrderByDescending")
|
||||||
|
: (useThenBy ? "ThenBy" : "OrderBy");
|
||||||
|
|
||||||
|
var resultExpression = Expression.Call(
|
||||||
|
typeof(Queryable),
|
||||||
|
command,
|
||||||
|
new Type[] { typeof(TEntity), lambda.ReturnType },
|
||||||
|
source.Expression,
|
||||||
|
Expression.Quote(lambda));
|
||||||
|
|
||||||
|
return source.Provider.CreateQuery<TEntity>(resultExpression);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Expression<Func<TEntity, object>> GenerateLambdaWithSafeMemberAccess<TEntity>(string fullPropertyName)
|
||||||
|
{
|
||||||
|
var parameter = Expression.Parameter(typeof(TEntity), "e");
|
||||||
|
Expression propertyValue = parameter;
|
||||||
|
Expression nullCheck = null;
|
||||||
|
|
||||||
|
foreach (var name in fullPropertyName.Split('.'))
|
||||||
{
|
{
|
||||||
var parts = fullPropertyName.Split('.');
|
propertyValue = Expression.PropertyOrField(propertyValue, name);
|
||||||
for (var i = 0; i < parts.Length - 1; i++)
|
|
||||||
|
if (propertyValue.Type.IsNullable())
|
||||||
{
|
{
|
||||||
propertyValue = Expression.PropertyOrField(propertyValue, parts[i]);
|
nullCheck = GenerateOrderNullCheckExpression(propertyValue, nullCheck);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var propertyAccess = Expression.MakeMemberAccess(propertyValue, propertyInfo);
|
var expression = nullCheck == null
|
||||||
var orderByExpression = Expression.Lambda(propertyAccess, parameter);
|
? propertyValue
|
||||||
var resultExpression = Expression.Call(typeof(Queryable), command, new Type[] { type, propertyInfo.PropertyType },
|
: Expression.Condition(nullCheck, Expression.Default(propertyValue.Type), propertyValue);
|
||||||
source.Expression, Expression.Quote(orderByExpression));
|
|
||||||
return source.Provider.CreateQuery<TEntity>(resultExpression);
|
var converted = Expression.Convert(expression, typeof(object));
|
||||||
|
return Expression.Lambda<Func<TEntity, object>>(converted, parameter);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Expression GenerateOrderNullCheckExpression(Expression propertyValue, Expression nullCheckExpression)
|
||||||
|
{
|
||||||
|
return nullCheckExpression == null
|
||||||
|
? Expression.Equal(propertyValue, Expression.Default(propertyValue.Type))
|
||||||
|
: Expression.OrElse(nullCheckExpression, Expression.Equal(propertyValue, Expression.Default(propertyValue.Type)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
12
Sieve/Extensions/TypeExtentions.cs
Normal file
12
Sieve/Extensions/TypeExtentions.cs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Sieve.Extensions
|
||||||
|
{
|
||||||
|
public static partial class TypeExtentions
|
||||||
|
{
|
||||||
|
public static bool IsNullable(this Type type)
|
||||||
|
{
|
||||||
|
return !type.IsValueType || Nullable.GetUnderlyingType(type) != null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -170,21 +170,27 @@ namespace Sieve.Services
|
|||||||
}
|
}
|
||||||
|
|
||||||
Expression outerExpression = null;
|
Expression outerExpression = null;
|
||||||
var parameterExpression = Expression.Parameter(typeof(TEntity), "e");
|
var parameter = Expression.Parameter(typeof(TEntity), "e");
|
||||||
foreach (var filterTerm in model.GetFiltersParsed())
|
foreach (var filterTerm in model.GetFiltersParsed())
|
||||||
{
|
{
|
||||||
Expression innerExpression = null;
|
Expression innerExpression = null;
|
||||||
foreach (var filterTermName in filterTerm.Names)
|
foreach (var filterTermName in filterTerm.Names)
|
||||||
{
|
{
|
||||||
var (fullName, property) = GetSieveProperty<TEntity>(false, true, filterTermName);
|
var (fullPropertyName, property) = GetSieveProperty<TEntity>(false, true, filterTermName);
|
||||||
if (property != null)
|
if (property != null)
|
||||||
{
|
{
|
||||||
var converter = TypeDescriptor.GetConverter(property.PropertyType);
|
var converter = TypeDescriptor.GetConverter(property.PropertyType);
|
||||||
|
Expression propertyValue = parameter;
|
||||||
|
Expression nullCheck = null;
|
||||||
|
|
||||||
dynamic propertyValue = parameterExpression;
|
foreach (var name in fullPropertyName.Split('.'))
|
||||||
foreach (var part in fullName.Split('.'))
|
|
||||||
{
|
{
|
||||||
propertyValue = Expression.PropertyOrField(propertyValue, part);
|
propertyValue = Expression.PropertyOrField(propertyValue, name);
|
||||||
|
|
||||||
|
if (propertyValue.Type.IsNullable())
|
||||||
|
{
|
||||||
|
nullCheck = GenerateFilterNullCheckExpression(propertyValue, nullCheck);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (filterTerm.Values == null) continue;
|
if (filterTerm.Values == null) continue;
|
||||||
@ -217,6 +223,11 @@ namespace Sieve.Services
|
|||||||
expression = Expression.Not(expression);
|
expression = Expression.Not(expression);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (nullCheck != null)
|
||||||
|
{
|
||||||
|
expression = Expression.AndAlso(nullCheck, expression);
|
||||||
|
}
|
||||||
|
|
||||||
if (innerExpression == null)
|
if (innerExpression == null)
|
||||||
{
|
{
|
||||||
innerExpression = expression;
|
innerExpression = expression;
|
||||||
@ -251,7 +262,14 @@ namespace Sieve.Services
|
|||||||
}
|
}
|
||||||
return outerExpression == null
|
return outerExpression == null
|
||||||
? result
|
? result
|
||||||
: result.Where(Expression.Lambda<Func<TEntity, bool>>(outerExpression, parameterExpression));
|
: result.Where(Expression.Lambda<Func<TEntity, bool>>(outerExpression, parameter));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Expression GenerateFilterNullCheckExpression(Expression propertyValue, Expression nullCheckExpression)
|
||||||
|
{
|
||||||
|
return nullCheckExpression == null
|
||||||
|
? Expression.NotEqual(propertyValue, Expression.Default(propertyValue.Type))
|
||||||
|
: Expression.AndAlso(nullCheckExpression, Expression.NotEqual(propertyValue, Expression.Default(propertyValue.Type)));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Expression GetExpression(TFilterTerm filterTerm, dynamic filterValue, dynamic propertyValue)
|
private static Expression GetExpression(TFilterTerm filterTerm, dynamic filterValue, dynamic propertyValue)
|
||||||
@ -311,7 +329,7 @@ namespace Sieve.Services
|
|||||||
|
|
||||||
if (property != null)
|
if (property != null)
|
||||||
{
|
{
|
||||||
result = result.OrderByDynamic(fullName, property, sortTerm.Descending, useThenBy);
|
result = result.OrderByDynamic(fullName, sortTerm.Descending, useThenBy);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -373,12 +391,12 @@ namespace Sieve.Services
|
|||||||
bool isCaseSensitive)
|
bool isCaseSensitive)
|
||||||
{
|
{
|
||||||
return Array.Find(typeof(TEntity).GetProperties(), p =>
|
return Array.Find(typeof(TEntity).GetProperties(), p =>
|
||||||
{
|
{
|
||||||
return p.GetCustomAttribute(typeof(SieveAttribute)) is SieveAttribute sieveAttribute
|
return p.GetCustomAttribute(typeof(SieveAttribute)) is SieveAttribute sieveAttribute
|
||||||
&& (canSortRequired ? sieveAttribute.CanSort : true)
|
&& (!canSortRequired || sieveAttribute.CanSort)
|
||||||
&& (canFilterRequired ? sieveAttribute.CanFilter : true)
|
&& (!canFilterRequired || sieveAttribute.CanFilter)
|
||||||
&& ((sieveAttribute.Name ?? p.Name).Equals(name, isCaseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase));
|
&& (sieveAttribute.Name ?? p.Name).Equals(name, isCaseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private IQueryable<TEntity> ApplyCustomMethod<TEntity>(IQueryable<TEntity> result, string name, object parent, object[] parameters, object[] optionalParameters = null)
|
private IQueryable<TEntity> ApplyCustomMethod<TEntity>(IQueryable<TEntity> result, string name, object parent, object[] parameters, object[] optionalParameters = null)
|
||||||
|
@ -31,7 +31,6 @@ namespace SieveUnitTests
|
|||||||
LikeCount = 100,
|
LikeCount = 100,
|
||||||
IsDraft = true,
|
IsDraft = true,
|
||||||
CategoryId = null,
|
CategoryId = null,
|
||||||
TopComment = new Comment { Id = 0, Text = "A1" },
|
|
||||||
FeaturedComment = new Comment { Id = 4, Text = "A2" }
|
FeaturedComment = new Comment { Id = 4, Text = "A2" }
|
||||||
},
|
},
|
||||||
new Post() {
|
new Post() {
|
||||||
@ -57,7 +56,7 @@ namespace SieveUnitTests
|
|||||||
LikeCount = 3,
|
LikeCount = 3,
|
||||||
IsDraft = true,
|
IsDraft = true,
|
||||||
CategoryId = 2,
|
CategoryId = 2,
|
||||||
TopComment = new Comment { Id = 1, Text = "D1" },
|
TopComment = new Comment { Id = 1 },
|
||||||
FeaturedComment = new Comment { Id = 7, Text = "D2" }
|
FeaturedComment = new Comment { Id = 7, Text = "D2" }
|
||||||
},
|
},
|
||||||
}.AsQueryable();
|
}.AsQueryable();
|
||||||
@ -388,11 +387,10 @@ namespace SieveUnitTests
|
|||||||
};
|
};
|
||||||
|
|
||||||
var result = _processor.Apply(model, _posts);
|
var result = _processor.Apply(model, _posts);
|
||||||
Assert.AreEqual(3, result.Count());
|
Assert.AreEqual(2, result.Count());
|
||||||
var posts = result.ToList();
|
var posts = result.ToList();
|
||||||
Assert.IsTrue(posts[0].TopComment.Text.Contains("B"));
|
Assert.IsTrue(posts[0].TopComment.Text.Contains("B"));
|
||||||
Assert.IsTrue(posts[1].TopComment.Text.Contains("C"));
|
Assert.IsTrue(posts[1].TopComment.Text.Contains("C"));
|
||||||
Assert.IsTrue(posts[2].TopComment.Text.Contains("D"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
|
Loading…
Reference in New Issue
Block a user