模式的额外信息 - 例子

您可以在JSON模式中定义额外的信息。

一个常见的用例是添加一个将在文档中显示的example

有几种方法可以声明额外的 JSON 模式信息。

Pydantic schema_extra

您可以使用 Configschema_extra 为Pydantic模型声明一个示例,如Pydantic 文档:定制 Schema中所述:

Python 3.10+Python 3.8+

  1. from fastapi import FastAPI
  2. from pydantic import BaseModel
  3. app = FastAPI()
  4. class Item(BaseModel):
  5. name: str
  6. description: str | None = None
  7. price: float
  8. tax: float | None = None
  9. model_config = {
  10. "json_schema_extra": {
  11. "examples": [
  12. {
  13. "name": "Foo",
  14. "description": "A very nice Item",
  15. "price": 35.4,
  16. "tax": 3.2,
  17. }
  18. ]
  19. }
  20. }
  21. @app.put("/items/{item_id}")
  22. async def update_item(item_id: int, item: Item):
  23. results = {"item_id": item_id, "item": item}
  24. return results
  1. from typing import Union
  2. from fastapi import FastAPI
  3. from pydantic import BaseModel
  4. app = FastAPI()
  5. class Item(BaseModel):
  6. name: str
  7. description: Union[str, None] = None
  8. price: float
  9. tax: Union[float, None] = None
  10. model_config = {
  11. "json_schema_extra": {
  12. "examples": [
  13. {
  14. "name": "Foo",
  15. "description": "A very nice Item",
  16. "price": 35.4,
  17. "tax": 3.2,
  18. }
  19. ]
  20. }
  21. }
  22. @app.put("/items/{item_id}")
  23. async def update_item(item_id: int, item: Item):
  24. results = {"item_id": item_id, "item": item}
  25. return results

这些额外的信息将按原样添加到输出的JSON模式中。

Field 的附加参数

Field, Path, Query, Body 和其他你之后将会看到的工厂函数,你可以为JSON 模式声明额外信息,你也可以通过给工厂函数传递其他的任意参数来给JSON 模式声明额外信息,比如增加 example:

Python 3.10+Python 3.8+

  1. from fastapi import FastAPI
  2. from pydantic import BaseModel, Field
  3. app = FastAPI()
  4. class Item(BaseModel):
  5. name: str = Field(examples=["Foo"])
  6. description: str | None = Field(default=None, examples=["A very nice Item"])
  7. price: float = Field(examples=[35.4])
  8. tax: float | None = Field(default=None, examples=[3.2])
  9. @app.put("/items/{item_id}")
  10. async def update_item(item_id: int, item: Item):
  11. results = {"item_id": item_id, "item": item}
  12. return results
  1. from typing import Union
  2. from fastapi import FastAPI
  3. from pydantic import BaseModel, Field
  4. app = FastAPI()
  5. class Item(BaseModel):
  6. name: str = Field(examples=["Foo"])
  7. description: Union[str, None] = Field(default=None, examples=["A very nice Item"])
  8. price: float = Field(examples=[35.4])
  9. tax: Union[float, None] = Field(default=None, examples=[3.2])
  10. @app.put("/items/{item_id}")
  11. async def update_item(item_id: int, item: Item):
  12. results = {"item_id": item_id, "item": item}
  13. return results

Warning

请记住,传递的那些额外参数不会添加任何验证,只会添加注释,用于文档的目的。

Body 额外参数

你可以通过传递额外信息给 Field 同样的方式操作Path, Query, Body等。

比如,你可以将请求体的一个 example 传递给 Body:

