ruby-on-railsrswag

NoMethodError: undefined method 'path' for class RSpec::ExampleGroups:: - Problem using rswag (swagger_helper)


I have the following code to generate Swagger docs:

require 'swagger_helper'

RSpec.describe 'Transactions API', swagger_doc: 'v1/swagger.yaml' do
  path '/transactions' do
    get 'List transactions' do
      tags 'Transactions'
      produces 'application/json'
      parameter name: :user_id, in: :query, type: :integer, description: 'User ID (optional)'

      response '200', 'transactions listed' do
        schema type: :array, items: {
          type: :object,
          properties: {
            transaction_id: { type: :integer },
            user_id: { type: :integer },
            from_currency: { type: :string },
            to_currency: { type: :string }
          }
        }
        run_test!
      end
    end
  end   
end

I am running the following code to generate the Swagger docs:

$ bundle exec rake rswag:specs:swaggerize

But I am getting the following error:

$ bundle exec rake rswag:specs:swaggerize
/home/dalton/.rbenv/versions/3.4.4/bin/ruby -I/home/dalton/workspace/ror/ruby/3.4.0/gems/rspec-core-3.13.4/lib:/home/dalton/workspace/ror/ruby/3.4.0/gems/rspec-support-3.13.4/lib /home/dalton/workspace/ror/ruby/3.4.0/gems/rspec-core-3.13.4/exe/rspec --pattern spec/requests/\*\*/\*_spec.rb,\ spec/api/\*\*/\*_spec.rb,\ spec/integration/\*\*/\*_spec.rb --format Rswag::Specs::SwaggerFormatter --dry-run --order defined
DEPRECATION WARNING: Rswag::Ui: WARNING: The method will be renamed to "openapi_endpoint" in v3.0 (called from block in <main> at /home/dalton/workspace/ror/curconv/config/initializers/rswag_ui.rb:11)
Generating Swagger docs ...

An error occurred while loading ./spec/integration/transactions_swagger_spec.rb.
Failure/Error:
    path '/transactions' do
      get 'List transactions' do
        tags 'Transactions'
        produces 'application/json'
        parameter name: :user_id, in: :query, type: :integer, description: 'User ID (optional)'
  
        response '200', 'transactions listed' do
          schema type: :array, items: {
            type: :object,
            properties: {

NoMethodError:
  undefined method 'path' for class RSpec::ExampleGroups::TransactionsAPI
# ./spec/integration/transactions_swagger_spec.rb:4:in 'block in <main>'
# ./spec/integration/transactions_swagger_spec.rb:3:in '<main>'
Swagger doc generated at /home/dalton/workspace/ror/curconv/swagger/v1/swagger.yaml

Finished in 0.00006 seconds (files took 1.15 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples

How to solve this problem?


Solution

  • After searching, I found the following solution in the rswag GitHub, described by Nikita Ignatovich: we must uncomment the config.infer_spec_type_from_file_location! line in the file spec/rails_helper.rb.

    Other option is using the type: :request in addition to the `swagger_doc` option. Thus, your line must be like this:

    RSpec.describe 'Your API', type: :request, swagger_doc: 'v1/swagger.yaml' do