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