如何在从不同基本控制器继承的控制器中包含swagger-docs?

时间:2017-02-28 13:53:22

标签: ruby-on-rails swagger

我使用swagger-docs向API添加文档,我的配置如下所示:

Swagger::Docs::Config.register_apis({
  "1.0" => {
    :api_file_path => "public/",
    :base_path => "http://localhost:3000",
    :clean_directory => true,
    :base_api_controller => ActionController::API,
    :attributes => {
      :info => {
        "title" => "Test",
        "description" => "Test",
        "contact" => "contact@example.com",
        "license" => "MIT",
        "licenseUrl" => "https://opensource.org/licenses/MIT"
      }
    }
  }
})

这适用于从ActionController::API继承的控制器,但是当控制器从不同的基本控制器继承时,如何包含DSL方法?例如,使用knock gem进行身份验证时:

class Users::AuthenticationController < Knock::AuthTokenController
  swagger_controller :login, "Request a new JWT to issue authenticated requests"

  swagger_api :create do
    summary "Generates a JWT to be used for authenticated requests"
    param :email, "auth[email]", :string, :required, "Email for authentication"
    param :password, "auth[password]", :string, :required, "Password for authentication"
    response :created
    response :not_found, "Credentials specified were wrong"
  end

  def entity_class
    User
  end
end

此代码返回以下错误:

undefined method `swagger_controller' for Users::AuthenticationController:Class

我尝试使用以下方法包含这些方法:

include Swagger::Docs::ImpotentMethods

但这并没有奏效,即使它删除了错误。

1 个答案:

答案 0 :(得分:2)

这真的很愚蠢,我希望它能为将来节省时间:

include Swagger::Docs::ImpotentMethods只是方法的空实现,正如名称所示。

包括这样的方法,它应该都能正常工作:

class Users::AuthenticationController < Knock::AuthTokenController
  include Swagger::Docs::Methods
end