| 1 |
from fastapi import Body, FastAPI |
| 2 |
from pydantic import BaseModel |
| 3 |
|
| 4 |
app = FastAPI() |
| 5 |
|
| 6 |
|
| 7 |
class Item(BaseModel): |
| 8 |
name: str |
| 9 |
description: str | None = None |
| 10 |
price: float |
| 11 |
tax: float | None = None |
| 12 |
|
| 13 |
|
| 14 |
@app.put("/items/{item_id}") |
| 15 |
async def update_item( |
| 16 |
*, |
| 17 |
item_id: int, |
| 18 |
item: Item = Body( |
| 19 |
openapi_examples={ |
| 20 |
"normal": { |
| 21 |
"summary": "A normal example", |
| 22 |
"description": "A **normal** item works correctly.", |
| 23 |
"value": { |
| 24 |
"name": "Foo", |
| 25 |
"description": "A very nice Item", |
| 26 |
"price": 35.4, |
| 27 |
"tax": 3.2, |
| 28 |
}, |
| 29 |
}, |
| 30 |
"converted": { |
| 31 |
"summary": "An example with converted data", |
| 32 |
"description": "FastAPI can convert price `strings` to actual `numbers` automatically", |
| 33 |
"value": { |
| 34 |
"name": "Bar", |
| 35 |
"price": "35.4", |
| 36 |
}, |
| 37 |
}, |
| 38 |
"invalid": { |
| 39 |
"summary": "Invalid data is rejected with an error", |
| 40 |
"value": { |
| 41 |
"name": "Baz", |
| 42 |
"price": "thirty five point four", |
| 43 |
}, |
| 44 |
}, |
| 45 |
}, |
| 46 |
), |
| 47 |
): |
| 48 |
results = {"item_id": item_id, "item": item} |
| 49 |
return results |