-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Add support for
openapi_examples
in all FastAPI parameters (#10152)
* ♻️ Refactor model for OpenAPI Examples to use a reusable TypedDict * ✨ Add support for openapi_examples in parameters * 📝 Add new docs examples for new parameter openapi_examples * 📝 Update docs for Schema Extra to include OpenAPI examples * ✅ Add tests for new source examples, for openapi_examples * ✅ Add tests for openapi_examples corner cases and all parameters * 💡 Tweak and ignore type annotation checks for custom TypedDict
- Loading branch information
Showing
16 changed files
with
1,695 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
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( | ||
openapi_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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from typing import Union | ||
|
||
from fastapi import Body, FastAPI | ||
from pydantic import BaseModel | ||
from typing_extensions import Annotated | ||
|
||
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: Annotated[ | ||
Item, | ||
Body( | ||
openapi_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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from typing import Annotated | ||
|
||
from fastapi import Body, FastAPI | ||
from pydantic import BaseModel | ||
|
||
app = FastAPI() | ||
|
||
|
||
class Item(BaseModel): | ||
name: str | ||
description: str | None = None | ||
price: float | ||
tax: float | None = None | ||
|
||
|
||
@app.put("/items/{item_id}") | ||
async def update_item( | ||
*, | ||
item_id: int, | ||
item: Annotated[ | ||
Item, | ||
Body( | ||
openapi_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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from typing import Annotated, 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: Annotated[ | ||
Item, | ||
Body( | ||
openapi_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 |
Oops, something went wrong.