如何在 FastAPI 中为 UploadFile 创建 OpenAPI 模式?

How to create an OpenAPI schema for an UploadFile in FastAPI?

FastAPI 在 OpenAPI 规范中自动为 UploadFile 个参数生成一个架构。

例如,这段代码:

from fastapi import FastAPI, File, UploadFile

app = FastAPI()


@app.post("/uploadfile/")
async def create_upload_file(file: UploadFile = File(..., description="The file")):
    return {"filename": file.filename}

将根据 OpenAPI 规范中的 components:schemas 生成此架构:

{
    "Body_create_upload_file_uploadfile__post": {
        "title": "Body_create_upload_file_uploadfile__post",
        "required":["file"],
        "type":"object",
        "properties":{
            "file": {"title": "File", "type": "string", "description": "The file","format":"binary"}
        }
    }
}

如何明确指定 UploadFiles 的架构(或至少指定其名称)?

我已阅读 FastAPIs 文档并搜索了问题跟踪器,但一无所获。

我在 FastAPI#1442 上回答了这个问题,但为了防止其他人偶然发现这个问题,这里是从上面链接的 post 中复制并粘贴的:

经过一些调查,这是可能的,但它需要一些猴子补丁。使用给定的示例 here,解决方案如下所示:

from fastapi import FastAPI, File, UploadFile
from typing import Callable

app = FastAPI()

@app.post("/files/")
async def create_file(file: bytes = File(...)):
    return {"file_size": len(file)}

@app.post("/uploadfile/")
async def create_upload_file(file: UploadFile = File(...)):
    return {"filename": file.filename}

def update_schema_name(app: FastAPI, function: Callable, name: str) -> None:
    """
    Updates the Pydantic schema name for a FastAPI function that takes
    in a fastapi.UploadFile = File(...) or bytes = File(...).

    This is a known issue that was reported on FastAPI#1442 in which
    the schema for file upload routes were auto-generated with no
    customization options. This renames the auto-generated schema to
    something more useful and clear.

    Args:
        app: The FastAPI application to modify.
        function: The function object to modify.
        name: The new name of the schema.
    """
    for route in app.routes:
        if route.endpoint is function:
            route.body_field.type_.__name__ = name
            break

update_schema_name(app, create_file, "CreateFileSchema")
update_schema_name(app, create_upload_file, "CreateUploadSchema")

您可以编辑 OpenAPI 架构本身。我更喜欢将这些模式移动到路径中(因为它们对于每个路径都是唯一的):

from fastapi import FastAPI, File, UploadFile
from fastapi.openapi.utils import get_openapi


app = FastAPI()


@app.post("/uploadfile/")
async def create_upload_file(file1: UploadFile = File(...), file2: UploadFile = File(...)):
    pass


def custom_openapi():
    if app.openapi_schema:
        return app.openapi_schema
    openapi_schema = get_openapi(
        title="Custom title",
        version="2.5.0",
        description="This is a very custom OpenAPI schema",
        routes=app.routes,
    )
    # Move autogenerated Body_ schemas, see https://github.com/tiangolo/fastapi/issues/1442
    for path in openapi_schema["paths"].values():
        for method_data in path.values():
            if "requestBody" in method_data:
                for content_type, content in method_data["requestBody"]["content"].items():
                    if content_type == "multipart/form-data":
                        schema_name = content["schema"]["$ref"].lstrip("#/components/schemas/")
                        schema_data = openapi_schema["components"]["schemas"].pop(schema_name)
                        content["schema"] = schema_data
    app.openapi_schema = openapi_schema
    return app.openapi_schema

app.openapi = custom_openapi