简体   繁体   English

在 FastAPI 生成的 OpenAPI 文档中更改模式名称

[英]Changing schema name in OpenAPI docs generated by FastAPI

I'm using FastAPI to create backend for my project.我正在使用 FastAPI 为我的项目创建后端。 I have a method that allows to upload a file.我有一个允许上传文件的方法。 I implemented it as follows:我实现它如下:

from fastapi import APIRouter, UploadFile, File

from app.models.schemas.files import FileInResponse

router = APIRouter()


@router.post("", name="files:create-file", response_model=FileInResponse)
async def create(file: UploadFile = File(...)) -> FileInResponse:
    pass

As you can see, I use a dedicated pydantic model for a method result— FileInResponse :如您所见,我使用专用的 pydantic model 作为方法结果FileInResponse

from pathlib import Path

from pydantic import BaseModel


class FileInResponse(BaseModel):
    path: Path

And I follow this naming pattern for models (naming models as <Entity>InCreate , <Entity>InResponse , and so on) throughout the API.我在整个 API 中遵循模型的这种命名模式(将模型命名为<Entity>InCreate<Entity>InResponse等)。 However, I couldn't create a pydantic model with a field of the type File , so I had to declare it directly in the route definition (ie without a model containing it).但是,我无法创建具有File类型字段的 pydantic model ,因此我必须直接在路由定义中声明它(即没有包含它的 model )。 As a result, I have this long auto generated name Body_files_create_file_api_files_post in the OpenAPI docs:结果,我在 OpenAPI 文档中有这个自动生成的长名称Body_files_create_file_api_files_post

生成的 OpenAPI 文档

Is there a way to change the schema name?有没有办法更改架构名称?

If you take a look at response model you see this :如果您查看响应 model 您会看到

It receives the same type you would declare for a Pydantic model atribute, so it can be a Pydantic model, but it can also be, eg a list of Pydantic models, like List[Item].它接收与您为 Pydantic model 属性声明的类型相同的类型,因此它可以是 Pydantic model,但也可以是,例如 Pydantic 模型列表,如 List[Item]。

FastAPI will use this response_model to: FastAPI 将使用这个 response_model 来:

Convert the output data to its type declaration.
Validate the data.
Add a JSON Schema for the response, in the OpenAPI path operation.
Will be used by the automatic documentation systems.

That mean that response model is use to return json not a file like you want to do.这意味着响应 model 用于返回 json 而不是您想要的文件。

I couldn't create a pydantic model with a field of the type File我无法使用 File 类型的字段创建 pydantic model

This is totaly normal这是完全正常的

so I had to declare it directly in the route definition (ie without a model containing it)所以我不得不直接在路由定义中声明它(即没有包含它的 model)

And that's normal too, remember that File is not a python type nor a pydantic specific type so it can't be part of a model inheritating from pydantic BaseModel.这也很正常,请记住 File 不是 python 类型也不是 pydantic 特定类型,因此它不能是继承自 pydantic BaseModel 的 model 的一部分。

To do a file response you can follow the official doc :要进行文件响应,您可以按照官方文档

from fastapi import FastAPI
from fastapi.responses import FileResponse

some_file_path = "large-video-file.mp4"
app = FastAPI()


@app.get("/")
async def main():
    return FileResponse(some_file_path)

if you want to return a file like object (ie file from an instance ofUploadFile ) follow this part of the doc:如果您想返回 object 之类的文件(即来自UploadFile实例的文件),请遵循文档的这一部分:

from fastapi import FastAPI
from fastapi.responses import StreamingResponse

some_file_path = "large-video-file.mp4"
app = FastAPI()


@app.get("/")
def main():
    def iterfile():
        with open(some_file_path, mode="rb") as file_like:
            yield from file_like

    return StreamingResponse(iterfile(), media_type="video/mp4")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM