Forum Discussion

mma5997's avatar
mma5997
Occasional Contributor
5 years ago

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.

  • 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

     

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    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

     

    • mma5997's avatar
      mma5997
      Occasional Contributor

      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?