-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
OptionalQuery extractor #2309
Comments
Sure! Are you up for making a PR? I think it should live in axum-extra like |
Yes, would love to contribute. |
Hello @davidpdrsn, the documentation suggests using a pattern such as |
Seems like a good idea to note the presence of those extractors in that section of the docs, a PR would be appreciated. Long-term, I would like to change how optional extractors work, see #2298 for discussion. |
I can put in a PR.
Nice PR. Only question being, why do you have to keep supporting optional query in Edit: The PR is here: #2801 |
If that issue is resolved as I proposed, then we will remove all of those |
Feature Request
Motivation
Currently, there is no ergonomic way to express the "handler accepts either all query parameters or none" in the handler signature.
Signature like this
results in an error message from the deserialization library on request without query parameters.
Signature like this
works but silently swallows all parsing errors.
Proposal
Add an extractor
OptionalQuery
, similar toOptionalPath
proposed in #1884 that succeeds with None if there are no query parameters present in the request URI, and attempts to deserialize the query parameters to T and return Some(T) on success, rejecting the request on failure.Alternatives
All of the alternatives I can think of require the user of the library to perform verification logic or custom parsing.
Result
as an extractor and then matching onErr
variant will work, but it is much less ergonomic and requires nested match statementsImplementing custom parsing with
RawQuery
extractor is much less ergonomic and will duplicate parsing logic from the library into user code.Defining
QueryParams
like this and adding verification logic onto the struct also seems undesirableThe text was updated successfully, but these errors were encountered: