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
Enhancement: route_reverse
does not parse query parameters
#2650
Comments
I'm not saying we won't add this, but for your example there's actually a better way to achieve this: from litestar import Litestar, get, Request
@get("/", name="hello_world")
async def hello_world(request: Request, p: int = 0, q: int = 0) -> str:
query = request.query_params
query["q"] = q + 1
path = request.url.with_replacements(query=query)
return f"Hello, World at {path}"
app = Litestar([hello_world]) Conceptually, the reason why your approach doesn't work is because |
In flask's |
I'd like to add to the request for this. I recently had a use case for a url that had both path parameters and query parameters. I ended up monkey patching route_reverse (I still need to check if I have to monkey patch url_for too) to allow route_reverse to allow for both query_parameters and path_parameters. I'm sure there are significantly betters ways to do this but here is my monkey patch:
|
We have the option of trying to migrate to a pattern like this, which allows us to be specific about where the supplied components live in the url, and also allow us to have names in the function signature that might conflict with path/query parameter names: url = route_reverse("my_route", path={"scheme": "something"}, query={"anchor": "something"}, scheme="https", anchor="page_section") Or, we can head in the same direction that flask's I feel like 1st is more correct, but maybe less convenient to use while 2nd is less perfect but more convenient to use. |
my vote would be for essentially the first proposal, although I would suggest changing the first parameters name from name to route_name to avoid collisions with common parameters, as well as not using the names path and query directly (using either _path _query or path_parameters query_parameters). to retain backward comparability I would retain **kwargs as only path parameters. |
Summary
The function
route_reverse
does not parse query parameters, adding that would be very useful.Basic Example
The following example has a single router at
/
with two query parametersp
andq
but theroute_reverse
method does not add them.A request to
/
returns justHello, World at /
which is not correct, it should returnHello, World at /?q=1
. A request to/?p=1
should returnHello, World at /?p=1&q=1
, i.e. the function should deal gracefully with the default values as well.Drawbacks and Impact
Maybe people rely on the method not returning the query params. A flag controlling the removal of query parameters could be added for them.
Unresolved questions
No response
Note
While we are open for sponsoring on GitHub Sponsors and
OpenCollective, we also utilize Polar.sh to engage in pledge-based sponsorship.
Check out all issues funded or available for funding on our Polar.sh Litestar dashboard
The text was updated successfully, but these errors were encountered: