Add support for adding multiple examples in request bodies and path, query, cookie, and header params (#1267)

Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
This commit is contained in:
Austin Orr
2021-05-05 11:20:56 -07:00
committed by GitHub
parent 3e32eb55f0
commit e10a4375f9
9 changed files with 1220 additions and 24 deletions

View File

@@ -0,0 +1,52 @@
from typing import Optional
from fastapi import Body, FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
description: Optional[str] = None
price: float
tax: Optional[float] = None
@app.put("/items/{item_id}")
async def update_item(
*,
item_id: int,
item: Item = Body(
...,
examples={
"normal": {
"summary": "A normal example",
"description": "A **normal** item works correctly.",
"value": {
"name": "Foo",
"description": "A very nice Item",
"price": 35.4,
"tax": 3.2,
},
},
"converted": {
"summary": "An example with converted data",
"description": "FastAPI can convert price `strings` to actual `numbers` automatically",
"value": {
"name": "Bar",
"price": "35.4",
},
},
"invalid": {
"summary": "Invalid data is rejected with an error",
"value": {
"name": "Baz",
"price": "thirty five point four",
},
},
},
),
):
results = {"item_id": item_id, "item": item}
return results