我想知道是否有人在使用Swagger UI为Swagger生成的API生成API文档之前完成此操作 . 这是我的一个简单例子:
class Api::V1::UsersController < Api::V1::BaseController
swagger_controller :users, 'Users'
swagger_api :show do
summary 'Returns a user'
param :path, :id, :integer, :optional, "User Id"
notes '/api/v1/users/:id'
response :ok, "Success", :Users
response :unauthorized
response :not_acceptable
response :not_found
end
def show
user = User.find(params[:id])
render(json: Api::V1::UserSerializer.new(user).to_json)
end
end
我用 rake swagger:docs
生成了swagger文档,并且在我看到 Users#show
的文档时可以达到 http://localhost:3000/api-docs.json
,但是当我单击"Try it out!"时,我得到 api/v1/users/show
的模板错误
api-docs.json
:
{
"apiVersion": "1.0",
"swaggerVersion": "1.2",
"basePath": "http://localhost:3000",
"apis": [
{
"path": "/api/v1/users.{format}",
"description": "Users"
}
],
"authorizations": null
}
users.json
:
{
"apiVersion": "1.0",
"swaggerVersion": "1.2",
"basePath": "http://localhost:3000",
"resourcePath": "users",
"apis": [
{
"path": "/api/v1/users/{id}.json",
"operations": [
{
"summary": "Returns a user",
"parameters": [
{
"paramType": "path",
"name": "id",
"type": "integer",
"description": "User Id",
"required": false
}
],
"notes": "/api/v1/users/:id",
"responseMessages": [
{
"code": 200,
"responseModel": "Users",
"message": "Success"
},
{
"code": 401,
"responseModel": null,
"message": "Unauthorized"
},
{
"code": 404,
"responseModel": null,
"message": "Not Found"
},
{
"code": 406,
"responseModel": null,
"message": "Not Acceptable"
}
],
"nickname": "Api::V1::Users#show",
"method": "get"
}
]
}
],
"authorizations": null
}
如何为我的show方法呈现正确的响应,以便它查找序列化的json而不是视图文件?
1 回答
所以,我找到了答案 . 首先,删除由
rake swagger:docs
创建的所有json文件,并将以下内容添加到swagger_docs.rb
初始化程序中::clean_directory => true
以便每次运行rake swagger:docs
时,将清除公用文件夹中的目录 .为了让swagger文档与我如何使用'm building my API with ActiveModel'序列化程序来改变
Api::V1::UsersController
中编写的DSL,如下所示:然后运行
rake swagger:docs
并显示用户的调用应该正常工作 .