Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swagger docs - multiple error codes for a single http response

I'm trying to document a model that always returns a custom response code and associated description. For a single HTTP response code, there could be multiple custom response codes. For example, a 400 response might include:

HTTP Code Code Description
400 1 Error 1
400 2 Error 2
400 3 Error 3

I could document that a 400 status returns an "Error" object that has "Code" and "Description". But I'd like to include all of the custom status codes in the documentation if that's even possible. Is it possible? If so, how would I go about doing this?

like image 518
user1795832 Avatar asked Mar 01 '26 05:03

user1795832


1 Answers

For anyone facing this problem nowadays, here's how I was able to solve it:

    '422':
      description: 'Unprocessable Entity'
      content:
          application/json:
              schema:
                oneOf:
                  - $ref: '#/components/schemas/SchemaForErrror1'
                  - $ref: '#/components/schemas/SchemaForErrror2'
    
              examples:
                Error1:
                  $ref: '#/components/examples/ExampleForError1'
                Error2:
                  $ref: '#/components/examples/ExampleForError2'
like image 102
Sebastian palacio florez Avatar answered Mar 03 '26 04:03

Sebastian palacio florez



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!