mirror of
https://github.com/fastapi/fastapi.git
synced 2025-12-30 09:39:20 -05:00
* 📝 Add docs recommending Union over Optional * 📝 Update docs recommending Union over Optional * 📝 Update source examples for docs, recommend Union over Optional * 📝 Update highlighted lines with updated source examples * 📝 Update highlighted lines in Markdown with recent code changes * 📝 Update docs, use Union instead of Optional * ♻️ Update source examples to recommend Union over Optional * 🎨 Update highlighted code in Markdown after moving from Optional to Union
30 lines
567 B
Python
30 lines
567 B
Python
from typing import Union
|
|
|
|
from fastapi import Body, FastAPI
|
|
from pydantic import BaseModel
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
class Item(BaseModel):
|
|
name: str
|
|
description: Union[str, None] = None
|
|
price: float
|
|
tax: Union[float, None] = None
|
|
|
|
|
|
@app.put("/items/{item_id}")
|
|
async def update_item(
|
|
item_id: int,
|
|
item: Item = Body(
|
|
example={
|
|
"name": "Foo",
|
|
"description": "A very nice Item",
|
|
"price": 35.4,
|
|
"tax": 3.2,
|
|
},
|
|
),
|
|
):
|
|
results = {"item_id": item_id, "item": item}
|
|
return results
|