richhollis / swagger-docs

Generates swagger-ui json files for Rails APIs with a simple DSL.
MIT License
750 stars 150 forks source link

All controllers skipped with using rails-api #137

Closed jesmith17 closed 8 years ago

jesmith17 commented 8 years ago

I am using Rails API, so my ApplicationController extends from ActionController::API

I have added the DSL to the top of one of my controllers

swagger_controller :invoice, "Invoice Management"

    swagger_api :index do
      summary "Fetches all Invoices"
      notes "Fetches all non-cancelled invoices in order by create date, desc. Supports paging"
      param "page_number"
      response :unauthorized
      response :not_acceptable
    end

Initializer looks like

Swagger::Docs::Config.base_api_controller = ActionController::API

Swagger::Docs::Config.register_apis({
    "v1" => {
        # the extension used for the API
        :api_extension_type => :json,
        # the output location where your .json files are written to
        :api_file_path => "public/api/v1/",
        # the URL base path to your API
        :base_path => "http://api.somedomain.com",
        # if you want to delete all .json files at each generation
        :clean_directory => true,
        # Ability to setup base controller for each api version. Api::V1::SomeController for example.
        :base_api_controller => ApplicationController,

        # add custom attributes to api-docs
        :attributes => {
            :info => {
                "title" => "Swagger Sample App",
                "description" => "This is a sample description.",
                "termsOfServiceUrl" => "http://helloreverb.com/terms/",
                "contact" => "apiteam@wordnik.com",
                "license" => "Apache 2.0",
                "licenseUrl" => "http://www.apache.org/licenses/LICENSE-2.0.html"
            }
        }
    }
})

(note: I intentionally didn't change it much to make sure it wasn't something I did).

SD_LOG_LEVEL=1 rake swagger:docs

Produces the following output

uninitialized constant Api::PasswordsController
rake aborted!
ArgumentError: wrong number of arguments (given 1, expected 4..6)
/usr/local/lib/ruby/gems/2.3.0/gems/swagger-docs-0.2.8/lib/swagger/docs/dsl.rb:48:in `param'
/sandbox/charter-pay/charter-pay-api/app/controllers/api/v1/invoices_controller.rb:13:in `block in <class:InvoicesController>'
/usr/local/lib/ruby/gems/2.3.0/gems/swagger-docs-0.2.8/lib/swagger/docs/dsl.rb:8:in `instance_eval'
/usr/local/lib/ruby/gems/2.3.0/gems/swagger-docs-0.2.8/lib/swagger/docs/dsl.rb:8:in `call'
/usr/local/lib/ruby/gems/2.3.0/gems/swagger-docs-0.2.8/lib/swagger/docs/methods.rb:19:in `block in swagger_actions'
/usr/local/lib/ruby/gems/2.3.0/gems/swagger-docs-0.2.8/lib/swagger/docs/methods.rb:18:in `each'
/usr/local/lib/ruby/gems/2.3.0/gems/swagger-docs-0.2.8/lib/swagger/docs/methods.rb:18:in `swagger_actions'
/usr/local/lib/ruby/gems/2.3.0/gems/swagger-docs-0.2.8/lib/swagger/docs/generator.rb:188:in `get_route_path_apis'
/usr/local/lib/ruby/gems/2.3.0/gems/swagger-docs-0.2.8/lib/swagger/docs/generator.rb:143:in `block in process_path'
/usr/local/lib/ruby/gems/2.3.0/gems/swagger-docs-0.2.8/lib/swagger/docs/generator.rb:141:in `each'
/usr/local/lib/ruby/gems/2.3.0/gems/swagger-docs-0.2.8/lib/swagger/docs/generator.rb:141:in `process_path'
/usr/local/lib/ruby/gems/2.3.0/gems/swagger-docs-0.2.8/lib/swagger/docs/generator.rb:75:in `block in generate_doc'
/usr/local/lib/ruby/gems/2.3.0/gems/swagger-docs-0.2.8/lib/swagger/docs/generator.rb:74:in `each'
/usr/local/lib/ruby/gems/2.3.0/gems/swagger-docs-0.2.8/lib/swagger/docs/generator.rb:74:in `generate_doc'
/usr/local/lib/ruby/gems/2.3.0/gems/swagger-docs-0.2.8/lib/swagger/docs/generator.rb:56:in `block in generate_docs'
/usr/local/lib/ruby/gems/2.3.0/gems/swagger-docs-0.2.8/lib/swagger/docs/generator.rb:53:in `each'
/usr/local/lib/ruby/gems/2.3.0/gems/swagger-docs-0.2.8/lib/swagger/docs/generator.rb:53:in `generate_docs'
/usr/local/lib/ruby/gems/2.3.0/gems/swagger-docs-0.2.8/lib/swagger/docs/generator.rb:24:in `write_docs'
/usr/local/lib/ruby/gems/2.3.0/gems/swagger-docs-0.2.8/lib/tasks/swagger.rake:5:in `block (2 levels) in <top (required)>'
Tasks: TOP => swagger:docs
(See full trace by running task with --trace)

I assume the API::passwords issues with part of Devise, and am not worried about that.

The only way I can get past this is to either remove the top line of the config where we set the base_api_controller, or to comment out the

swagger_controller :invoice, "Invoice Management"

line in the controlller.

Then I get

uninitialized constant Api::PasswordsController
undefined method `swagger_controller' for Api::V1::InvoicesController:Class
v1: 0 processed / 15 skipped

when I execute.

What am i missing here?

Using swagger-docs 0.2.8 and Rails 4.2.5 Rake 11.1.1

richhollis commented 8 years ago

@jesmith17 What was the fix in the end?

otobrglez commented 8 years ago

@richhollis - I had the same problem. So I added this to my Swagger docs initializer:

class Swagger::Docs::Config
  def self.base_api_controller; ApiController end
end

In my case ApiController inherits from ActionController::API.

richhollis commented 8 years ago

Hey @otobrglez - Are you using Rails API / ActionController::API too?

otobrglez commented 8 years ago

@richhollis Yes. However later on I found out that this doesen't work as expected. Sometimes in development mode controllers get reloaded and for some reason I also get missing error. So I'm now running this - and it works. With Rails 4 that is.:

module Swagger
  module Docs
    class Config
      def self.base_api_controller
        ActionController::API
      end
    end
  end
end
fercreek commented 7 years ago

https://github.com/richhollis/swagger-docs/issues/147#issuecomment-337801122 I solved the issue in this way