3

Using gem rswag-specs (2.3.1). Defined responses among components according to https://swagger.io/docs/specification/components/ :

 config.swagger_docs = {
  'swagger.yaml' => {
      openapi: '3.0.1',
      info: {
        title: 'title',
      },
      paths: {},
      components: {
        responses: {
          "403": {
            type: :object,
            properties: {
              errors: {
                type: :array,
                items: {
                  '$ref' => '#/components/schemas/forbidden_error'
                }
              }
            }
          }
        }
      }
    }
  }

Test fails with error:

      response '403', 'Wrong permissions' do
        let(:Authorization) { "Bearer #{token}" }
        let(:id) { item.id }
        schema '$ref' => '#/components/responses/403'

        run_test!
      end
 JSON::Schema::SchemaError:
       The fragment '/components/responses' does not exist on schema aec776a1-291e-5317-89bf-6ea163b4bac3

How can I use /components/responses section?

Sergii Brytiuk
  • 345
  • 1
  • 12
  • Where is `components/schemas/forbidden_error` defined? Does it work, when you define `items` directly? – Thomas Koppensteiner Oct 28 '20 at 19:58
  • There is no issue with `components/schemas/forbidden_error`. I can't reference `responses` as described here https://swagger.io/docs/specification/components, like `/components/responses/403'`. They say it should be possible to do `# Can be referenced as '#/components/responses/404NotFound'` – Sergii Brytiuk Oct 29 '20 at 10:59

1 Answers1

2

Update

I the example '#/components/schemas/forbidden_error' is not defined. Try to add it to /components/schemas or to inline it, as in the "404" example.

config.swagger_docs = {
  'swagger.yaml' => {
    openapi: '3.0.1',
    info: {
      title: 'title',
    },
    paths: {},
    components: {
      schemas: {
        forbidden_error: {
          type: object,
          properties: {
            message: string
          }
        }
      },  
      responses: {
        "403": {
          type: :object,
          properties: {
            errors: {
              type: :array,
              items: {
                '$ref' => '#/components/schemas/forbidden_error'
              }
            }
          }
        },
        "404": {
          type: :object,
          properties: {
            errors: {
              type: :array,
              items: {
                type: object,
                properties: {
                  message: string
                }
              }
            }
          }
        }
      }
    }
  }
}

Origianl Answer

This was a wrong approach.

The example in the rswag gem readme suggests that the nesting is components/schemas/refrenced_object. In your example responses is located directly under components. Would move responses under `schemas as well.

  • 1
    Absolutely. I mean `responses` section, which should be inside `components` on the same level as `schemas` according to swagger spec https://swagger.io/docs/specification/components. Does it make sense to you? Thanks – Sergii Brytiuk Oct 29 '20 at 10:56
  • @SergiiBrytiuk yes, now it makes more sense. Will update my answer. – Thomas Koppensteiner Oct 29 '20 at 18:53