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

Params array: only promote first argument if it can be an array #267

Merged
merged 2 commits into from
Dec 1, 2022
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
2 changes: 1 addition & 1 deletion src/DynamicExpresso.Core/Parsing/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2228,7 +2228,7 @@ private static bool CheckIfMethodIsApplicableAndPrepareIt(MethodData method, Exp
declaredWorkingParameters++;
}

if (paramsArrayPromotedArgument == null)
if (paramsArrayPromotedArgument == null && (paramsArrayTypeFound == null || args.Length == method.Parameters.Length))
{
if (parameterType.IsGenericParameter)
{
Expand Down
16 changes: 16 additions & 0 deletions test/DynamicExpresso.UnitTest/GithubIssues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ public static class Utils
{
public static List<T> Array<T>(IEnumerable<T> collection) => new List<T>(collection);
public static List<dynamic> Array(params dynamic[] array) => Array((IEnumerable<dynamic>)array);
public static int ParamArrayObjects(params object[] values) => values.Length;
public static IEnumerable<dynamic> Select<TSource>(IEnumerable<TSource> collection, string expression) => new List<dynamic>();
public static IEnumerable<dynamic> Select(IEnumerable collection, string expression) => new List<dynamic>();
public static int Any<T>(IEnumerable<T> collection) => 1;
Expand Down Expand Up @@ -703,6 +704,21 @@ public List<int> Add(List<int> list, Func<int, int> transform)
return list.Select(i => transform(i)).ToList();
}
}

[Test]
[TestCase("0, null, 0, 0")]
[TestCase("null, null, 0, 0")]
[TestCase("new object[] { null, null, null, null }")]
public void GitHub_Issue_263(string paramsArguments)
{
var interpreter = new Interpreter();
interpreter.Reference(typeof(Utils));

Assert.Throws<NullReferenceException>(() => interpreter.Eval<int>("Utils.ParamArrayObjects(null)"));

var result = interpreter.Eval<int>($"Utils.ParamArrayObjects({paramsArguments})");
Assert.AreEqual(4, result);
}
}

internal static class GithubIssuesTestExtensionsMethods
Expand Down