1from typing import Optional
2
3from fastapi import 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: float = 10.5
14
15
16items = {
17    "foo": {"name": "Foo", "price": 50.2},
18    "bar": {"name": "Bar", "description": "The Bar fighters", "price": 62, "tax": 20.2},
19    "baz": {
20        "name": "Baz",
21        "description": "There goes my baz",
22        "price": 50.2,
23        "tax": 10.5,
24    },
25}
26
27
28@app.get(
29    "/items/{item_id}/name",
30    response_model=Item,
31    response_model_include=["name", "description"],
32)
33async def read_item_name(item_id: str):
34    return items[item_id]
35
36
37@app.get("/items/{item_id}/public", response_model=Item, response_model_exclude=["tax"])
38async def read_item_public_data(item_id: str):
39    return items[item_id]
40