相关疑难解决方法(0)

Swagger - 指定可选对象属性或多个响应

我有一个API,要么成功返回以下响应:

{
    "result": "success",
    "filename": "my-filename.txt"
}
Run Code Online (Sandbox Code Playgroud)

失败后如下:

{
    "result": "error",
    "message": "You must specify the xxx parameter."
}
Run Code Online (Sandbox Code Playgroud)

仅当请求成功时才指定filename属性,但如果出现错误则提供消息.这意味着消息和文件名属性是"可选的",但结果属性是必需的.

我尝试在定义中定义此响应对象,如下所示:

"my_response_object": {
    "type": "object",
    "properties": {
        "result": {
            "type": "string",
            "description": "value of 'success' or 'error', indicated whether was successful",
            "required": true
        },
        "message": {
            "type": "string",
            "description": "an error message if there was an issue",
            "required": false
        },
        "filename": {
            "type": "string",
            "description": "the filename to return if the request was successful",
            "required": false
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但似乎swagger不喜欢"required"属性,并将显示以下错误消息: …

swagger

30
推荐指数
1
解决办法
4万
查看次数

Swagger 文档 - 单个 http 响应的多个错误代码

我正在尝试记录一个始终返回自定义响应代码和相关描述的模型。对于单个 HTTP 响应代码,可能有多个自定义响应代码。例如,400 响应可能包括:

+===========+======+=============+
| HTTP Code | Code | Description |
+===========+======+=============+
|       400 |    1 | Error 1     |
+-----------+------+-------------+
|       400 |    2 | Error 2     |
+-----------+------+-------------+
|       400 |    3 | Error 3     |
+-----------+------+-------------+
Run Code Online (Sandbox Code Playgroud)

我可以记录 400 状态返回一个具有“代码”和“描述”的“错误”对象。但如果可能的话,我想在文档中包含所有自定义状态代码。是否可以?如果是这样,我将如何去做?

swagger swagger-ui swagger-editor

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

标签 统计

swagger ×2

swagger-editor ×1

swagger-ui ×1