grape-swagger

What is grape-swagger?

grape-swagger provides an autogenerated documentation for your grape-API. The generated documentation is Swagger-compliant, meaning it can easily be discovered in Swagger UI

Installation

grape-swagger is available as a gem, install it simply via the commandline:

gem install grape-swagger

or add to your Gemfile:

gem 'grape-swagger'

Usage

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.

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. You can do this by putting below code in your Grape API definition:

before do
    header['Access-Control-Allow-Origin'] = '*'
    header['Access-Control-Request-Method'] = '*'
end

Configure

You can pass a hash with some configuration possibilities to add_swagger_documentation, all of these are optional:

  • :mount_path The path were the API documentation is loaded, default '/swagger_doc'
  • :api_version Version of the API that's being exposed
  • :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.
  • :markdown Allow markdown in notes, default false
  • :hide_documentation_path Don't show the '/swagger_doc' path in the generated swagger documentation

Swagger Header Parameters

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 reallly needed",
      required: false
    }
  }
}

Hiding an endpoint

You can hide an endpoint by adding :hidden => true in the description of the endpoint:

desc 'Hide this endpoint', {
  :hidden => true
}

Grape Entities

Add the grape-entity gem to our Gemfile. Please refer to the grape-entity documentation for more details.

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." }
    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

Swagger additions

grape-swagger 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 call: ':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 this syntax:

get '/', :http_codes => [
  [400, "Invalid parameter entry"],
] do
  ...
end

Contributing to grape-swagger

  • Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
  • Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
  • Fork the project.
  • Start a feature/bugfix branch.
  • Commit and push until you are happy with your contribution.
  • Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright (c) 2012 Tim Vandecasteele. See LICENSE.txt for further details.