mirror of
https://github.com/fastapi/fastapi.git
synced 2026-03-24 09:44:01 -04:00
✨ Add support for adding multiple examples in request bodies and path, query, cookie, and header params (#1267)
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
This commit is contained in:
52
docs_src/schema_extra_example/tutorial004.py
Normal file
52
docs_src/schema_extra_example/tutorial004.py
Normal file
@@ -0,0 +1,52 @@
|
||||
from typing import Optional
|
||||
|
||||
from fastapi import Body, FastAPI
|
||||
from pydantic import BaseModel
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
class Item(BaseModel):
|
||||
name: str
|
||||
description: Optional[str] = None
|
||||
price: float
|
||||
tax: Optional[float] = None
|
||||
|
||||
|
||||
@app.put("/items/{item_id}")
|
||||
async def update_item(
|
||||
*,
|
||||
item_id: int,
|
||||
item: Item = Body(
|
||||
...,
|
||||
examples={
|
||||
"normal": {
|
||||
"summary": "A normal example",
|
||||
"description": "A **normal** item works correctly.",
|
||||
"value": {
|
||||
"name": "Foo",
|
||||
"description": "A very nice Item",
|
||||
"price": 35.4,
|
||||
"tax": 3.2,
|
||||
},
|
||||
},
|
||||
"converted": {
|
||||
"summary": "An example with converted data",
|
||||
"description": "FastAPI can convert price `strings` to actual `numbers` automatically",
|
||||
"value": {
|
||||
"name": "Bar",
|
||||
"price": "35.4",
|
||||
},
|
||||
},
|
||||
"invalid": {
|
||||
"summary": "Invalid data is rejected with an error",
|
||||
"value": {
|
||||
"name": "Baz",
|
||||
"price": "thirty five point four",
|
||||
},
|
||||
},
|
||||
},
|
||||
),
|
||||
):
|
||||
results = {"item_id": item_id, "item": item}
|
||||
return results
|
||||
Reference in New Issue
Block a user