Forum Discussion

swagger123's avatar
swagger123
New Member
6 months ago

Reference error responses in OAS3

Hi, I'm trying to figure out the correct way of referencing error responses in openapi 3.
It seems OK to reference the '404' response object this way. At least the swagger editor do not complain.
However, I get build error not finding that model when generating kotlin client code with the Swagger editor client generator.
https://editor-next.swagger.io/

 

openapi: 3.0.3
info:
  title: Foo bar API
  version: 1.0.0
paths:
  /foo/{bar}:
      get:
        summary: Get bar.
        description: Returns bar.
        parameters:
          - in: path
            name: bar
            schema:
              $ref: '#/components/schemas/BarId'
            required: true
            description: The bar.
        responses:
          '200':
            description: Bar found.
            content:
              application/json:
                schema:
                  $ref: '#/components/schemas/BarId'
          '404':
            description: Bar is missing
            content:
              application/json:
                schema:
                  $ref: '#/components/responses/FooError'
components:
  schemas:
    BarId:
      description: "Bar id"
      type: string
      example: 123
  responses:
    FooError:
      description: Error
      content:
        application/json:
          schema:
            type: object
            properties:
              message:
                type: string
                example: Something went wrong.

 


 

1 Reply

  • JOHNSMITH167's avatar
    JOHNSMITH167
    Occasional Contributor

    Ensure that your '404' error response is defined under components/responses with a unique name, and the reference is correct with path. Make sure the Swagger client generator recognizes the definition. If using the Swagger editor, validate the YAML to catch any syntax issues that might cause build errors.