7

我正在为嵌套资源(属于交付的内容)定义路径。如果客户端收到 404,则可能是因为未找到交付 ID,或者交付不包含任何指定类型的内容。

如何使用 OpenAPI (YAML) 对其进行建模?

我现在有这个...

 paths:
  '/deliveries/{id}/content/articles':
    get:
      summary: Retrieves articles from a delivery
      description: Retrieves all articles from a single delivery
      [...]
      responses:
        '200':
          description: articles found
          schema:
            $ref: '#/definitions/Article'
        '404':
          description: delivery not found
          schema:
            $ref: '#/definitions/Error'
        '404':
          description: delivery did not contain any articles
          schema:
            $ref: '#/definitions/Error'

...但是当我从 Swagger 编辑器中保存 JSON 时,它会删除除最后一个响应之外的所有 404 响应(“交付不包含任何文章”)。

4

1 回答 1

5

在 OpenAPI/Swagger 2.0 中不允许每个状态码有多个响应类型,但在 OpenAPI 3.0 中通过使用oneOf.

在 OpenAPI 2.0 中,404 响应只能有一个模式:

      responses:
        '404':
          description: delivery not found, or delivery did not contain any articles
          schema:
            $ref: '#/definitions/Error'

...
definitions:
  Error:
    type: object
    properties:
      status:
        type: integer
      type:
        type: string
      message:
        type: string

Error有效载荷可以在哪里,比如:

{
  "status": 404,
  "type": "DeliveryNotFoundError",
  "message": "delivery not found"
}

{
  "status": 404,
  "type": "NoArticlesInDeliveryError",
  "message": "delivery did not contain any articles"
}
于 2016-11-18T17:26:28.343 回答