9 - Declare Request Example Data

9 - Declare Request Example Data#

class Item3(BaseModel):
    name: str
    description: str | None = None
    price: float
    tax: float | None = None

    model_config = {
        "json_schema_extra": {
            "examples": [
                {
                    "name": "Foo",
                    "description": "A very nice Item",
                    "price": 35.4,
                    "tax": 3.2,
                }
            ]
        }
    }

@app.put("/items12/{item_id}")
async def update_item(item_id: int, item: Item3):
    results = {"item_id": item_id, "item": item}
    return results

The data in model_config will be added as an example in the OpenAPI and Swagger documentation.

class Item3(BaseModel):
    name: str = Field(examples=["Pikachu", "Axew"])
    description: str | None = Field(default=None, examples=["The 25th Pokemon in the Pokedex."])
    price: float = Field(examples=[35.4])
    tax: float | None = Field(default=None, examples=[3.2])

The examples attribute can also be used to add examples to the schema when using Field, Path, Query, Body, Header, Form, File, and Cookie.