Fastapi: [BUG] Example is nested when using embed=True and example

Created on 26 May 2020  路  4Comments  路  Source: tiangolo/fastapi

Describe the bug

When using a single Body parameter (with embed=True) and example=, the openapi doc shows a nested entry, which differs with what actually works.

To Reproduce

Following code:

@app.post("/")
def read_root(status: str = Body(..., embed=True, example={'status': 'allGood'}):
    return status

Openapi docs "Example Value"

{
  "status": {
     "status": "allGood"
     }
}

Working request will actually be:
{ "status": "allGood" }

Expected behavior

openapi example should match the actual working request

answered bug

All 4 comments

Just do example='allGood'?

thank you, this does work.
Would this be considered a workaround or is this the proper way to handle this?

Thanks for the help @amacfie ! :bow:

@taoofshawn embed=True would be, more or less, for something simple and quick. If you need to add more custom examples or other things, it would probably be better to declare a Pydantic model with a status field.

You can see more details about adding examples in the docs: https://fastapi.tiangolo.com/tutorial/schema-extra-example/

understood, thanks!

Was this page helpful?
0 / 5 - 0 ratings