Skip to content

Body related parameters don't take alias in consideration #10286

@Kludex

Description

@Kludex

Privileged issue

  • I'm @tiangolo or he asked me directly to create an issue here.

Issue Content

The Body, File, and Form parameters don't support alias.

The following snippet demonstrates the issue:

from typing import Annotated

from fastapi import Body, Cookie, FastAPI, Form, UploadFile, File, Query, Header, Path

app = FastAPI()


@app.post("/{path}")
def endpoint(
    path: Annotated[int, Path(alias="PathAlias")],
    cookie: Annotated[int, Cookie(alias="CookieAlias")],
    header: Annotated[int, Header(alias="HeaderAlias")],
    query: Annotated[int, Query(alias="QueryAlias")],
    body: Annotated[int, Body(alias="BodyAlias")],
    form: Annotated[int, Form(alias="FormAlias")],
    file: Annotated[UploadFile, File(alias="FileAlias")],
):
    ...

If you look at the generated Swagger you see that alias is only being used for params.Param related fields i.e. Path, Cookie, etc.

Screenshot 2023-09-20 at 10 04 21

People can overcome this right now using validation_alias as follows:

from typing import Annotated

from fastapi import Body, Cookie, FastAPI, Form, UploadFile, File, Query, Header, Path

app = FastAPI()


@app.post("/{path}")
def endpoint(
    path: Annotated[int, Path(alias="PathAlias")],
    cookie: Annotated[int, Cookie(alias="CookieAlias")],
    header: Annotated[int, Header(alias="HeaderAlias")],
    query: Annotated[int, Query(alias="QueryAlias")],
    body: Annotated[int, Body(validation_alias="BodyAlias")],
    form: Annotated[int, Form(validation_alias="FormAlias")],
    file: Annotated[UploadFile, File(validation_alias="FileAlias")],
):
    ...

In any case, this should be fixed in FastAPI.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions