Skip to content

Commit b0f09c1

Browse files
committed
Fix #432 ?
1 parent c977d92 commit b0f09c1

File tree

3 files changed

+81
-3
lines changed

3 files changed

+81
-3
lines changed

src/System.Linq.Dynamic.Core/Parser/ExpressionHelper.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,7 @@ private List<Expression> CollectExpressions(bool addSelf, Expression sourceExpre
323323
break;
324324

325325
case MethodCallExpression methodCallExpression:
326-
// FIX method without parameter: https://github.com/zzzprojects/System.Linq.Dynamic.Core/issues/432
327-
expression = methodCallExpression.Arguments.FirstOrDefault();
326+
expression = methodCallExpression.Object;
328327
expressionRecognized = true;
329328
break;
330329

test/System.Linq.Dynamic.Core.Tests/ExpressionTests.cs

+79-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Newtonsoft.Json.Linq;
1+
using FluentAssertions;
2+
using Newtonsoft.Json.Linq;
23
using NFluent;
34
using System.Collections.Generic;
45
using System.Dynamic;
@@ -49,6 +50,17 @@ public class TestObjectIdClass
4950
public long ObjectId { get; set; }
5051
}
5152

53+
public class Foo
54+
{
55+
public Foo FooValue { get; set; }
56+
57+
public string Zero() => null;
58+
59+
public string One(int x) => null;
60+
61+
public string Two(int x, int y) => null;
62+
}
63+
5264
[Fact]
5365
public void ExpressionTests_Add_Number()
5466
{
@@ -1394,6 +1406,72 @@ public void ExpressionTests_NullPropagating(string test, string query)
13941406
Check.That(queryAsString).Equals(query);
13951407
}
13961408

1409+
[Fact]
1410+
public void ExpressionTests_NullPropagating_InstanceMethod_Zero_Arguments()
1411+
{
1412+
// Arrange 1
1413+
var expression = "np(FooValue.Zero().Length)";
1414+
1415+
// Act 1
1416+
var lambdaExpression = DynamicExpressionParser.ParseLambda(typeof(Foo), null, expression, new Foo());
1417+
1418+
// Assert 1
1419+
lambdaExpression.ToString().Should().Be("Param_0 => IIF((((Param_0 != null) AndAlso (Param_0.FooValue != null)) AndAlso (Param_0.FooValue.Zero() != null)), Convert(Param_0.FooValue.Zero().Length), null)");
1420+
1421+
// Arrange 2
1422+
var q = new[] { new Foo { FooValue = new Foo() } }.AsQueryable();
1423+
1424+
// Act 2
1425+
var result = q.Select(expression).FirstOrDefault() as string;
1426+
1427+
// Assert 2
1428+
result.Should().BeNull();
1429+
}
1430+
1431+
[Fact]
1432+
public void ExpressionTests_NullPropagating_InstanceMethod_One_Argument()
1433+
{
1434+
// Arrange 1
1435+
var expression = "np(FooValue.One(1).Length)";
1436+
1437+
// Act 1
1438+
var lambdaExpression = DynamicExpressionParser.ParseLambda(typeof(Foo), null, expression, new Foo());
1439+
1440+
// Assert 1
1441+
lambdaExpression.ToString().Should().Be("Param_0 => IIF((((Param_0 != null) AndAlso (Param_0.FooValue != null)) AndAlso (Param_0.FooValue.One(1) != null)), Convert(Param_0.FooValue.One(1).Length), null)");
1442+
1443+
// Arrange 2
1444+
var q = new[] { new Foo { FooValue = new Foo() } }.AsQueryable();
1445+
1446+
// Act 2
1447+
var result = q.Select(expression).FirstOrDefault() as string;
1448+
1449+
// Assert 2
1450+
result.Should().BeNull();
1451+
}
1452+
1453+
[Fact]
1454+
public void ExpressionTests_NullPropagating_InstanceMethod_Two_Arguments()
1455+
{
1456+
// Arrange 1
1457+
var expression = "np(FooValue.Two(1, 42).Length)";
1458+
1459+
// Act 1
1460+
var lambdaExpression = DynamicExpressionParser.ParseLambda(typeof(Foo), null, expression, new Foo());
1461+
1462+
// Assert 1
1463+
lambdaExpression.ToString().Should().Be("Param_0 => IIF((((Param_0 != null) AndAlso (Param_0.FooValue != null)) AndAlso (Param_0.FooValue.Two(1, 42) != null)), Convert(Param_0.FooValue.Two(1, 42).Length), null)");
1464+
1465+
// Arrange 2
1466+
var q = new[] { new Foo { FooValue = new Foo() } }.AsQueryable();
1467+
1468+
// Act 2
1469+
var result = q.Select(expression).FirstOrDefault() as string;
1470+
1471+
// Assert 2
1472+
result.Should().BeNull();
1473+
}
1474+
13971475
[Fact]
13981476
public void ExpressionTests_NullPropagation_Method()
13991477
{

test/System.Linq.Dynamic.Core.Tests/System.Linq.Dynamic.Core.Tests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
<PackageReference Include="xunit" Version="2.4.1" />
3838
<PackageReference Include="NFluent" Version="2.7.0" />
3939
<PackageReference Include="Moq" Version="4.13.1" />
40+
<!--<PackageReference Include="ExpressionTreeToString" Version="3.1.47" />-->
4041
</ItemGroup>
4142

4243
<ItemGroup Condition=" '$(TargetFramework)' == 'net452' ">

0 commit comments

Comments
 (0)