:api_extension_type => :json,
# the output location where your .json files are written to
:api_file_path => "public",
# the URL base path to your API
:base_path => "http://localhost:3000",
# if you want to delete all .json files at each generation
:clean_directory => false,
# Ability to setup base controller for each api version. Api::V1::SomeController for example.
:parent_controller => ApplicationController,
# add custom attributes to api-docs
:attributes => {
:info => {
"title" => "Swagger Sample App",
"description" => "This is a sample description.",
"termsOfServiceUrl" => "http://xxx",
"contact" => "xxx",
"license" => "Apache 2.0",
"licenseUrl" => "http://www.apache.org/licenses/LICENSE-2.0.html"
}
}
}
})
`
UserController.rb
`class Api::V1::UsersController < ApplicationController
include Swagger::Docs::ImpotentMethods
swagger_controller :users, "User Management"
swagger_api :index do
summary "Fetches all User items"
response :unauthorized
response :not_acceptable, "The request you made is not acceptable"
response :requested_range_not_satisfiable
end
swagger_api :show do
summary "Fetches a single User item"
param :path, :id, :integer, :required, "User Id"
response :ok, "Success", :User
response :unauthorized
response :not_acceptable
response :not_found
end
get model list /api/v1/users/index
def index
@user = User.all
render :json => {users: @user} , :status => 200
end
get model by id /api/v1/users/show?id=1
def show
@user = User.find(params[:id])
render :json => {user: @user}, :status => 200
end
create model
def create
@user = User.new(user_params)
if @user.save
render '200'
else
render '400'
end
end
update model by id
def update
end
delete model by id
def delete
end
private
def user_params
params.require(:user).permit(:name,:password,:address)
end
end
`
and the last , run command : SD_LOG_LEVEL=1 rake swagger:docs
then generator a the doc file what has not action structure on apis .
Hi all I used rails-api 5.1.2 ,let me show my code
swagger_doc.rb `Swagger::Docs::Config.base_api_controller = ActionController::API
Swagger::Docs::Config.register_apis({ "1.0" => {
the extension used for the API
`
UserController.rb `class Api::V1::UsersController < ApplicationController include Swagger::Docs::ImpotentMethods
swagger_controller :users, "User Management"
swagger_api :index do summary "Fetches all User items" response :unauthorized response :not_acceptable, "The request you made is not acceptable" response :requested_range_not_satisfiable end
swagger_api :show do summary "Fetches a single User item" param :path, :id, :integer, :required, "User Id" response :ok, "Success", :User response :unauthorized response :not_acceptable response :not_found end
get model list /api/v1/users/index
def index @user = User.all render :json => {users: @user} , :status => 200 end
get model by id /api/v1/users/show?id=1
def show @user = User.find(params[:id]) render :json => {user: @user}, :status => 200 end
create model
def create @user = User.new(user_params)
end
update model by id
def update end
delete model by id
def delete end
private def user_params params.require(:user).permit(:name,:password,:address) end
end
`
and the last , run command : SD_LOG_LEVEL=1 rake swagger:docs then generator a the doc file what has not action structure on apis .
{ "apiVersion": "1.0", "swaggerVersion": "1.2", "basePath": "http://localhost:3000", "apis": [ { "path": "/api/v1/users.{format}", "description": "User Management" } ], "authorizations": null, "info": { "title": "Swagger Sample App", "description": "This is a sample description.", "termsOfServiceUrl": "http://xxx", "contact": "xxx", "license": "Apache 2.0", "licenseUrl": "http://www.apache.org/licenses/LICENSE-2.0.html" } }