The grape-swagger gem provides an autogenerated documentation for your Grape API. The generated documentation is Swagger-compliant, meaning it can easily be discovered in Swagger UI. You should be able to point the petstore demo to your API.
Add to your Gemfile:
gem 'grape-swagger'
Mount all your different APIs (with Grape::API
superclass) on a root node. In the root class definition, include add_swagger_documentation
, this sets up the system and registers the documentation on '/swagger_doc.json'. See test/api.rb for a simple demo.
require 'grape-swagger'
module API
class Root < Grape::API
mount API::Cats
mount API::Dogs
mount API::Pirates
add_swagger_documentation
end
end
To explore your API, either download Swagger UI and set it up yourself or go to the online swagger demo and enter your localhost url documentation root in the url field (probably something in the line of http://localhost:3000/swagger_doc.json).
If you use the online demo, make sure your API supports foreign requests by enabling CORS in Grape, otherwise you'll see the API description, but requests on the API won't return. Use rack-cors to enable CORS.
require 'rack/cors'
use Rack::Cors do
allow do
origins '*'
resource '*', headers: :any, methods: [ :get, :post, :put, :delete, :options ]
end
end
```
Alternatively you can set CORS headers in a Grape `before` block.
``` ruby
before do
header['Access-Control-Allow-Origin'] = '*'
header['Access-Control-Request-Method'] = '*'
end
You can pass a hash with optional configuration settings to add_swagger_documentation
.
The API class to document, default self
.
The path where the API documentation is loaded, default is /swagger_doc
.
API class name.
Allow markdown in notes
, default is false
. See below for details.
Don't add .(format)
to the end of URLs, default is false
.
Version of the API that's being exposed.
Base path of the API that's being exposed. This configuration parameter accepts a proc
to evaluate base_path
, useful when you need to use request attributes to determine its value.
This value is added to the authorizations
key in the JSON documentation.
Add base path to the URLs, default is true
.
Add basePath
key to the JSON documentation, default is true
.
A list of entities to document. Combine with the grape-entity gem. See below for details.
Don't show the /swagger_doc
path in the generated swagger documentation.
Documentation response format, default is :json
.
A hash merged into the info
key of the JSON documentation. This may contain:
:title
: The API title to be displayed on the API homepage.:description
: A description of the API.:contact
: Contact email.:license
: The name of the license.:license_url
: The URL of the license.:terms_of_service_url
: The URL of the API terms and conditions.
Customize the Swagger API documentation route, typically contains a desc
field. The default description is "Swagger compatible API description".
add_swagger_documentation \
api_documentation: { desc: 'Reticulated splines API swagger-compatible documentation.' }
Customize the Swagger API specific documentation route, typically contains a desc
field. The default description is "Swagger compatible API description for specific API".
add_swagger_documentation \
specific_api_documentation: { desc: 'Reticulated splines API swagger-compatible endpoint documentation.' }
Swagger also supports the documentation of parameters passed in the header. Since grape's params[]
doesn't return header parameters we can specify header parameters seperately in a block after the description.
desc "Return super-secret information", {
headers: {
"XAuthToken" => {
description: "Valdates your identity",
required: true
},
"XOptionalHeader" => {
description: "Not really needed",
required: false
}
}
}
You can hide an endpoint by adding hidden: true
in the description of the endpoint:
desc 'Hide this endpoint', hidden: true
You can specify a swagger nickname to use instead of the auto generated name by adding `:nickname 'string'``` in the description of the endpoint.
desc 'Get a full list of pets', nickname: 'getAllPets'
Add the grape-entity gem to our Gemfile.
The following example exposes statuses. And exposes statuses documentation adding :type and :desc.
module API
module Entities
class Status < Grape::Entity
expose :text, documentation: { type: 'string', desc: 'Status update text.' }
expose :links, using: Link, documentation: { type: 'link', is_array: true }
end
class Link < Grape::Entity
def self.entity_name
'link'
end
expose :href, documentation: { type: 'url' }
expose :rel, documentation: { type: 'string'}
end
end
class Statuses < Grape::API
version 'v1'
desc 'Statuses index', entity: API::Entities::Status
get '/statuses' do
statuses = Status.all
type = current_user.admin? ? :full : :default
present statuses, with: API::Entities::Status, type: type
end
end
end
module API
module Entities
class Client < Grape::Entity
expose :name, documentation: { type: 'string', desc: 'Name' }
expose :addresses, using: Entities::Address,
documentation: { type: 'Address', desc: 'Addresses.', param_type: 'body', is_array: true }
end
class Address < Grape::Entity
expose :street, documentation: { type: 'string', desc: 'Street.' }
end
end
class Clients < Grape::API
version 'v1'
desc 'Clients index', params: Entities::Client.documentation
get '/clients' do
...
end
end
add_swagger_documentation models: [Entities::Client, Entities::Address]
end
Note: is_array
is false
by default.
module API
module Entities
class Client < Grape::Entity
expose :name, documentation: { type: 'string', desc: 'Name' }
expose :address, using: Entities::Address,
documentation: { type: 'Address', desc: 'Addresses.', param_type: 'body', is_array: false }
end
class Address < Grape::Entity
expose :street, documentation: { type: 'string', desc: 'Street' }
end
end
class Clients < Grape::API
version 'v1'
desc 'Clients index', params: Entities::Client.documentation
get '/clients' do
...
end
end
add_swagger_documentation models: [Entities::Client, Entities::Address]
end
The grape-swagger gem allows you to add an explanation in markdown in the notes field. Which would result in proper formatted markdown in Swagger UI. The default Swagger UI doesn't allow HTML in the notes field, so you need to use an adapted version of Swagger UI (you can find one at https://github.com/tim-vandecasteele/swagger-ui/tree/vasco).
We're using kramdown for parsing the markdown, specific syntax can be found here.
Be sure to enable markdown in the add_swagger_documentation
with 'markdown: true'.
desc "Reserve a virgin in heaven", {
notes: <<-NOTE
Virgins in Heaven
-----------------
> A virgin doesn't come for free
If you want to reserve a virgin in heaven, you have to do
some crazy stuff on earth.
def do_good
puts 'help people'
end
* _Will go to Heaven:_ Probably
* _Will go to Hell:_ Probably not
NOTE
}
You can also document the HTTP status codes that your API returns with the following syntax.
get '/', http_codes: [
[400, "Invalid parameter entry"],
] do
...
end
See CONTRIBUTING.
Copyright (c) 2012-2014 Tim Vandecasteele and contributors. See LICENSE.txt for details.