-
-
Notifications
You must be signed in to change notification settings - Fork 931
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
Make the URL.include_query_params() to support multiple query string parameters with the same name #1738
Conversation
…with the same name
b650034
to
a3e4dba
Compare
@@ -133,8 +133,17 @@ def replace(self, **kwargs: typing.Any) -> "URL": | |||
components = self.components._replace(**kwargs) | |||
return self.__class__(components.geturl()) | |||
|
|||
def include_query_params(self, **kwargs: typing.Any) -> "URL": | |||
def include_query_params( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And how do you include a query param called "items"? 👀
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can think of about 3 solutions to this issue:
a. use positional-only parameters: def include_query_params(self, items, /, **kwargs):
- however, that syntax only works in Python 3.8+, and we still want to support Python 3.7;
b. use an *args parameter (similar to ImmutableMultiDict
__init__
): def include_query_params(self, *args, **kwargs):
- that looks like a fine solution, even if the signature becomes a bit harder to read;
c. define a new method, e.g. def append_query_params(self, items):
- that solution also looks fine to me but makes the API a bit heavier;
An intermediary solution would be to define the API as in (b), but raise an error if more than one unnamed argument is passed, so that a migration to (a) becomes possible once support for Python 3.7 can be dropped;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a. use positional-only parameters: def include_query_params(self, items, /, **kwargs): - however, that syntax only works in Python 3.8+, and we still want to support Python 3.7;
We could do:
def include_query_params(self, __items, **kwargs):
This makes __items
positional only as far as type checkers are concerned (see https://peps.python.org/pep-0484/#positional-only-arguments) and at runtime you just can't have a query parameter with the name __items
, which I hope no one is using. Then when Python 3.7 because the minimum supported version we can enforce it at runtime with your version
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the target API is to use a positional parameter, I would rather switch to the def append_query_items(self, *args, **kwargs):
syntax and check inside the method body that len(items) < 2
- that is, I prefer my breaking changes to be impossible rather than extremely unlikely. (E.g. there is also potential for someone who does not use a type checker to simply call the function with __items=[]
, which would break once __items
was replaced with the /
syntax)
However, that is a question of API design, and I do not know what policy (if any) Starlette has for such API changes.
I've applied suggestions of @adriangb. Take a look. |
I think it would make sense to include |
This is also achievable with joining items outside the |
@lexabug I would like to take a step back and try to clarify the original use case. It sounds like you are using |
The original use case that lead me to this suggestion is: I have a web service that works like an API gateway, so it receives a specific requests, processes it with different middlewares and then the request is sent to a target service. Sometimes original URLs may contain query string parameters representing arrays/lists like |
Could you use an external (i.e. non-Starlette) library to do the URL parsing/building, and then hand it off to Starlette? URL parsing is complicated and full of dragons, I would be a bit concern that this sort of change would set the precedent for Starlette providing this functionality (currently it only really provides the minimum required for other functionality in Starlette to work). |
The proposed solution have some drawbacks:
I would like to propose to explore an alternative, where url.include_query_params(page=1, search='my query', tags=['tag1', 'tag2', 'tag3'])
# ?page=1&search=my%20query&tags=tag1&tags=tag2&tags=tag3 Also, keys in url.include_query_params({
'page': 1,
'search': 'my query',
'tags[]': ['tag1', 'tag2', 'tag3'],
})
# ?page=1&search=my%20query&tags[]=tag1&tags[]=tag2&tags[]=tag3
url.replace_query_params({
'page': 2,
})
# ?page=2&search=my%20query&tags[]=tag1&tags[]=tag2&tags[]=tag3 This is the most flexible solution of all I know, but it is the most complicated and definitely a breaking change. It may make sense to introduce the third method Another point worth to mention is that there is no any common naming convention for multiparams exists. Some frameworks expect |
@adriangb while it is achievable by extra coding, I am sure that URL manipulation is one of the basic features of web frameworks and should be in the Starlette's core. We already have |
Do Django and/or Flask have in-depth URL manipulation utilities? |
I don't know. What I wanted to say is that if Starlette provides a tool to manipulate URLs it should be complete. Lists in query parameters are a pretty common thing. |
Given all the discussions and back and forths about |
After thinking a lot, I agree that |
How did you folks overcome this? Is there still a need for it? 👀 |
I haven't found anything better than this yet. |
2 lines... Problem solved? 👀 |
In templates it is very inconvenient to do like this. |
Do you still think the append method is the best solution here? |
Yes, it solves the problem. |
PR welcome for Thanks for the discussion everybody, and the PR @lexabug . 🙏 |
The
URL.include_query_params()
overwrites query string parameters with the same name. For example:So the
new_url
will be:Which is incorrect. The specification of the URLs allows multiple query string parameters with the same name, so the backend that should process that query string must correctly treat such parameters as arrays/lists.
This change adds a new optional argument to the
URL.include_query_params()
that is handled a container of query string parameters and those parameters are appended to the original query string.