-
-
Notifications
You must be signed in to change notification settings - Fork 159
Extensible query string functions #1286
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
Conversation
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
a3eac3c to
d82ea66
Compare
This was referenced Jul 5, 2023
Closed
Codecov Report
@@ Coverage Diff @@
## master #1286 +/- ##
==========================================
+ Coverage 92.97% 93.00% +0.02%
==========================================
Files 255 268 +13
Lines 8259 8776 +517
==========================================
+ Hits 7679 8162 +483
- Misses 580 614 +34
|
…l as a field name
…shape of parsers, making it easier to use them directly from external code
d82ea66 to
38a4a83
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This PR enables you to plug in your own functions in query string parameters. For example, after defining the
isUpperCasefilter function, you can use:Likewise, after you've added the
sumfunction, you can use the following request:or compose with other functions, such as
count:It works for sorting too. After adding the
lengthfunction, the following request can be used:To make this all work, some preliminary changes were needed. First of all, move the existing validation callbacks into the query string parsers so that they can become injectable. Until now, we've had the pubternal
FieldChainRequirementsenum, a set of fuzzy flags to indicate the expected shape of resource field chains (likeowner.firstNameandposts.comments.votes). This has been replaced with a pattern matcher, so that developers can define their own shapes of expected field chains. Such a pattern is a simplified form of a regular expression and is matched against the resource graph. For example, the patternO*Ameans to match zero or more to-one relationships, followed by an attribute. The hard part is producing helpful errors on match failure, which is what the original code did. The query string parsers now track the failure position to make this easier to comprehend. For example:Now fails by reporting the position, including the
^marker to point to the error location:{ "errors": [ { "status": "400", "title": "The specified filter is invalid.", "detail": "Filtering on relationship 'appointments' is not allowed. Failed at position 40: and(equals(showWeekNumbers,'true'),has(^appointments))", "source": { "parameter": "filter" } } ] }As it turns out, this is pretty nice for brace matching as well:
With
FieldChainRequirementsout of the way, the parsers and LINQ builders have been moved out of the *.Internal namespaces. New interfaces enable to plug in your own parts, such asIFilterParserandIWhereClauseBuilder. The built-in implementations are public non-sealed and provide virtual methods to override the needed parts.For example, to implement the
isUpperCasefilter function, you'd:public class IsUpperCaseExpression : FilterExpressionpublic class IsUpperCaseFilterParser : FilterParserand override theParseFiltermethod, delegating to base if the keyword is not "isUpperCase".public class IsUpperCaseWhereClauseBuilder : WhereClauseBuilderand override theDefaultVisitmethod, delegating to your code if the expression type isIsUpperCaseExpression.IsUpperCaseFilterParserandIsUpperCaseWhereClauseBuilderin the IoC container.Working implementations with tests for the functions mentioned above are provided in this PR. See the \test\JsonApiDotNetCoreTests\IntegrationTests\QueryStrings\CustomFunctions directory.
Fixes #1283.
Fixes #1277.
QUALITY CHECKLIST