Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference response components in OpenAPI?

I am writing an OpenAPI definition for my API. I am using components for responses, but Swagger Editor shows an error when I try to reference these components:

responses:
  - $ref: '#/components/responses/401'
  - $ref: '#/components/responses/400'

enter image description here

What is the correct way to reference response components?

like image 830
Vinh Nguyễn Avatar asked Jun 14 '26 12:06

Vinh Nguyễn


1 Answers

The correct way to reference response components is:

responses:
  '400':
    $ref: '#/components/responses/400'
  '401':
    $ref: '#/components/responses/401'

That is, responses is a map (not an array/list) where the keys are HTTP status codes and the values are response definitions.


Can you override the response description when referencing the component?

This is possible in OpenAPI 3.1, but not in earlier versions. Put the new description alongside the $ref:

# openapi: 3.1.0

responses:

  '400':
    $ref: '#/components/responses/400'
    description: This description overrides that of the referenced response.

  '401':
    $ref: '#/components/responses/401'
    description: This description overrides that of the referenced response.
like image 117
Helen Avatar answered Jun 17 '26 23:06

Helen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!