2012-07-19 16:37:46 +08:00
# grape-swagger
2014-07-14 21:38:50 +08:00
[](https://travis-ci.org/tim-vandecasteele/grape-swagger)
2012-07-19 16:37:46 +08:00
## What is grape-swagger?
grape-swagger provides an autogenerated documentation for your [grape ](https://github.com/intridea/grape )-API. The generated documentation is Swagger-compliant, meaning it can easily be discovered in [Swagger UI ](https://github.com/wordnik/swagger-ui )
## Related projects
2014-07-14 21:43:23 +08:00
2012-07-19 16:37:46 +08:00
* [Grape ](https://github.com/intridea/grape )
* [Swagger UI ](https://github.com/wordnik/swagger-ui )
## Installation
2014-07-14 21:43:23 +08:00
Add to your Gemfile:
2012-07-19 16:37:46 +08:00
```gem 'grape-swagger'```
## Usage
2014-07-14 21:43:23 +08:00
2012-07-19 16:37:46 +08:00
Once you have the gem installed, mount all your different APIs (with ```Grape::API``` superclass) on a root node. In the root class definition, also include ```add_swagger_documentation```, this sets up the system and registers the documentation on '/swagger_doc.json'. Setup done, you can restart your local server now.
``` ruby
require 'grape-swagger'
module API
class Root < Grape::API
mount API::Cats
mount API::Dogs
mount API::Pirates
add_swagger_documentation
end
end
```
2013-07-27 03:12:20 +08:00
To explore your API, either download [Swagger UI ](https://github.com/wordnik/swagger-ui ) and set it up yourself or go to the [online swagger demo ](http://petstore.swagger.wordnik.com/ ) and enter your localhost url documentation root in the url field (probably something in the line of http://localhost:3000/swagger_doc.json).
2012-12-20 16:13:39 +08:00
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. You can do this by putting below code in your Grape API definition:
```` ruby
before do
header['Access-Control-Allow-Origin'] = '*'
header['Access-Control-Request-Method'] = '*'
end
````
2012-07-19 16:37:46 +08:00
2012-07-19 22:15:14 +08:00
## Configure
2014-07-14 21:43:23 +08:00
2012-07-19 22:15:14 +08:00
You can pass a hash with some configuration possibilities to ```add_swagger_documentation```, all of these are optional:
2014-05-27 00:45:13 +08:00
2014-07-15 00:00:29 +08:00
* ```:target_class``` The API class to document, default `self` .
* ```:mount_path``` The path where the API documentation is loaded, default '/swagger_doc'.
2014-05-27 00:45:13 +08:00
* ```:class_name```
2014-07-15 00:00:29 +08:00
* ```:markdown``` Allow markdown in `notes` , default `false` .
* ```:hide_format``` , Don't add '.(format)' to the end of URLs, default `false` .
* ```:api_version``` Version of the API that's being exposed.
2014-01-09 06:49:07 +08:00
* ```:base_path``` Basepath 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 the base_path.
2014-07-15 00:00:29 +08:00
* ```:authorizations``` Added to the `authorizations` key in the JSON documentation.
* ```:include_base_url``` Add base path to the URLs, default `true` .
* ```:root_base_path``` Add `basePath` key to the JSON documentation, default `true` .
2014-04-25 03:53:24 +08:00
* ```:models``` Allows adds an array with the entities for build models specifications. You need to use grape-entity gem.
2014-07-15 00:00:29 +08:00
* ```:hide_documentation_path``` Don't show the '/swagger_doc' path in the generated swagger documentation.
2014-05-27 00:45:13 +08:00
* ```:format```
2014-07-16 20:58:15 +08:00
* ```:info``` Added to the `info` key in the JSON documentation. `info` is a hash that 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.
2014-01-09 06:49:07 +08:00
2012-08-17 03:47:18 +08:00
## Swagger Header Parameters
2013-10-08 17:11:31 +08:00
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.
2012-08-17 03:47:18 +08:00
``` ruby
desc "Return super-secret information", {
2012-08-18 07:41:23 +08:00
headers: {
"XAuthToken" => {
2012-08-17 03:47:18 +08:00
description: "Valdates your identity",
2013-07-27 03:12:20 +08:00
required: true
2012-08-17 03:47:18 +08:00
},
2012-10-14 22:56:36 +08:00
"XOptionalHeader" => {
2012-08-17 03:47:18 +08:00
description: "Not reallly needed",
2013-07-27 03:12:20 +08:00
required: false
2012-08-17 03:47:18 +08:00
}
2012-08-18 07:41:23 +08:00
}
2012-08-17 03:47:18 +08:00
}
```
2013-10-08 17:11:31 +08:00
2014-07-14 21:43:23 +08:00
## Hiding an Endpoint
2013-10-08 17:11:31 +08:00
You can hide an endpoint by adding ```:hidden => true``` in the description of the endpoint:
``` ruby
desc 'Hide this endpoint', {
:hidden => true
}
```
## Grape Entities
2013-07-27 03:12:20 +08:00
Add the [grape-entity ](https://github.com/agileanimal/grape-entity ) gem to our Gemfile.
2014-02-19 19:25:21 +08:00
Please refer to the [grape-entity documentation ](https://github.com/intridea/grape-entity/blob/master/README.md )
2013-07-27 03:12:20 +08:00
for more details.
The following example exposes statuses. And exposes statuses documentation adding :type and :desc.
```ruby
module API
module Entities
class Status < Grape::Entity
2013-07-27 03:23:28 +08:00
expose :text, :documentation => { :type => "string", :desc => "Status update text." }
2013-07-27 03:12:20 +08:00
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
```
2012-08-17 03:47:18 +08:00
2014-04-25 03:53:24 +08:00
### Relationships
2014-07-14 21:43:23 +08:00
#### 1xN
2014-04-25 03:53:24 +08:00
```ruby
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
```
2014-07-14 21:43:23 +08:00
#### 1x1
2014-04-25 03:53:24 +08:00
2014-07-14 21:43:23 +08:00
Note: `is_array` is `false` by default.
2014-04-25 03:53:24 +08:00
```ruby
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
```
2014-07-14 21:43:23 +08:00
## Swagger Additions
2014-04-25 03:53:24 +08:00
2014-07-14 21:43:23 +08:00
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).
2012-08-02 16:11:37 +08:00
We're using [kramdown ](http://kramdown.rubyforge.org ) for parsing the markdown, specific syntax can be found [here ](http://kramdown.rubyforge.org/syntax.html ).
2014-07-14 21:43:23 +08:00
Be sure to enable markdown in the `add_swagger_documentation` with 'markdown: true'.
2012-08-02 16:11:37 +08:00
``` ruby
desc "Reserve a virgin in heaven", {
:notes => < < -NOTE
2014-07-14 21:43:23 +08:00
Virgins in Heaven
2012-08-02 16:11:37 +08:00
-----------------
> 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
}
```
2013-12-16 00:09:00 +08:00
You can also document the HTTP status codes that your API returns with this syntax:
``` ruby
get '/', :http_codes => [
[400, "Invalid parameter entry"],
] do
...
end
```
2012-07-19 16:37:46 +08:00
## Contributing to grape-swagger
2014-07-14 21:43:23 +08:00
See [CONTRIBUTING ](CONTRIBUTING.md ).
2012-07-19 16:37:46 +08:00
2014-07-14 21:43:23 +08:00
## Copyright and License
2012-07-19 16:37:46 +08:00
2014-07-14 21:43:23 +08:00
Copyright (c) 2012-2014 Tim Vandecasteele and contributors. See [LICENSE.txt ](LICENSE.txt ) for details.