Skip to content

Fix bug in Filter Any within Expand #2530

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

Merged
merged 3 commits into from
Jul 29, 2021
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 @@ -940,7 +940,9 @@ private ParameterExpression HandleLambdaParameters(IEnumerable<RangeVariable> ra
foreach (RangeVariable rangeVariable in rangeVariables)
{
ParameterExpression parameter;
if (!_lambdaParameters.TryGetValue(rangeVariable.Name, out parameter))

// Create a Parameter Expression for rangeVariables which are not $it Lambda parameters or $this.
if (!_lambdaParameters.TryGetValue(rangeVariable.Name, out parameter) && rangeVariable.Name != ODataThisParameterName)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the comment you talk about creating parameter expression for range variables that are neither $it lambda parameters or $this but your change only added && rangeVariable.Name != ODataThisParameterName. Will that suffice?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code !_lambdaParameters.TryGetValue(rangeVariable.Name, out parameter) ensures we don't create Parameter expressions for lambda parameters

{
// Work-around issue 481323 where UriParser yields a collection parameter type
// for primitive collections rather than the inner element type of the collection.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,67 @@ public void ProjectAsWrapper_Element_ExpandAndMultipleOrderBy_ShouldOrder()
Assert.Equal(4, fourth.Id);
}

[Theory]
[InlineData("Orders($filter=Customer/HomeAddress/Cities/any(e:e/CityName eq 1001))", new[] { "QueryOrder1" })]
[InlineData("Orders($filter=Customer/HomeAddress/Cities/any(e:e/CityName eq 1002))", new[] { "QueryOrder1", "QueryOrder3" })]
[InlineData("Orders($filter=Customer/HomeAddress/Cities/any(e:e/CityName eq 1003))", new[] { "QueryOrder2", "QueryOrder3" })]
public void ProjectAsWrapper_Element_ExpandAndFilterByAny(string expand, object expected)
{
// Arrange
// Customer?$expand=Orders($filter=Customer/HomeAddress/Cities/any(e:e/CityName eq 1001))
// Customer?$expand=Orders($filter=Customer/HomeAddress/Cities/any(e:e/CityName eq 1002))
// Customer?$expand=Orders($filter=Customer/HomeAddress/Cities/any(e:e/CityName eq 1003))

var city1 = new QueryCity() { Id = 1, CityName = 1001 };
var city2 = new QueryCity() { Id = 2, CityName = 1002 };
var city3 = new QueryCity() { Id = 3, CityName = 1003 };
var city4 = new QueryCity() { Id = 4, CityName = 1004 };

QueryCustomer customer1 = new QueryCustomer
{
HomeAddress = new QueryAddress
{
Cities = new List<QueryCity>() { city1, city2 }
}
};

QueryCustomer customer2 = new QueryCustomer
{
HomeAddress = new QueryAddress
{
Cities = new List<QueryCity>() { city3, city4 }
}
};

QueryCustomer customer3 = new QueryCustomer
{
HomeAddress = new QueryAddress
{
Cities = new List<QueryCity>() { city2, city3 }
}
};

var orders = new List<QueryOrder>
{
new QueryOrder{ Title = "QueryOrder1", Customer = customer1 },
new QueryOrder{ Title = "QueryOrder2", Customer = customer2 },
new QueryOrder{ Title = "QueryOrder3", Customer = customer3 },
};

Expression source = Expression.Constant(new QueryCustomer() { Orders = orders });
SelectExpandClause selectExpandClause = ParseSelectExpand(null, expand, _model, _customer, _customers);
Assert.NotNull(selectExpandClause);

// Act
Expression projection = _binder.ProjectAsWrapper(source, selectExpandClause, _customer, _customers);

// Assert
var customerWrappers = Expression.Lambda(projection).Compile().DynamicInvoke() as SelectExpandWrapper<QueryCustomer>;
var orderWrappers = customerWrappers.Container.ToDictionary(PropertyMapper)["Orders"] as IEnumerable<SelectExpandWrapper<QueryOrder>>;
var orderTitleList = orderWrappers.Select(s => s.Instance.Title).ToList();
Assert.Equal(expected, orderTitleList);
}

[Fact]
public void ProjectAsWrapper_Element_ProjectedValueContainsSubKeys_IfDollarRefInDollarExpandOnSubNavigationProperty()
{
Expand Down