Python 3.10+Python 3.9+Python 3.8+Python 3.10+ non-AnnotatedPython 3.8+ non-Annotated

  1. from typing import Annotated
  2. from fastapi import Body, FastAPI
  3. from pydantic import BaseModel
  4. app = FastAPI()
  5. class Item(BaseModel):
  6. name: str
  7. description: str | None = None
  8. price: float
  9. tax: float | None = None
  10. @app.put("/items/{item_id}")
  11. async def update_item(
  12. item_id: int,
  13. item: Annotated[
  14. Item,
  15. Body(
  16. examples=[
  17. {
  18. "name": "Foo",
  19. "description": "A very nice Item",
  20. "price": 35.4,
  21. "tax": 3.2,
  22. }
  23. ],
  24. ),
  25. ],
  26. ):
  27. results = {"item_id": item_id, "item": item}
  28. return results
  1. from typing import Annotated, Union
  2. from fastapi import Body, FastAPI
  3. from pydantic import BaseModel
  4. app = FastAPI()
  5. class Item(BaseModel):
  6. name: str
  7. description: Union[str, None] = None
  8. price: float
  9. tax: Union[float, None] = None
  10. @app.put("/items/{item_id}")
  11. async def update_item(
  12. item_id: int,
  13. item: Annotated[
  14. Item,
  15. Body(
  16. examples=[
  17. {
  18. "name": "Foo",
  19. "description": "A very nice Item",
  20. "price": 35.4,
  21. "tax": 3.2,
  22. }
  23. ],
  24. ),
  25. ],
  26. ):
  27. results = {"item_id": item_id, "item": item}
  28. return results
  1. from typing import Union
  2. from fastapi import Body, FastAPI
  3. from pydantic import BaseModel
  4. from typing_extensions import Annotated
  5. app = FastAPI()
  6. class Item(BaseModel):
  7. name: str
  8. description: Union[str, None] = None
  9. price: float
  10. tax: Union[float, None] = None
  11. @app.put("/items/{item_id}")
  12. async def update_item(
  13. item_id: int,
  14. item: Annotated[
  15. Item,
  16. Body(
  17. examples=[
  18. {
  19. "name": "Foo",
  20. "description": "A very nice Item",
  21. "price": 35.4,
  22. "tax": 3.2,
  23. }
  24. ],
  25. ),
  26. ],
  27. ):
  28. results = {"item_id": item_id, "item": item}
  29. return results

Tip

尽可能选择使用 Annotated 的版本。

  1. from fastapi import Body, FastAPI
  2. from pydantic import BaseModel
  3. app = FastAPI()
  4. class Item(BaseModel):
  5. name: str
  6. description: str | None = None
  7. price: float
  8. tax: float | None = None
  9. @app.put("/items/{item_id}")
  10. async def update_item(
  11. item_id: int,
  12. item: Item = Body(
  13. examples=[
  14. {
  15. "name": "Foo",
  16. "description": "A very nice Item",
  17. "price": 35.4,
  18. "tax": 3.2,
  19. }
  20. ],
  21. ),
  22. ):
  23. results = {"item_id": item_id, "item": item}
  24. return results

Tip

尽可能选择使用 Annotated 的版本。

  1. from typing import Union
  2. from fastapi import Body, FastAPI
  3. from pydantic import BaseModel
  4. app = FastAPI()
  5. class Item(BaseModel):
  6. name: str
  7. description: Union[str, None] = None
  8. price: float
  9. tax: Union[float, None] = None
  10. @app.put("/items/{item_id}")
  11. async def update_item(
  12. item_id: int,
  13. item: Item = Body(
  14. examples=[
  15. {
  16. "name": "Foo",
  17. "description": "A very nice Item",
  18. "price": 35.4,
  19. "tax": 3.2,
  20. }
  21. ],
  22. ),
  23. ):
  24. results = {"item_id": item_id, "item": item}
  25. return results

文档 UI 中的例子

使用上面的任何方法,它在 /docs 中看起来都是这样的:

模式的额外信息 - 例子 - 图1

技术细节

关于 exampleexamples

JSON Schema在最新的一个版本中定义了一个字段 examples ,但是 OpenAPI 基于之前的一个旧版JSON Schema,并没有 examples.

所以 OpenAPI为了相似的目的定义了自己的 example (使用 example, 而不是 examples), 这也是文档 UI 所使用的 (使用 Swagger UI).

所以,虽然 example 不是JSON Schema的一部分,但它是OpenAPI的一部分,这将被文档UI使用。

其他信息

同样的方法,你可以添加你自己的额外信息,这些信息将被添加到每个模型的JSON模式中,例如定制前端用户界面,等等。