相关疑难解决方法(0)

如何从Swagger UI导出Swagger JSON/YAML文件?

如何导出Swagger定义文件(它应该是JSON或YAML文件)?我被要求这样做,而且我对Swagger只有粗略的了解.

我们确实有一个端点http://example.com//swagger/ui/index#!看起来像这样(截图不是我们真正的端点,但我不能发布):

在此输入图像描述

版本是api version: v1.

我看不到"导出"按钮.那么我该如何导出呢?

swagger swagger-ui

24
推荐指数
4
解决办法
3万
查看次数

如何使用 FastAPI 在 OpenAPI/Swagger 中记录默认 None/null?

使用 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)

python database swagger openapi fastapi

5
推荐指数
1
解决办法
9647
查看次数

FastAPI:如何获取 OpenAPI 中的自定义验证错误?

例如,我不想在 FastAPI Swagger 文档中公开字符串的正确正则表达式。如何实现这一目标?

招摇验证错误: 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 似乎甚至在到达端点之前就抛出验证错误。

我尝试了很多事情,但似乎没有任何效果。

python swagger openapi fastapi

5
推荐指数
0
解决办法
624
查看次数

标签 统计

swagger ×3

fastapi ×2

openapi ×2

python ×2

database ×1

swagger-ui ×1