| 1 |
from fastapi import 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 = 10.5 |
| 12 |
tags: list[str] = [] |
| 13 |
|
| 14 |
|
| 15 |
items = { |
| 16 |
"foo": {"name": "Foo", "price": 50.2}, |
| 17 |
"bar": {"name": "Bar", "description": "The bartenders", "price": 62, "tax": 20.2}, |
| 18 |
"baz": {"name": "Baz", "description": None, "price": 50.2, "tax": 10.5, "tags": []}, |
| 19 |
} |
| 20 |
|
| 21 |
|
| 22 |
@app.get("/items/{item_id}", response_model=Item, response_model_exclude_unset=True) |
| 23 |
async def read_item(item_id: str): |
| 24 |
return items[item_id] |