根据 OpenAPI 3 (Swagger) 规范,响应状态代码对于错误状态代码的多种媒体类型是否有意义

问题描述

如果我们遵循响应 here 的 OAS3 规范,我们可以看到每个响应状态代码可以有多种媒体类型,而每种媒体类型又具有特定的架构。

用例:例如下面的 oas3 示例,我们可以看到 200 有一个二进制流响应,但 400 有 3 种媒体类型:application/json、application/xml、text/plain。

客户端也希望请求带有下面提到的所有媒体类型的 accept-type 标头。我们如何为 400 响应代码提供特定的媒体类型,或者基本上我们如何传达给 REST 服务,以在 400 错误请求和 200 返回二进制流时以应用程序/xml 的媒体类型进行响应。

此 OAS3 响应多媒体类型是否对客户端/服务器错误有意义。如果是,那么为期望设置的接受类型是什么,对于 400 个错误请求说“application/xml”。

请参考下面的 swagger UI snap。我们还看到错误代码媒体类型下拉列表。但是当我们尝试执行其余操作时,仅根据 200 状态码的媒体类型填充接受标头

enter image description here

openapi: 3.0.0
info:
  version: "1.0"
  title: Resource
  description: Resource service
paths:
  /resource:
    get:
      summary: getResource
      description: getResource
      operationId: get-resource
      responses:
        "200":
          description: a binary document to be returned
          content:
            application/octet-stream:
              schema:
                type: string
                format: binary
        "400":
          description: Bad Request
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error400Element"
            application/xml:
              schema:
                $ref: "#/components/schemas/Error400Element"
            text/plain:
              schema:
                $ref: "#/components/schemas/Error400Element"
        "500":
          description: Internal Server Error
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error500Element"
            application/xml:
              schema:
                $ref: "#/components/schemas/Error500Element"
            text/plain:
              schema:
                $ref: "#/components/schemas/Error500Element"
servers:
  - url: http://localhost:8088/
components:
  schemas:
    Error400Element:
      type: object
      required:
        - name
      properties:
        name:
          type: string
        number:
          type: integer
    Error500Element:
      type: object
      properties:
        number:
          type: integer
        flag:
          type: boolean

编辑:修改了 OAS3 规范和 SwaggerUI

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...