How to specify multiple 404 causes in OpenAPI (Swagger)?

前端 未结 1 1391
无人及你
无人及你 2020-12-20 20:24

I\'m defining a path for a nested resource (content belonging to a delivery). If the client gets a 404 it could either because the delivery ID was not found, or the deliver

相关标签:
1条回答
  • 2020-12-20 21:09

    Multiple response types per status code are not allowed in OpenAPI/Swagger 2.0, but are supported in OpenAPI 3.0 by using oneOf.

    In OpenAPI 2.0, you can only have a single schema for a 404 response:

          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
    

    where the Error payload can be, say:

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

    {
      "status": 404,
      "type": "NoArticlesInDeliveryError",
      "message": "delivery did not contain any articles"
    }
    
    0 讨论(0)
提交回复
热议问题