【发布时间】:2023-01-20 15:20:43
【问题描述】:
该代码在 Postman 中运行良好并提供有效响应但无法生成文档
class Role(str, Enum):
Internal = "internal"
External = "external"
class Info(BaseModel):
id: int
role: Role
class AppInfo(Info):
info: str
@app.post("/api/v1/create", status_code=status.HTTP_200_OK)
async def create(info: Info, apikey: Union[str, None] = Header(str)):
if info:
alias1 = AppInfo(info="Portal Gun", id=123, role=info.role)
alias2 = AppInfo(info="Plumbus", id=123, , role=info.role)
info_dict.append(alias1.dict())
info_dict.append(alias2.dict())
return {"data": info_dict}
else:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Please provide the input"
)
收到错误:
TypeError: Object of type 'type' is not JSON serializable
【问题讨论】:
-
你在运行什么命令?
-
uvicorn app.main:app --reload 我在 app 文件夹中有一个 main.py
-
我怀疑
role: Role行,因为type是 Python 中所有类的类型。
标签: python enums header fastapi pydantic