Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Cannot return response containing pydantic model that includes aliases since FastAPI 0.47.1 #1043

Closed
Labels
bugSomething isn't workingreviewed
@johnanthonyowens

Description

@johnanthonyowens

Starting in 0.47.1, responses fail validation if they include any pydantic model objects that contain field aliases

You can work around this by explicitly calling.dict(by_alias=True) on the top-level response model object before returning it from the path operation function (see the example below). However, this is super unintuitive and makes the use of field aliases painful. In our codebase we use aliases extensively (viaConfig.alias_generator) to expose camelCase in our public APIs while keeping our internal field names snake_case, and this issue is a real hassle.

To Reproduce

  1. Create atest.py file with:
importfastapiimportpydanticimportstarlette.testclientfromtypingimportListapp=fastapi.FastAPI()classItem(pydantic.BaseModel):my_value:int=pydantic.Field(...,alias='myValue')classRootResponse(pydantic.BaseModel):items:List[Item]=pydantic.Field(...)@app.get('/broken_1',response_model=RootResponse)asyncdefbroken_1():# embedded models don't work if they have aliased fieldsreturn {'items': [Item(myValue=5),Item(myValue=10)]}@app.get('/broken_2',response_model=RootResponse)asyncdefbroken_2():# making the top-level response a model instance doesn't helpreturnRootResponse(items=[Item(myValue=5),Item(myValue=10)])@app.get('/workaround',response_model=RootResponse)asyncdefworkaround():# workaround is to build a top-level model and then dict(by_alias=True)returnRootResponse(items=[Item(myValue=5),Item(myValue=10)]).dict(by_alias=True)withstarlette.testclient.TestClient(app)astest_client:forpathin ['/broken_1','/broken_2','/workaround']:try:print('-------------------------------------------')print(path)response=test_client.get(path)exceptExceptionasexc:print(exc)else:print('succeeded')
  1. Runpython3 test.py with FastAPI 0.47.0. All 3 cases succeed.
  2. Runpython3 test.py with FastAPI 0.49.0. Only the "workaround" case succeeds.

Expected behavior

Model classes that include aliased fields (whether the alias is directly specified on the field as in the above example, or using a pydanticConfig.alias_generator) should be usable with the context of a response model.

Environment

  • OS: Linux (Ubuntu 18.04)
  • FastAPI Version: 0.47.1 and above
  • Python version: 3.7, 3.8

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingreviewed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2026 Movatter.jp