Reusing Response Definitions in OpenAPI with components.responses
Learn how to define a response once in OpenAPI's components.responses and reuse it with $ref to cut duplication and keep docs consistent.
28 Sept 2026, 15:10 UTC

Why reuse responses
When many endpoints return the same error or success shape, defining that shape once and referencing it eliminates duplication, keeps documentation consistent, and reduces the chance of mismatched schemas.
Worked example
Below is a minimal OpenAPI 3.0 YAML snippet that defines a reusable NotFoundError response under components.responses and references it in a GET /users/{id} operation.
openapi: 3.0.3
info:
title: Sample API
version: 1.0.0
paths:
/users/{id}:
get:
summary: Retrieve a user by ID
parameters:
- name: id
in: path
required: true
schema:
type: string
responses:
'200':
description: Successful response
content:
application/json:
schema:
$ref: '#/components/schemas/User'
'404':
$ref: '#/components/responses/NotFoundError'
components:
schemas:
User:
type: object
properties:
id:
type: string
name:
type: string
responses:
NotFoundError:
description: User not found
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorResponse'
schemas:
ErrorResponse:
type: object
properties:
code:
type: integer
example: 404
message:
type: string
example: "User not found"
The $ref: '#/components/responses/NotFoundError' line tells any OpenAPI‑aware tool to insert the full response object defined under components.responses at that spot. In Swagger UI the 404 response will appear with the description “User not found” and the example error schema.
Limitations
- The referenced component must exist in the same document; external
$reffiles are only resolved if the tooling (e.g., Swagger CLI, Redoc) is configured to bundle or fetch them. - OpenAPI 3.0+ uses the
componentsobject. Pure Swagger 2.0 documents do not havecomponents; they rely ondefinitionsand inline response definitions, so the pattern above will not work without upgrading to OpenAPI 3.0. - Circular references (e.g., a response that references a schema which in turn references the same response) can cause validation tools to enter infinite loops and break code generation.
Common pitfalls
- Incorrect indentation: YAML is whitespace‑sensitive. If the
$refline is not indented under the properresponses:key, the parser treats it as a top‑level key and the reference is ignored. - Missing description or schema: A component response that lacks either a
descriptionor aschemablock will render as an empty or misleading entry in the generated docs. - Wrong reference path: Using a relative path like
./NotFoundErroror a typo in the fragment identifier (#/components/responses/NotFound) leads to unresolved$referrors during linting or UI rendering.
Verification steps
- Save the YAML to a file, e.g.,
api.yaml. - Open it in Swagger UI (via
npx swagger-ui-dist api.yamlor an online editor such as Swagger Editor). - Select the
GET /users/{id}operation and expand the404response; you should see the description “User not found” and the example error schema. - Run a linter like
openapi-linter api.yamlorspeccy api.yaml; the output should report no unresolved$referrors.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.