From 848b5d33aebe3746fdf9fbfba1cbfcc554293269 Mon Sep 17 00:00:00 2001 From: romain V Date: Wed, 17 Oct 2018 17:04:59 +0200 Subject: [PATCH] FIX : protection contre un bug EF + evaluation client EF dans le plus cas simple https://github.com/LuccaSA/RestDrivenDomain/issues/236 https://github.com/LuccaSA/RestDrivenDomain/issues/182 --- src/Rdd.Infra/Helpers/WebFilterConverter.cs | 18 ++++++++++++++++-- test/Rdd.Web.Tests/QueryBuilderTests.cs | 16 ++++++++++------ 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/Rdd.Infra/Helpers/WebFilterConverter.cs b/src/Rdd.Infra/Helpers/WebFilterConverter.cs index dfec400b..b5aa5a7a 100644 --- a/src/Rdd.Infra/Helpers/WebFilterConverter.cs +++ b/src/Rdd.Infra/Helpers/WebFilterConverter.cs @@ -60,13 +60,27 @@ protected Expression> AndFactory(Func> Equals(IExpression field, IList values) => BuildLambda(Contains, field, values); protected virtual Expression Contains(Expression leftExpression, IList values) { - return Expression.Call(typeof(Enumerable), "Contains", new[] { leftExpression.Type }, Expression.Constant(values), leftExpression); + if (values.Count == 1) + { + return Expression.Equal(leftExpression, Expression.Constant(values[0])); + } + else + { + return Expression.Call(typeof(Enumerable), "Contains", new[] { leftExpression.Type }, Expression.Constant(values), leftExpression); + } } public Expression> NotEqual(IExpression field, IList values) => BuildLambda(NotEqual, field, values); protected virtual Expression NotEqual(Expression leftExpression, IList values) { - return Expression.Not(Contains(leftExpression, values)); + if (values.Count == 1) + { + return Expression.NotEqual(leftExpression, Expression.Constant(values[0])); + } + else + { + return Expression.Not(Contains(leftExpression, values)); + } } public Expression> ContainsAll(IExpression field, IList values) => AndFactory(value => BuildLambda(Equal, field, value), values); diff --git a/test/Rdd.Web.Tests/QueryBuilderTests.cs b/test/Rdd.Web.Tests/QueryBuilderTests.cs index 863f6c6a..d852f44e 100644 --- a/test/Rdd.Web.Tests/QueryBuilderTests.cs +++ b/test/Rdd.Web.Tests/QueryBuilderTests.cs @@ -88,11 +88,13 @@ public void UntilOnDatetime() Assert.Equal(users[1], nullUser); } - [Fact] - public void EqualsFilter() + [Theory] + [InlineData(1)] + [InlineData(1, 2)] + public void EqualsFilter(params int[] values) { var builder = new WebFilterConverter(); - var expression = builder.Equals(PropertyExpression.New(u => u.Id), new List { 1, 2 }); + var expression = builder.Equals(PropertyExpression.New(u => u.Id), new List(values)); var goodUser = new User { Id = 1 }; var badUser = new User { Id = 3 }; @@ -102,11 +104,13 @@ public void EqualsFilter() Assert.Equal(users[0], goodUser); } - [Fact] - public void NotEqualsFilter() + [Theory] + [InlineData(2)] + [InlineData(1, 2)] + public void NotEqualsFilter(params int[] values) { var builder = new WebFilterConverter(); - var expression = builder.NotEqual(PropertyExpression.New(u => u.Id), new List { 1, 2 }); + var expression = builder.NotEqual(PropertyExpression.New(u => u.Id), new List(values)); var goodUser = new User { Id = 3 }; var badUser = new User { Id = 2 };