Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw ArgumentException when expression has invalid compare (e.g. string with int) #851

Merged
merged 1 commit into from
Oct 31, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
// SOFTWARE.
#endregion

#if NETSTANDARD1_3_OR_GREATER || NET35 || NET40 || NET45 || NET452 || NET46 || NETCOREAPP2_1 || UAP10_0
#if NETSTANDARD1_3 || NETSTANDARD2_0 || NET35 || NET40 || NET45 || NET452 || NET46 || NETCOREAPP2_1 || UAP10_0

// ReSharper disable once CheckNamespace
namespace System.Diagnostics.CodeAnalysis;
Expand Down
17 changes: 8 additions & 9 deletions src/System.Linq.Dynamic.Core/Parser/ExpressionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,11 @@ private void TryConvertTypes(ref Expression left, ref Expression right)

private static Expression GenerateStaticMethodCall(string methodName, Expression left, Expression right)
{
var methodInfo = GetStaticMethod(methodName, left, right);
if (!TryGetStaticMethod(methodName, left, right, out var methodInfo))
{
throw new ArgumentException($"Method '{methodName}' not found on type '{left.Type}' or '{right.Type}'");
}

var parameters = methodInfo.GetParameters();
var parameterTypeLeft = parameters[0].ParameterType;
var parameterTypeRight = parameters[1].ParameterType;
Expand All @@ -495,15 +499,10 @@ private static Expression GenerateStaticMethodCall(string methodName, Expression
return Expression.Call(null, methodInfo, [left, right]);
}

private static MethodInfo GetStaticMethod(string methodName, Expression left, Expression right)
private static bool TryGetStaticMethod(string methodName, Expression left, Expression right, [NotNullWhen(true)] out MethodInfo? methodInfo)
{
var methodInfo = left.Type.GetMethod(methodName, [left.Type, right.Type]);
if (methodInfo == null)
{
methodInfo = right.Type.GetMethod(methodName, [left.Type, right.Type])!;
}

return methodInfo;
methodInfo = left.Type.GetMethod(methodName, [left.Type, right.Type]) ?? right.Type.GetMethod(methodName, [left.Type, right.Type]);
return methodInfo != null;
}

private static Expression? GetMethodCallExpression(MethodCallExpression methodCallExpression)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public enum ExampleFlags
D = 8,
};

public class MyView
{
public Dictionary<string, string>? Properties { get; set; }
}

public ExpressionParserTests()
{
Expand Down Expand Up @@ -421,8 +425,18 @@ public void Parse_StringConcat(string expression, string result)

// Act
var parsedExpression = parser.Parse(typeof(string)).ToString();

// Assert
parsedExpression.Should().Be(result);
}

[Fact]
public void Parse_InvalidExpressionShouldThrowArgumentException()
{
// Arrange & Act
Action act = () => DynamicExpressionParser.ParseLambda<MyView, bool>(ParsingConfig.Default, false, "Properties[\"foo\"] > 2", Array.Empty<object>());

// Assert
act.Should().Throw<ArgumentException>().WithMessage("Method 'Compare' not found on type 'System.String' or 'System.Int32'");
}
}