如何导出Swagger定义文件(它应该是JSON或YAML文件)?我被要求这样做,而且我对Swagger只有粗略的了解.
我们确实有一个端点http://example.com//swagger/ui/index#!看起来像这样(截图不是我们真正的端点,但我不能发布):
版本是api version: v1.
我看不到"导出"按钮.那么我该如何导出呢?
使用 ORM,我想要执行一个 POST 请求,让某些字段具有null值,该值将在数据库中转换为那里指定的默认值。
问题是 OpenAPI (Swagger) docs忽略了默认值None,并且默认情况下仍然提示 a UUID。
from fastapi import FastAPI
from pydantic import BaseModel
from typing import Optional
from uuid import UUID
import uvicorn
class Table(BaseModel):
# ID: Optional[UUID] # the docs show a example UUID, ok
ID: Optional[UUID] = None # the docs still shows a uuid, when it should show a null or valid None value.
app = FastAPI()
@app.post("/table/", response_model=Table)
def create_table(table: Table):
# here we call …Run Code Online (Sandbox Code Playgroud) 例如,我不想在 FastAPI Swagger 文档中公开字符串的正确正则表达式。如何实现这一目标?
我实际上是通过以下方式实现的:
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
return PlainTextResponse(
str("the string does not match the correct regex"), status_code=422
)
Run Code Online (Sandbox Code Playgroud)
但它仅在我从 Postman 调用端点时才起作用,但在 Swagger 中,FastAPI 似乎甚至在到达端点之前就抛出验证错误。
我尝试了很多事情,但似乎没有任何效果。