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

Fixes #1103: Enable any/all in $compute #1115

Merged
merged 1 commit into from
Dec 1, 2023
Merged

Fixes #1103: Enable any/all in $compute #1115

merged 1 commit into from
Dec 1, 2023

Conversation

xuzhg
Copy link
Member

@xuzhg xuzhg commented Nov 29, 2023

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 for AnyNode is the toplevel source, but the rangeVariable (r) is the lambda variable.

@@ -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")
Copy link
Contributor

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?

Copy link
Member Author

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.

Comment on lines +291 to +318
[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);
}
Copy link

Choose a reason for hiding this comment

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

🎉

@xuzhg xuzhg merged commit bba09bf into main Dec 1, 2023
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Compute with a lambda throw exception
4 participants