cancel
Showing results for 
Search instead for 
Did you mean: 

How to have a custom status code in response

SOLVED
Highlighted
New Contributor

How to have a custom status code in response

      responses:
        '777':
          description: My Custom
          content:
            application/json:
              schema:
                type: string

If I have "777" like fieldName for the status code in OAS3.0 spec YAML, then the editor.swagger.io throws an error as 
"should only have three-digit status codes, `default`, and vendor extensions (`x-*`) as properties"

If I have the same status code in Swagger2.0 spec YAML, as below, then it doesn't throw an error.

      responses:
        '777':
          description: custom
          schema:
            type: string

 

PS: I checked the indentation, it seems correct because if I change "777" in OAS3.0 spec YAML to standard status code like "400", "200" then it doesn't throw an error.

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Moderator

Re: How to have a custom status code in response

OpenAPI only supports the standard HTTP status codes from the 1xx-5xx range. You can provide custom codes and messages in the body of error responses, for example:

 

      responses:
        '400':
          description: Bad request
          content:
            application/json:
              schema:
                type: object
                properties:
                  code:
                    type: integer
                    example: 777
                  message:
                    type: string
                    example: Lorem ipsum

 


Helen Kosova
SmartBear Documentation Team Lead
________________________
Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️

View solution in original post

2 REPLIES 2
Highlighted
Moderator

Re: How to have a custom status code in response

OpenAPI only supports the standard HTTP status codes from the 1xx-5xx range. You can provide custom codes and messages in the body of error responses, for example:

 

      responses:
        '400':
          description: Bad request
          content:
            application/json:
              schema:
                type: object
                properties:
                  code:
                    type: integer
                    example: 777
                  message:
                    type: string
                    example: Lorem ipsum

 


Helen Kosova
SmartBear Documentation Team Lead
________________________
Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️

View solution in original post

Highlighted
New Contributor

Re: How to have a custom status code in response

Hi @HKosova 
But if we use this custom status code like "777" under any standard status code, say "400", Then I can't have a specific response definition for "400" as there's already a key/field named "400".

It wasn't the thing with Swagger20 as well right, it used to have a separate key for the custom status code mapped to its object defined. And it worked without error. No WA was needed like you suggested is needed.

Is this a behavioural change in OAS3.0 that responses with custom status codes won't be supported directly but will have to sacrifice the definition of standard status code?

New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors