Rails 5,Swagger UI不为控制器生成文档json文件
为了熟悉swagger环境和API文档编制,我遵循了以下步骤。我在网上查看了所有相关问题,但没有一个对我有用 当我运行Rails 5,Swagger UI不为控制器生成文档json文件,swagger,ruby-on-rails-5,swagger-ui,Swagger,Ruby On Rails 5,Swagger Ui,为了熟悉swagger环境和API文档编制,我遵循了以下步骤。我在网上查看了所有相关问题,但没有一个对我有用 当我运行rake-swagger:docs时,它应该在public/api/v1/下生成users.json文件,而不是。它只生成api docs.json文件。 此外,在终端中给出消息1.0:0已处理/4已跳过 我尝试添加了Swagger::Docs::Config.base\u api\u controller=ActionController::api,但也没有解决问题 共享基本文
rake-swagger:docs
时,它应该在public/api/v1/
下生成users.json
文件,而不是。它只生成api docs.json
文件。
此外,在终端中给出消息1.0:0已处理/4已跳过
我尝试添加了Swagger::Docs::Config.base\u api\u controller=ActionController::api
,但也没有解决问题
共享基本文件。如果您需要任何进一步的信息,我将很乐意与您分享。希望你能帮忙,真的困在这里了。多谢各位
swagger\u 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"
}
}
}
})
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
{
"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"
}
}
用户\u控制器.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"
}
}
}
})
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
{
"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"
}
}
通过比较运行中的应用程序和我的应用程序,我找到了解决问题的方法。唯一的区别是
routes.rb
文件。显然,在routes.rb中为我们的用户模型生成嵌套路由的步骤跳过了I和Rails。在我将下面的内容添加到routes.rb后,问题解决了
namespace :api do
namespace :v1 do
resources :users
end
end
希望有帮助。在您的
swagger\u docs.rb
include Swagger::Docs::ImpotentMethods
# Swagger::Docs::Config.register_apis({})
class Swagger::Docs::Config
def self.base_api_controller
ActionController::API
end
end
Swagger::Docs::Config.base_api_controller = ActionController::API
include Swagger::Docs::ImpotentMethods
或者将其置于routes.rb的顶部
include Swagger::Docs::ImpotentMethods
# Swagger::Docs::Config.register_apis({})
class Swagger::Docs::Config
def self.base_api_controller
ActionController::API
end
end
Swagger::Docs::Config.base_api_controller = ActionController::API
include Swagger::Docs::ImpotentMethods