Repository navigation
Support contains, startswith and endswith in $filter and database policies #3858
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
Open
Richard Cadogan (arkin-nz)
wants to merge
3
commits into
Azure:main
Choose a base branch
from
arkin-nz:feature/policy-string-functions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using Azure.DataApiBuilder.Core.Resolvers; | ||
| using Microsoft.OData.UriParser; | ||
|
|
||
| namespace Azure.DataApiBuilder.Core.Parsers | ||
| { | ||
| /// <summary> | ||
| /// Shared handling for the OData string functions supported in $filter and database policies: | ||
| /// contains, startswith and endswith. The first argument must be a field and the second must | ||
| /// resolve to a non-empty string, either a constant (including a bound claim value) or a | ||
| /// concat of such values. Folding concat here keeps the whole search value in one parameter. | ||
| /// </summary> | ||
| internal static class ODataStringFunctionHelper | ||
| { | ||
| private const string CONTAINS = "contains"; | ||
| private const string STARTSWITH = "startswith"; | ||
| private const string ENDSWITH = "endswith"; | ||
| private const string CONCAT = "concat"; | ||
|
|
||
| /// <summary> | ||
| /// Validates a string function call and returns its match type, field and resolved search value. | ||
| /// </summary> | ||
| /// <param name="nodeIn">The function call node.</param> | ||
| /// <returns>The match type, the field being searched and the search value.</returns> | ||
| /// <exception cref="NotSupportedException">The function or its arguments are not supported.</exception> | ||
| public static (LikeMatch Match, SingleValuePropertyAccessNode Field, string Value) GetFieldAndValue(SingleValueFunctionCallNode nodeIn) | ||
| { | ||
| LikeMatch match = nodeIn.Name switch | ||
| { | ||
| CONTAINS => LikeMatch.Contains, | ||
| STARTSWITH => LikeMatch.StartsWith, | ||
| ENDSWITH => LikeMatch.EndsWith, | ||
| _ => throw new NotSupportedException($"The function '{nodeIn.Name}' is not supported.") | ||
| }; | ||
|
|
||
| QueryNode[] arguments = nodeIn.Parameters.ToArray(); | ||
| if (arguments.Length != 2 || Unwrap(arguments[0]) is not SingleValuePropertyAccessNode field) | ||
| { | ||
| throw new NotSupportedException($"The first argument of '{nodeIn.Name}' must be a field."); | ||
| } | ||
|
|
||
| string? value = FoldString(arguments[1]); | ||
| if (string.IsNullOrEmpty(value)) | ||
| { | ||
| throw new NotSupportedException($"The second argument of '{nodeIn.Name}' must be a non-empty string value."); | ||
| } | ||
|
|
||
| return (match, field, value); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// String functions are only supported as conditions in their own right. Comparing their | ||
| /// result, as in <c>startswith(title, 'A') eq true</c>, is rejected rather than translated. | ||
| /// </summary> | ||
| /// <param name="nodeIn">The binary operator node.</param> | ||
| /// <exception cref="NotSupportedException">A function call is an operand of a comparison.</exception> | ||
| public static void ThrowIfFunctionIsCompared(BinaryOperatorNode nodeIn) | ||
| { | ||
| if (nodeIn.OperatorKind is not (BinaryOperatorKind.And or BinaryOperatorKind.Or) && | ||
| (Unwrap(nodeIn.Left) is SingleValueFunctionCallNode || Unwrap(nodeIn.Right) is SingleValueFunctionCallNode)) | ||
| { | ||
| throw new NotSupportedException("A function call cannot be an operand of a comparison; use it directly as a condition."); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Resolves a node to a string when it is a string constant or a concat of string constants. | ||
| /// Returns null for anything else, including null values. | ||
| /// </summary> | ||
| private static string? FoldString(QueryNode node) | ||
|
aaronburtle marked this conversation as resolved.
Resolved
|
||
| { | ||
| switch (Unwrap(node)) | ||
| { | ||
| case ConstantNode { Value: string value }: | ||
| return value; | ||
| case SingleValueFunctionCallNode { Name: CONCAT } concat: | ||
| QueryNode[] parts = concat.Parameters.ToArray(); | ||
| if (parts.Length != 2) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| string? left = FoldString(parts[0]); | ||
| string? right = FoldString(parts[1]); | ||
| return left is null || right is null ? null : left + right; | ||
| default: | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| private static QueryNode Unwrap(QueryNode node) | ||
| { | ||
| while (node is ConvertNode convertNode) | ||
| { | ||
| node = convertNode.Source; | ||
| } | ||
|
|
||
| return node; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.