6

fastapi 中自动生成的 openapi3 怎样设置二级目录

 2 years ago
source link: https://www.v2ex.com/t/825566
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

V2EX  ›  Python

fastapi 中自动生成的 openapi3 怎样设置二级目录

  skyone123 · 4 小时 33 分钟前 · 143 次点击

请问下,fastapi 中自动生成的 openapi3 怎样设置二级目录,多级目录,目前只有一级目录

https://i.bmp.ovh/imgs/2021/12/d6d8eb88c155a402.png

2 条回复    2021-12-31 21:51:09 +08:00

zachlhb      1 小时 37 分钟前 via iPhone

问下你用这个框架如何统一返回格式呢,好像每个接口只能返回模型

skyone123      1 小时 0 分钟前

嗯,对,你先提前定义个 Response 的 model ,然后就可以统一返回了,形如
from typing import Generic, TypeVar, Optional, Union
from pydantic import Field
from pydantic.generics import GenericModel

Data = TypeVar('Data')


class ResultResponse(GenericModel, Generic[Data]):
"""
自定义返回模型,使用 generic-models 定义自定义模型
https://pydantic-docs.helpmanual.io/usage/models/#generic-models
所有返回数据都用如下格式,方便前端统一处理
{
code: 200,
message: '请求成功',
data: None
}
"""
code: int = Field(default=200, description='返回码')
message: str = Field(default='请求成功', description='消息内容')
result: Optional[Data]


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK