Skip to content

Add swagger compliant documentation to your grape API

License

Notifications You must be signed in to change notification settings

jhecking/grape-swagger

 
 

Repository files navigation

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

Related projects

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
  • :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 to 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 
    }
  }
}

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
}

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

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

About

Add swagger compliant documentation to your grape API

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 100.0%