How not to copy-paste 3 generic error responses in almost all paths?

OpenAPI 2.0 (fka Swagger 2.0)

Looks like I can add the following global response definition:

# An object to hold responses that can be used across operations.
# This property does not define global responses for all operations.
responses:
  NotAuthorized:
    description: The requester is unauthorized.
    schema:
      $ref: '#/definitions/Error'

However I will still need to reference it in paths like this:

401:
  $ref: '#/responses/NotAuthorized'

OpenAPI 3.x

Same thing in OpenAPI 3.x, except it uses #/components/responses/... instead of #/responses/...:

openapi: 3.0.0

# An object to hold responses that can be used across operations.
# This property does not define global responses for all operations.
components:
  responses:
    NotAuthorized:
      description: The requester is unauthorized.
      schema:
        $ref: '#/components/schemas/Error'

# Then, in operation responses, use:
...
401:
  $ref: '#/components/responses/NotAuthorized'

There’s also an open feature request in the OpenAPI Specification repository to add support for global/default responses for operations.

Leave a Comment