Populating parameter using Depends() ignores pydantic model config #9118
-
|
I use Environment
Testcasefrom pydantic import BaseModel, Field
from fastapi import Depends, FastAPI
app = FastAPI()
class TestParams(BaseModel):
class Config:
allow_population_by_field_name = True
param: bool = Field(False, alias='alias')
@app.get('/')
async def main(params: TestParams = Depends()):
return params.dict()Expected results$ curl "http://127.0.0.1:8000/?param=true"
{"param":true}
$ curl "http://127.0.0.1:8000/?alias=true"
{"param":true}Actual results$ curl "http://127.0.0.1:8000/?param=true"
{"param":false}
$ curl "http://127.0.0.1:8000/?alias=true"
{"param":true} |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments
-
|
Somehow EDIT: I dig inside much better and doubt it will be easy fix: def get_typed_signature(call: Callable) -> inspect.Signature:
signature = inspect.signature(call)That's how FastAPI getting signature of Model on validation so there is no info about real name. I'll continue researching a bit more but now it looks like quite difficult task :) |
Beta Was this translation helpful? Give feedback.
-
|
Yeah, the problem is there by design. I believe the same issue would be with
|
Beta Was this translation helpful? Give feedback.
-
That's by design?
Not true, I believe. If you have: async def f(p: int = Query(..., alias='q')):
passthen: inspect.signature(f).parameters['p'].default.alias == 'q' |
Beta Was this translation helpful? Give feedback.
-
|
@jrversteegh you right that alias and name here is different things but FastAPI (if I remember correctly this case) redefines initial model and creates duplicate where name equal to alias (if exists) or name. And that why i said "there is no info about real name." since we creating new model that knows nothing about initial one |
Beta Was this translation helpful? Give feedback.
-
I highly doubt that since I'm able to use aliases that aren't valid python variable names like in async def f(page_size: int = Query(..., alias='page[size]')):
pass |
Beta Was this translation helpful? Give feedback.
-
|
For now there is no option in openapi to specify aliases for parameters. |
Beta Was this translation helpful? Give feedback.
For now there is no option in openapi to specify aliases for parameters.
Seems that for now the only option is to duplicate parameter, make both of them optional and manually handle conflict if both are passed or no one is passed.