OpenAPI Post 响应 201 内容错误

OpenAPI Post response 201 with content error

我想使用 OpenAPI v2 设计一个 API 并返回一个 ID 以响应 POST 请求。这是我试过的:

      responses:
        201:
          description: item created
          content:
            application/json:
              schema:
                type: integer
                example: 234
        400:
          description: invalid input, object invalid
        409:
          description: an existing item already exists

我正在使用 Swagger HUB,它为响应 201 引发了以下错误:

should NOT have additional properties additionalProperty: content

出现错误是因为您使用的语法(具体来说,content 关键字)是 OpenAPI 3.0,而不是 2.0。

对应的OpenAPI 2.0语法为:

    get:
      produces:
        - application/json
      ...

      responses:
        201:
          description: item created
          schema:
            type: integer
            example: 234

JSON 有效载荷通常作为对象或数组而不是原语发送。我建议您将响应设为 JSON 对象,例如{"id": 234}。在这种情况下,响应模式如下所示:

      responses:
        201:
          description: item created
          schema:
            type: object
            properties:
              id:
                type: integer
                example: 234