您可以将几个参数传递给路径操作装饰器以对其进行配置。

:::tips 警告
请注意,这些参数直接传递给路径操作装饰器,而不是传递给路径操作函数。 :::

响应状态码

您可以定义要在路径操作的响应中使用的(HTTP)status_code
您可以直接传递int代码,例如404。
但是,如果您不记得每个数字代码的用途,则可以在status中使用快捷方式常量:

  1. from typing import Set
  2. from fastapi import FastAPI, status
  3. from pydantic import BaseModel
  4. app = FastAPI()
  5. class Item(BaseModel):
  6. name: str
  7. description: str = None
  8. price: float
  9. tax: float = None
  10. tags: Set[str] = []
  11. @app.post("/items/", response_model=Item, status_code=status.HTTP_201_CREATED)
  12. async def create_item(*, item: Item):
  13. return item

该状态代码将在响应中使用,并将添加到OpenAPI架构中。

:::info 你也可以使用from starlette import status
FastAPI提供与starlette.status相同的fastapi.status,以方便开发人员。但它直接来自Starlette。 :::

标签

您可以在路径操作中添加标签,并通过带有str列表的参数标签(通常只有一个str)传递:

  1. from typing import Set
  2. from fastapi import FastAPI
  3. from pydantic import BaseModel
  4. app = FastAPI()
  5. class Item(BaseModel):
  6. name: str
  7. description: str = None
  8. price: float
  9. tax: float = None
  10. tags: Set[str] = []
  11. @app.post("/items/", response_model=Item, tags=["items"])
  12. async def create_item(*, item: Item):
  13. return item
  14. @app.get("/items/", tags=["items"])
  15. async def read_items():
  16. return [{"name": "Foo", "price": 42}]
  17. @app.get("/users/", tags=["users"])
  18. async def read_users():
  19. return [{"username": "johndoe"}]

它们将被添加到OpenAPI架构中,并由自动文档界面使用:

路径操作配置 - 图1

总结和说明

你可以添加一个summarydescription

  1. from typing import Set
  2. from fastapi import FastAPI
  3. from pydantic import BaseModel
  4. app = FastAPI()
  5. class Item(BaseModel):
  6. name: str
  7. description: str = None
  8. price: float
  9. tax: float = None
  10. tags: Set[str] = []
  11. @app.post(
  12. "/items/",
  13. response_model=Item,
  14. summary="Create an item",
  15. description="Create an item with all the information, name, description, price, tax and a set of unique tags",
  16. )
  17. async def create_item(*, item: Item):
  18. return item

来自文档字符串的描述

由于描述可能会很长并且涵盖多行,因此您可以在函数docstring中声明路径操作描述,FastAPI将从那里读取它。
您可以在文档字符串中编写Markdown,它将被正确解释和显示(考虑到文档字符串缩进)。

  1. from typing import Set
  2. from fastapi import FastAPI
  3. from pydantic import BaseModel
  4. app = FastAPI()
  5. class Item(BaseModel):
  6. name: str
  7. description: str = None
  8. price: float
  9. tax: float = None
  10. tags: Set[str] = []
  11. @app.post("/items/", response_model=Item, summary="Create an item")
  12. async def create_item(*, item: Item):
  13. """
  14. Create an item with all the information:
  15. - **name**: each item must have a name
  16. - **description**: a long description
  17. - **price**: required
  18. - **tax**: if the item doesn't have tax, you can omit this
  19. - **tags**: a set of unique tag strings for this item
  20. """
  21. return item

它将在交互式文档中使用:
路径操作配置 - 图2

响应说明

您可以使用参数response_description指定响应描述:

  1. from typing import Set
  2. from fastapi import FastAPI
  3. from pydantic import BaseModel
  4. app = FastAPI()
  5. class Item(BaseModel):
  6. name: str
  7. description: str = None
  8. price: float
  9. tax: float = None
  10. tags: Set[str] = []
  11. @app.post(
  12. "/items/",
  13. response_model=Item,
  14. summary="Create an item",
  15. response_description="The created item",
  16. )
  17. async def create_item(*, item: Item):
  18. """
  19. Create an item with all the information:
  20. - **name**: each item must have a name
  21. - **description**: a long description
  22. - **price**: required
  23. - **tax**: if the item doesn't have tax, you can omit this
  24. - **tags**: a set of unique tag strings for this item
  25. """
  26. return item


:::tips 请注意,response_description特别是指响应,description通常是指路径操作。 :::

:::info OpenAPI指定每个路径操作都需要响应描述。
因此,如果您不提供任何一种,FastAPI将自动生成“成功响应”之一。 :::

路径操作配置 - 图3

弃用路径操作

如果需要将路径操作标记为已弃用,但不删除它,则传递不建议使用的参数:

from fastapi import FastAPI

app = FastAPI()


@app.get("/items/", tags=["items"])
async def read_items():
    return [{"name": "Foo", "price": 42}]


@app.get("/users/", tags=["users"])
async def read_users():
    return [{"username": "johndoe"}]


@app.get("/elements/", tags=["items"], deprecated=True)
async def read_elements():
    return [{"item_id": "Foo"}]

在交互式文档中,它将明确标记为不推荐使用:

路径操作配置 - 图4
检查不赞成和不赞成使用的路径操作的样子:

路径操作配置 - 图5

概括

通过将参数传递给路径操作修饰符,可以轻松地为路径操作配置和添加元数据。