-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
Body
related parameters don't take alias
in consideration
#10286
Comments
When I use only validation_alias in Form I can see alias in Swagger, but the validation is not working well. I think issue is as FastAPI pass validation_alias to FieldInfo of Pydantic v2 in super().init () inside class Body |
Here is one way to fix this issue in FastAPI:
This would involve:
For example, raise an exception if they don't match or take precedence of one over the other.
This would fix the issue so |
I request a PR #10319 to fix it. |
It looks like this behavior might extend to from fastapi import Query, Path, Header, Cookie
from pydantic import BaseModel, ConfigDict, Field
class ForbidExtra(BaseModel):
model_config = ConfigDict(extra="forbid")
class FieldModel(ForbidExtra):
type_: str | None = Field(default=None, alias="type_alias")
class QueryModel(ForbidExtra):
type_: str | None = Query(default=None, alias="type_alias")
class CookieModel(ForbidExtra):
type_: str | None = Cookie(default=None, alias="type_alias")
class HeaderModel(ForbidExtra):
type_: str | None = Header(default=None, alias="type_alias")
class PathModel(ForbidExtra):
type_: str | None = Path(alias="type_alias")
field = FieldModel(type_alias="alias") # This behaves as expected.
# But these raise ValidationError:
query = QueryModel(type_alias="alias")
cookie = CookieModel(type_alias="alias")
header = HeaderModel(type_alias="alias")
path = PathModel(type_alias="alias") |
@harol97, the workaround didn't work for me, am I doing something wrong?
|
@hruzeda you could first do: |
Hi, as @Kludex told me to watch this issue I wanted to make sure if I apply the workaround correctly as it is not (maybe no longer, not 100% sure on that) working. The context you can find in this discussion: #11004 I tried: async def upload_docs(
files: list[UploadFile] = File(..., validation_alias="file")
) -> dict: and this async def upload_docs(
files: list[UploadFile] = File(..., alias="file", validation_alias="file")
) -> dict: without success. I want my multipart form data file upload to allow for either file or files in the post body to be compatible with some legacy systems. |
Privileged issue
Issue Content
The
Body
,File
, andForm
parameters don't supportalias
.The following snippet demonstrates the issue:
If you look at the generated Swagger you see that
alias
is only being used forparams.Param
related fields i.e.Path
,Cookie
, etc.People can overcome this right now using
validation_alias
as follows:In any case, this should be fixed in FastAPI.
The text was updated successfully, but these errors were encountered: