Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ private Expression BindCustomMethodExpressionOrNull(SingleValueFunctionCallNode
MethodInfo methodInfo;
if (UriFunctionsBinder.TryGetMethodInfo(node.Name, methodArgumentsType, out methodInfo))
{
return ExpressionBinderHelper.MakeFunctionCall(methodInfo, QuerySettings, arguments);
return ExpressionBinderHelper.MakeCustomFunctionCall(methodInfo, arguments);
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,22 @@ public static Expression MakeFunctionCall(MemberInfo member, ODataQuerySettings
return CreateFunctionCallWithNullPropagation(functionCall, arguments, querySettings);
}

//Custom methods might contain nullable parameters and, therefore, also should be able to take arguments of type Nullable<T>
public static Expression MakeCustomFunctionCall(MethodInfo method, params Expression[] arguments)
{
Expression functionCall;
if (method.IsStatic)
{
functionCall = Expression.Call(null, method, arguments);
}
else
{
functionCall = Expression.Call(arguments.First(), method, arguments.Skip(1));
}

return functionCall;
}

public static Expression CreateFunctionCallWithNullPropagation(Expression functionCall, Expression[] arguments, ODataQuerySettings querySettings)
{
if (querySettings.HandleNullPropagation == HandleNullPropagationOption.True)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ protected virtual Expression BindCustomMethodExpressionOrNull(SingleValueFunctio
MethodInfo methodInfo;
if (UriFunctionsBinder.TryGetMethodInfo(node.Name, methodArgumentsType, out methodInfo))
{
return ExpressionBinderHelper.MakeFunctionCall(methodInfo, context.QuerySettings, arguments);
return ExpressionBinderHelper.MakeCustomFunctionCall(methodInfo, arguments);
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@
//------------------------------------------------------------------------------

using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Microsoft.AspNetCore.OData.Query;
using Microsoft.AspNetCore.OData.Query.Expressions;
using Microsoft.AspNetCore.OData.Query.Wrapper;
using Microsoft.AspNetCore.OData.Tests.Commons;
using Microsoft.AspNetCore.Routing;
using Microsoft.OData.Edm;
using Microsoft.OData.UriParser;
using Moq;
using Xunit;
using Xunit.Sdk;

namespace Microsoft.AspNetCore.OData.Tests.Query.Expressions
{
Expand Down Expand Up @@ -161,6 +165,48 @@ public void GetPropertyExpression_Works_ForAggregateOrNonAggregate(bool isAggreg
Assert.NotNull(expression);
Assert.Equal(expected, expression.ToString());
}

[Theory]
[InlineData(0)]
[InlineData(null)]
public void MakeCustomFunctionCall_StaticMethod_ShouldCreateCorrectExpression(int? value)
{
// Arrange
MethodInfo methodInfo = typeof(TestCustomFunctionCall).GetMethod(nameof(TestCustomFunctionCall.StaticCustomMethod));
Expression[] arguments = { Expression.Constant(value, typeof(int?)) };

// Act
Expression result = ExpressionBinderHelper.MakeCustomFunctionCall(methodInfo, arguments);

// Assert
Assert.NotNull(result);
Assert.IsAssignableFrom<MethodCallExpression>(result);
var methodCall = (MethodCallExpression)result;
Assert.Equal(methodInfo, methodCall.Method);
Assert.Equal(arguments, methodCall.Arguments);
}

[Theory]
[InlineData(0)]
[InlineData(null)]
public void MakeCustomFunctionCall_InstanceMethod_ShouldCreateCorrectExpression(int? value)
{
// Arrange
MethodInfo methodInfo = typeof(TestCustomFunctionCall).GetMethod(nameof(TestCustomFunctionCall.InstanceCustomMethod));
Expression instance = Expression.Constant(new TestCustomFunctionCall());
Expression[] arguments = { instance, Expression.Constant(value, typeof(int?)) };

// Act
Expression result = ExpressionBinderHelper.MakeCustomFunctionCall(methodInfo, arguments);

// Assert
Assert.NotNull(result);
Assert.IsAssignableFrom<MethodCallExpression>(result);
var methodCall = (MethodCallExpression)result;
Assert.Equal(methodInfo, methodCall.Method);
Assert.Equal(arguments.Skip(1), methodCall.Arguments);
Assert.Equal(instance, methodCall.Object);
}
}

public class MyQueryBinder : QueryBinder
Expand All @@ -170,4 +216,9 @@ public static PropertyInfo Call_GetDynamicPropertyContainer(SingleValueOpenPrope
return GetDynamicPropertyContainer(openNode, context);
}
}
internal class TestCustomFunctionCall
{
public static void StaticCustomMethod(int? x) { }
public void InstanceCustomMethod(int? x) { }
}
}