-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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
Multiple aliases for query parameters #1889
Comments
It probably should accept a |
Yes, my intention was not to break compatibility and make it accept both an |
|
That sucks. |
At first I think you should look/open similar issue for Pydantic. Maybe there some workarounds or it's planned feature. Multiple params forone object seems wrong |
Yea, I agree it's kinda hackish. |
Maybe useful: pydantic/pydantic#324 (2 years old) |
So it doesn't seem like a pydantic solution is gonna happen any time soon, is there any progress on this? |
Opening a ticket with your proposal on the pydantic bug-tracker would probably help. Making a pull request even more so, but asking for some initial feedback on the API would probably be a good idea. |
I think I found a possible solution without modifying pydantic. Not sure if it will be officially merged in but I'll try to submit a PR in the coming days. Hopefully it helps a few people! |
@NoahCardoza can you provide an example? |
Still working out the kinks, but it looks like it's possible since pydantic collects any extra kwargs inside an |
Alright, so after more digging my original statement was incorrect. I was using the @app.get("/")
def get(query: str = Query(None, aliases=['query', 'q'])):
return query However, class MultiAliasablModelField(ModelField):
def __init__(self, *args, aliases: Optional[List[str]] = None, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.aliases = aliases
def __repr_args__(self) -> str:
args = super().__repr_args__()
if self.aliases:
args.append(('aliases', self.aliases))
return args This would require changes to the return annotations for the The I've to to run at the moment, but I'll fork/branch and try to get a complete example ready ASAP. |
Here is a working fix NoahCardoza/fastapi:feature/multiple-aliases! I need to do a little clean up and update the docs etc. You'll also noticed a Anyways, I'm not sure how a field with multiple aliases should properly render an error when it's a required field. For reference, I'm using this code to test: from typing import Optional
from pydantic import Required
from fastapi import FastAPI, Query
app = FastAPI()
@app.get("/")
def get(
question: str = Query(Required, aliases=('q', 'question')),
answer: Optional[int] = Query(None, aliases=('a', 'answer'))
):
return {
'question': question,
'answer': answer,
}
app.setup() And I'm using this script to test: req() {
curl -w "\n" "http://127.0.0.1:8000$1"
}
req
req "/?q=What%27s%20the%20answer%20to%20the%20universe?"
req "/?question=What%27s%20the%20answer%20to%20the%20universe?"
req "/?q=What%27s%20the%20answer%20to%20the%20universe?&a=42"
req "/?question=What%27s%20the%20answer%20to%20the%20universe?&answer=42" Which you can diff against:
|
How's this going on? |
1 similar comment
How's this going on? |
Sorry @vbigbang and @zhqu1148980644, I've become busy with other things at the moment. If I can get back to this I will, but if anyone else wants to take it up from where I left off, feel free! |
Sorry, but how this feature would appear in Swagger/OpenAPI generated by FastAPI? |
@iamthen0ise If I remember correctly, I think that was one of the issues I was stuck on before I ran out of time... I'm not exactly sure. |
This will be possible with Pydantic v2, and once FastAPI is updated for Pydantic v2. 🚀 Because of that, it would actually be more of a Pydantic issue than a FastAPI issue. 🤓
|
Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs. |
But this question by @iamthen0ise is still a FastAPI issue, isn't it? How would this be handled? |
Not sure yet how it would be handled. I haven't worked on that yet. |
I am not sure if this is helpful, but I'd like to share my perspective on this matter. As far as I see there are two kinds of related needs in functionality that come with very different impact on the existing implementation:
I'd argue it's kind of a bummer no. 1 isn't possible. |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
First check
Example
I would like to be able to do this:
Description
Currently to give a query parameter multiple aliases, you need to declare multiple query parameters.
The solution you would like
Being able to give a list of aliases to the
alias
arg of theQuery
class.Of course backwards compatibility won'y break, as
alias
arg would be of typeOptional[Union[str, Iterable[str]]]
.You would still be able to do this:
Describe alternatives you've considered
@Kludex mentioned creating a new argument
aliases
The text was updated successfully, but these errors were encountered: