Skip to content

Commit

Permalink
Merge pull request #37 from ZEXSM/bugfix/operator-in-new-array
Browse files Browse the repository at this point in the history
#ZEXSM#bugfix use new array in the operator In
  • Loading branch information
ZEXSM committed Nov 27, 2020
2 parents 80b98e1 + c1b7ba3 commit 08b06eb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/OData.QueryBuilder/Visitors/VisitorExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ protected virtual string VisitParameterExpression(ParameterExpression parameterE
MemberExpression memberExpression => GetValueOfMemberExpression(memberExpression),
ConstantExpression constantExpression => GetValueOfConstantExpression(constantExpression),
ListInitExpression listInitExpression => GetValueOfListInitExpression(listInitExpression),
NewArrayExpression newArrayExpression => GetValueOfNewArrayExpression(newArrayExpression),
_ => default,
};

Expand Down Expand Up @@ -280,6 +281,18 @@ protected object GetValueOfListInitExpression(ListInitExpression listInitExpress
return listInit;
}

protected object GetValueOfNewArrayExpression(NewArrayExpression newArrayExpression)
{
var array = Array.CreateInstance(newArrayExpression.Type.GetElementType(), newArrayExpression.Expressions.Count);

for (var i = 0; i < newArrayExpression.Expressions.Count; i++)
{
array.SetValue(GetValueOfExpression(newArrayExpression.Expressions[i]), i);
}

return array;
}

protected bool IsResourceOfMemberExpression(MemberExpression memberExpression) => memberExpression.Expression switch
{
ParameterExpression pe => pe.Type.GetProperty(memberExpression.Member.Name, memberExpression.Type) != default,
Expand Down
12 changes: 12 additions & 0 deletions test/OData.QueryBuilder.Test/ODataQueryOptionListTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,18 @@ public void ODataQueryBuilderList_Operator_In_is_empty_1()
.Should().Throw<ArgumentException>().WithMessage("Enumeration is empty or null");
}

[Fact(DisplayName = "Filter In operator with new => Success")]
public void ODataQueryBuilderList_Filter_In_with_new_Success()
{
var uri = _odataQueryBuilderDefault
.For<ODataTypeEntity>(s => s.ODataType)
.ByList()
.Filter((s, f, o) => o.In(s.ODataKind.ODataCode.Code, new[] { "123", "512" }) && o.In(s.IdType, new[] { 123, 512 }))
.ToUri();

uri.OriginalString.Should().Be($"http://mock/odata/ODataType?$filter=ODataKind/ODataCode/Code in ('123','512') and IdType in (123,512)");
}

[Fact(DisplayName = "Filter boolean values => Success")]
public void ODataQueryBuilderList_Filter_Boolean_Values_Success()
{
Expand Down

0 comments on commit 08b06eb

Please sign in to comment.