-
Notifications
You must be signed in to change notification settings - Fork 160
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
Fixes #1103: Enable any/all in $compute #1115
Conversation
@@ -373,7 +373,7 @@ public virtual Expression BindRangeVariable(RangeVariable rangeVariable, QueryBi | |||
} | |||
|
|||
// Be noted: it's used in $compute for $select and $expand. | |||
if (context.Source != null) | |||
if (context.Source != null && rangeVariable.Name == "$it") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you explain why this fixes the issue?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in any/all lambda expression, it contains the RangeVariable also, it could be like "r", "x" (nested lambda).
However, for the whole $compute, it also contains the RangeVariable, it is "$it" (top-level).
When we Bind the top-level, we should use the "cached" source if apply,
When we Bind the nested lambda, we should use the lambda parameter.
So far, the "context.Source" solution looks "not a good" design, we'd like to continue to improve it when we refactor the applyBinder.
[Fact] | ||
public async Task QueryForAnResource_IncludesDollarComputeWithAnyClause_WithDollarSelect() | ||
{ | ||
// Arrange | ||
string queryUrl = "odata/Customers?$select=Id,HasSnickers&$compute=Candys/any(r:r eq 'Snickers') as HasSnickers"; | ||
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, queryUrl); | ||
request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json;odata.metadata=none")); | ||
HttpClient client = CreateClient(); | ||
HttpResponseMessage response; | ||
|
||
// Act | ||
response = await client.SendAsync(request); | ||
|
||
// Assert | ||
string payload = await response.Content.ReadAsStringAsync(); | ||
|
||
Assert.NotNull(response); | ||
Assert.Equal(HttpStatusCode.OK, response.StatusCode); | ||
Assert.NotNull(response.Content); | ||
|
||
Assert.Equal("{\"value\":[" + | ||
"{\"Id\":1,\"HasSnickers\":false}," + | ||
"{\"Id\":2,\"HasSnickers\":false}," + | ||
"{\"Id\":3,\"HasSnickers\":true}," + | ||
"{\"Id\":4,\"HasSnickers\":true}," + | ||
"{\"Id\":5,\"HasSnickers\":false}" + | ||
"]}", payload); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉
Fixes #1103: Compute with lambda expression
When we $select a computed property from a $compute containing a lambda expression, we should be careful to process the "source" for rangeVariable.
So, for example:
http://localhost:5102/odata/products?$select=HasSnikers&$compute=Candys/any(r:r eq 'Snikers') as HasSnikers
The compute.expression is an
AnyNode
and the body is the binary operator node. so, the rangeVariable ($it) of source forAnyNode
is the toplevel source, but the rangeVariable (r) is the lambda variable.