Eliminate Duplicate Response Definitions in OpenAPI with components.responses
Learn how to eliminate duplicate HTTP response definitions in OpenAPI/Swagger by moving them to components.responses and referencing them with $ref.
30 Jul 2025, 13:59 UTC

Problem: Repeated response blocks bloat your API spec
When you design a REST API, many endpoints share the same error or success payloads—for example, a generic 404 Not Found envelope or a 200 OK wrapper. Copy‑pasting these objects into every responses section inflates the OpenAPI/YAML file, makes updates error‑prone, and hides the contract’s intent.
Thesis: Use components.responses and $ref to keep a single source of truth
OpenAPI 3.0 provides a top‑level components object where you can declare reusable building blocks, including responses. By defining a response once and referencing it with a JSON Reference ($ref), every operation inherits the same definition. This reduces duplication, centralises changes, and keeps the spec readable.
Worked example: Centralising a 404 response
Assume you have the following duplicated fragment in several paths:
responses:
'404':
description: Not Found
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorEnvelope'
examples:
notFound:
value:
code: 404
message: Resource not found
Instead of repeating this block, move it to components.responses:
components:
responses:
NotFound:
description: Not Found
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorEnvelope'
examples:
notFound:
value:
code: 404
message: Resource not found
Then, in each operation, replace the duplicated block with a simple reference:
paths:
/users/{id}:
get:
summary: Get a user by ID
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/User'
'404':
$ref: '#/components/responses/NotFound'
/orders/{id}:
get:
summary: Get an order by ID
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/Order'
'404':
$ref: '#/components/responses/NotFound'
How to apply the change safely
- Locate duplicates: Search your spec for identical response blocks (e.g.,
grep -A 5 "'404':" api.yaml | sort | uniq -d). - Extract to components: Add a
components.responsessection if it does not exist, paste the block under a meaningful key (e.g.,NotFound), and replace the originaldescription,content, andexampleswith a$refpointing to#/components/responses/NotFound. - Validate the reference: Run a validator that resolves JSON References, such as
swagger-cli verify api.yamloropenapi validator api.yaml. The command should exit with status 0 and report no unresolved$referrors. - Inspect the rendered docs: Serve the spec with Swagger UI (e.g.,
docker run -p 8080:8080 -v $(pwd):/swagger-ui swaggerapi/swagger-ui) and openhttp://localhost:8080. Select any operation that uses the reference, expand the 404 response, and verify that the description, schema, and example match the definition you placed incomponents.responses. - Iterate: Repeat the extraction for other duplicated responses (e.g., 400 Bad Request, 500 Internal Server Error) until the spec is clean.
Trade‑off and limitation
The main trade‑off is a modest learning curve: you must understand JSON Reference syntax and ensure your toolchain (Swagger UI, Redoc, code generators) resolves $ref correctly. Older Swagger UI releases (< 3.x) do not support $ref in the responses field, so you need to use a recent version. Additionally, circular references (e.g., a response referencing a schema that references the response) will cause parsing failures in most OpenAPI processors; keep the dependency graph acyclic.
Actionable closing
Start by extracting one duplicated response into components.responses, validate with swagger-cli verify, and confirm the rendered docs show the expected content. Once the first reference works, apply the same pattern to other duplicates. This incremental approach keeps your spec maintainable without risky bulk changes.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.