Search code examples
swaggerresponseopenapihttp-error

Is it possible to define a set/list of responses for reusage in open api?


Currently I have something like this:

paths: 
    /home
...
      responses:
        200:
          description: Successful operation
        401:
          $ref: '#/components/responses/401UnauthorizedDefault'
        404:
          $ref: '#/components/responses/404NotFound'
        502:
          $ref: '#/components/responses/502BadGatewayDefault'
    /sign_up
...
      responses:
        201:
          description: Created
        401:
          $ref: '#/components/responses/401UnauthorizedDefault'
        404:
          $ref: '#/components/responses/404NotFound'
        502:
          $ref: '#/components/responses/502BadGatewayDefault'

Is it possible to combine all three error responses and reference them with a $ref?

paths:
    /home
...
      responses:
        200:
          description: Successful operation
        $ref: '#/components/responses/AllCommonErrorResponses’
    /sign_up
...
      responses:
        201:
          description: Created
        $ref: '#/components/responses/AllCommonErrorResponses’

Currently I dont know all the error responses we will use. But there will be a set of shared. If we will extend them later, I dont want to go to every endpoint and adjust the responses.


Solution

  • OpenAPI v3 does not allow to reference a list of multiple responses.

    You can use specific (e.g., "200") response codes or default for others.

    There is no option to import a standard list of responses and reuse it.