Rails 5,Swagger UI不为控制器生成文档json文件

时间:2017-01-14 18:50:56

标签: swagger ruby-on-rails-5 swagger-ui

我正在关注this Swagger tutorial以熟悉swagger环境和API文档。我查看了sample app's repo上的所有相关问题,但没有一个对我有用。

当我运行rake swagger:docs时,它应该在users.json下生成public/api/v1/文件,但它不会。它只生成api-docs.json个文件。 终端也会给出消息1.0: 0 processed / 4 skipped

我尝试添加Swagger::Docs::Config.base_api_controller = ActionController::API并未解决问题。

共享基本文件。如果您需要任何进一步的信息,我很乐意与您分享。希望你能帮忙,真的卡在这里。谢谢。

swagger_docs.rb

# config/initializers/swagger-docs.rb
Swagger::Docs::Config.base_api_controller = ActionController::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" => "Swagger Doc",
        "description" => "Sample app shows how to setup swagger for your Ruby APIs",
        "contact" => "recepinancc@gmail.com",
        "license" => "Apache 2.0",
        "licenseUrl" => "http://www.apache.org/licenses/LICENSE-2.0.html"
      }
    }
  }
})

users_controller.rb

class Api::V1::UsersController < ApplicationController
  swagger_controller :users, "User Management"

# /api/v1/users create documentation
  swagger_api :create do
    summary "To create user"
    notes "Implementation notes, such as required params, example queries for apis are written here."
    param :form, "user[name]", :string, :required, "Name of user"
    param :form, "user[age]", :integer, :optional, "Age of user"
    param_list :form, "user[status]", :string, :required, "Status of user, can be active or inactive"
    response :success
    response :unprocessable_entity
    response :500, "Internal Error"
  end

  # POST /api/v1/users
  def create
    ...
  end
...
end

这是唯一生成的 api-docs.json

{
  "apiVersion": "1.0",
  "swaggerVersion": "1.2",
  "basePath": "http://localhost:3000",
  "apis": [

  ],
  "authorizations": null,
  "info": {
    "title": "Swagger Doc",
    "description": "Sample app shows how to setup swagger for your Ruby APIs",
    "contact": "recepinancc@gmail.com",
    "license": "Apache 2.0",
    "licenseUrl": "http://www.apache.org/licenses/LICENSE-2.0.html"
  }
}

2 个答案:

答案 0 :(得分:1)

我找到了解决我正在运行的应用程序与我的问题的解决方案。唯一的区别是routes.rb文件。显然,I和Rails跳过了为routes.rb中的用户模型生成嵌套路由的步骤。我在下面添加了routes.rb问题解决了。

namespace :api do
    namespace :v1 do
      resources :users
    end
end

希望它有所帮助。

答案 1 :(得分:0)

swagger_docs.rb

中试试这个
include Swagger::Docs::ImpotentMethods

# Swagger::Docs::Config.register_apis({})

class Swagger::Docs::Config
  def self.base_api_controller
    ActionController::API 
  end
end

或者将其放在routes.rb

的顶部
Swagger::Docs::Config.base_api_controller = ActionController::API
include Swagger::Docs::ImpotentMethods