Api 如何在Swagger 2.0中处理多个400响应

Api 如何在Swagger 2.0中处理多个400响应,api,yaml,swagger,Api,Yaml,Swagger,对于400 HTTP状态代码,我有两种情况,第一个响应如下所示: { "error": "No file uploaded!" } { "error": "Wrong file format. Endpoint only accepts images and pdf file formats!" } responses: "200": descrip

对于400 HTTP状态代码,我有两种情况,第一个响应如下所示:

{
  "error": "No file uploaded!"
}
{
  "error": "Wrong file format. Endpoint only accepts images and pdf file formats!"
}
responses:
        "200":
          description: "Successful operation"
          schema:
            type: file
            example: image/png
        "400":
          description: Multiple 400 errors
          schema:
            $ref: '#/definitions/MultiError400'
第二个反应是这样的:

{
  "error": "No file uploaded!"
}
{
  "error": "Wrong file format. Endpoint only accepts images and pdf file formats!"
}
responses:
        "200":
          description: "Successful operation"
          schema:
            type: file
            example: image/png
        "400":
          description: Multiple 400 errors
          schema:
            $ref: '#/definitions/MultiError400'
在swagger 2.0 yaml文件定义中,我以这种方式记录了两个响应:

definitions:
     MultiError400:
        type: object
        properties:
          error:
            type: array
            items:
              enum:
              - $ref: '#/definitions/FirstError'
              - $ref: '#/definitions/SecondError'
      FirstError:
        type: string
        example: "No file uploaded!"
      SecondError:
        type: string
        example: "Wrong file format. Endpoint only accepts images and pdf file formats!"
在端点内部,我这样称呼它:

{
  "error": "No file uploaded!"
}
{
  "error": "Wrong file format. Endpoint only accepts images and pdf file formats!"
}
responses:
        "200":
          description: "Successful operation"
          schema:
            type: file
            example: image/png
        "400":
          description: Multiple 400 errors
          schema:
            $ref: '#/definitions/MultiError400'
但当我测试它时,它给了我一个未记录的错误:

Undocumented
TypeError: Failed to fetch
如何解决此问题?

您可以尝试以下方法:

产生:
-“应用程序/json”
响应:
"400":
说明:“”
模式:
类型:“对象”
特性:
错误:
类型:“字符串”
说明:>
错误:
*`First Error`-未上载任何文件!
*`Second Error`-端点仅接受图像和pdf文件格式!
枚举:
-“没有上载文件!”
-“文件格式错误。Endpoint仅接受图像和pdf文件格式!”
默认值:“错误的文件格式。端点仅接受图像和pdf文件格式!”
您可以将任何
默认值
值表单
枚举
设置为在示例值中显示,它将如下所示:

{
  "error": "No file uploaded!"
}
{
  "error": "Wrong file format. Endpoint only accepts images and pdf file formats!"
}
responses:
        "200":
          description: "Successful operation"
          schema:
            type: file
            example: image/png
        "400":
          description: Multiple 400 errors
          schema:
            $ref: '#/definitions/MultiError400'


您的模型视图如下所示:

{
  "error": "No file uploaded!"
}
{
  "error": "Wrong file format. Endpoint only accepts images and pdf file formats!"
}
responses:
        "200":
          description: "Successful operation"
          schema:
            type: file
            example: image/png
        "400":
          description: Multiple 400 errors
          schema:
            $ref: '#/definitions/MultiError400'

有关更多详细信息,请参阅


希望有帮助。

您可以尝试以下方法:

产生:
-“应用程序/json”
响应:
"400":
说明:“”
模式:
类型:“对象”
特性:
错误:
类型:“字符串”
说明:>
错误:
*`First Error`-未上载任何文件!
*`Second Error`-端点仅接受图像和pdf文件格式!
枚举:
-“没有上载文件!”
-“文件格式错误。Endpoint仅接受图像和pdf文件格式!”
默认值:“错误的文件格式。端点仅接受图像和pdf文件格式!”
您可以将任何
默认值
值表单
枚举
设置为在示例值中显示,它将如下所示:

{
  "error": "No file uploaded!"
}
{
  "error": "Wrong file format. Endpoint only accepts images and pdf file formats!"
}
responses:
        "200":
          description: "Successful operation"
          schema:
            type: file
            example: image/png
        "400":
          description: Multiple 400 errors
          schema:
            $ref: '#/definitions/MultiError400'


您的模型视图如下所示:

{
  "error": "No file uploaded!"
}
{
  "error": "Wrong file format. Endpoint only accepts images and pdf file formats!"
}
responses:
        "200":
          description: "Successful operation"
          schema:
            type: file
            example: image/png
        "400":
          description: Multiple 400 errors
          schema:
            $ref: '#/definitions/MultiError400'

有关更多详细信息,请参阅



希望有帮助。

第二条错误消息不应返回400,而应返回。第二条错误消息不应返回400,而应返回。