Fastapi: [BUG] Openapi example breaks when using multiple dependencies with the same body

Created on 20 Feb 2020  路  2Comments  路  Source: tiangolo/fastapi

If multiple dependencies use the same body parameter, the openapi example gets broken.

Steps to Reproduce

  1. Create a file with:
from enum import Enum

from fastapi import Depends, FastAPI
from pydantic import BaseModel


app = FastAPI()


class Title(str, Enum):
    mr = 'Mr.'
    ms = 'Ms.'
    mrs = 'Mrs.'


class GreetRequest(BaseModel):
    name: str
    title: Title


def get_title(body: GreetRequest):
    return body.title


def get_name(body: GreetRequest):
    return body.name


@app.post('/greet')
def greet(name: str = Depends(get_name), title: Title = Depends(get_title)):
    return f"greetings {title} {name}!"


# For debugging purposes the script can be run directly
if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)
  1. Run the file
  2. Open the browser, go to localhost:8000/docs
  3. The request body example is:
{
  "body": {
    "name": "string",
    "title": "Mr."
  }
}
  1. Try out your endpoint
  2. It fails with a 422 Unprocessable Entity

Expected behavior

The request body example should be:

{
  "name": "string",
  "title": "Mr."
}

Environment

  • OS: Linux / Windows
  • FastAPI Version: 0.48.0
  • Python Version: 3.7.5
bug

Most helpful comment

I think this was recently fixed, please try with the latest version 0.54.1.

I just tried with your example and it seems to be working as expected.

All 2 comments

I think this was recently fixed, please try with the latest version 0.54.1.

I just tried with your example and it seems to be working as expected.

Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues.

Was this page helpful?
0 / 5 - 0 ratings