Skip to content

Commit

Permalink
Fix logic for indexer when parameter-type differs (#569)
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH authored Feb 21, 2022
1 parent 7334e67 commit b6a0d9b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
6 changes: 6 additions & 0 deletions NuGet.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
Open Command Prompt

System.Linq.Dynamic.Core\src>

del /S *.nupkg

VS rebuild

dotnet nuget push **\*.nupkg --source https://api.nuget.org/v3/index.json --api-key x
11 changes: 10 additions & 1 deletion src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1965,7 +1965,16 @@ Expression ParseElementAccess(Expression expr)
throw ParseError(errorPos, Res.NoApplicableIndexer,
TypeHelper.GetTypeName(expr.Type));
case 1:
return Expression.Call(expr, (MethodInfo)mb, args);
var indexMethod = (MethodInfo)mb;
var indexParameterType = indexMethod.GetParameters().First().ParameterType;

var indexArgumentExpression = args[0]; // Indexer only has 1 parameter, so we can use args[0] here
if (indexParameterType != indexArgumentExpression.Type)
{
indexArgumentExpression = Expression.Convert(indexArgumentExpression, indexParameterType);
}

return Expression.Call(expr, indexMethod, indexArgumentExpression);

default:
throw ParseError(errorPos, Res.AmbiguousIndexerInvocation, TypeHelper.GetTypeName(expr.Type));
Expand Down
25 changes: 25 additions & 0 deletions test/System.Linq.Dynamic.Core.Tests/QueryableTests.Select.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
using System.Collections.Generic;
using System.Linq.Dynamic.Core.Exceptions;
using System.Linq.Dynamic.Core.Tests.Helpers.Models;
using FluentAssertions;
using Linq.PropertyTranslator.Core;
using QueryInterceptor.Core;
using Xunit;
using NFluent;
using Newtonsoft.Json.Linq;
#if EFCORE
using Microsoft.AspNetCore.Identity;
#else
Expand Down Expand Up @@ -348,6 +350,29 @@ public void Select_Dynamic_RenameParameterExpression_Is_True()
Check.That(result).Equals("System.Int32[].Select(it => (it * it))");
}

[Fact]
public void Select_Dynamic_JObject_With_Array_Should_Use_Correct_Indexer()
{
// Arrange
var j = new JObject
{
{"I", new JValue(9)},
{"A", new JArray(new[] {1,2,3}) } ,
{"L", new JValue(5)}
};
var queryable = new[] { j }.AsQueryable();

// Act
var result = queryable.Select("new (long(I) as I, (new [] { long(A[0]), long(A[1]), long(A[2])}) as A, long(L) as L)").ToDynamicArray().First();

// Assert
Assert.Equal(9, result.I);
Assert.Equal(5, result.L);
Assert.Equal(1, result.A[0]);
Assert.Equal(2, result.A[1]);
Assert.Equal(3, result.A[2]);
}

[Fact]
public void Select_Dynamic_ReservedKeyword()
{
Expand Down

0 comments on commit b6a0d9b

Please sign in to comment.