-
First check
ExampleI would like to be able to do this: from fastapi import FastAPI, Query
app = FastAPI()
@app.get("/")
def get(query: Optional[str] = Query(None, alias=['q', 'query']):
return query DescriptionCurrently to give a query parameter multiple aliases, you need to declare multiple query parameters. The solution you would likeBeing able to give a list of aliases to the Query(alias=['q', 'query']) Of course backwards compatibility won'y break, as Query(alias='q') Describe alternatives you've considered@Kludex mentioned creating a new argument Query(aliases=['q', 'query']) |
Beta Was this translation helpful? Give feedback.
Replies: 24 comments 1 reply
-
It probably should accept a |
Beta Was this translation helpful? Give feedback.
-
Yes, my intention was not to break compatibility and make it accept both an |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
That sucks. |
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
Yea, I agree it's kinda hackish. |
Beta Was this translation helpful? Give feedback.
-
Maybe useful: pydantic/pydantic#324 (2 years old) |
Beta Was this translation helpful? Give feedback.
-
So it doesn't seem like a pydantic solution is gonna happen any time soon, is there any progress on this? |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
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! |
Beta Was this translation helpful? Give feedback.
-
@NoahCardoza can you provide an example? |
Beta Was this translation helpful? Give feedback.
-
Still working out the kinks, but it looks like it's possible since pydantic collects any extra kwargs inside an |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
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:
|
Beta Was this translation helpful? Give feedback.
-
How's this going on? |
Beta Was this translation helpful? Give feedback.
-
How's this going on? |
Beta Was this translation helpful? Give feedback.
-
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! |
Beta Was this translation helpful? Give feedback.
-
Sorry, but how this feature would appear in Swagger/OpenAPI generated by FastAPI? |
Beta Was this translation helpful? Give feedback.
-
@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. |
Beta Was this translation helpful? Give feedback.
-
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. 🤓
|
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
But this question by @iamthen0ise is still a FastAPI issue, isn't it? How would this be handled? |
Beta Was this translation helpful? Give feedback.
-
Not sure yet how it would be handled. I haven't worked on that yet. |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
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. 🤓