Swagui
a Rack middleware app and a command-line utility that helps you render swagger-ui, a great api documentation tool, with ease and grace.
Install
Add this line to your application's Gemfile:
group :development do
gem 'swagui'
end
Usage
Rack middleware
In your application.rb
(for Rails app) or config.ru
(for Rack app), add Swigui middleware close to the top of the middleware stack. For example, in a config.ru
file, add the following line:
if Rack.env == 'development'
use Swagui::App, url: '/doc', path: 'doc'
end
-
url
: the url path to access the swagger-ui. -
path
: the sub-directory name where the swagger files are stored. It assumes thatapi-docs
is the index file in the directory. - basic auth block: in case the doc site need to be protected by http basic auth, you can configure it by a block, for example:
use Swagui::App, url: '/doc', path: 'doc' do |username, password|
[username, password] == ['admin', 'admin']
end
You will be able to access the swagger-ui loaded with your api documentation at /doc
(your url
setting) when the server starts.
doc file format
Swagui
supports three types of doc format
- JSON documents with no extension (original swagger doc format), such as
api-docs
andaccount
. - JSON documents with
.json
extension, such asapi-docs.json
andaccount.json
. - YAML documents. This the recommended approach as it is naturally more concise than json and also tries to be more opinionated in the following ways:
-
auto listing: if not provided in
api-docs.yml
, the list of apis underapis
is automatically populated by the names of all the .yml files in the same directory. -
auto "Try it out": the
basePath
attribute of each doc, used fortry it out
feature, if unprovided, is automatically provided based on the host application. this assumes the doc is hosted as part of the application. -
schema-based models: to circumvent all the rather complex and tedious syntax for
models
, schema attributes underapis/operations/parameters
(for request body json schema) andapis/operations/responseMessages
(for response body json schema), and it will be used to automatically populate themodels
under root. -
template: in
api-docs.yml
, atemplate
section can be added that lists all the general attributes for all the apis, for example if you apis is a JSON api, chances are that they all consumes JSON requests and produces JSON responses. IN this case, instead of adding the following line in each service yml file, you can simply add this section toapi-docs.yml
.
-
auto listing: if not provided in
template:
produces:
- application/json
consumes:
- application/json
-
example value: in current swagger ui, according to swagui ui default value code, the render json structure is displayed with some empty values, such as
0
,0.0
,""
, etc, depends on the data type. This could make it hard to visualize how the json structure really looks like. Swagui provides an optionalexample
attribute for each property, for the sample value that populates the json display.
With this approach, swagger api docs are now a lot more concise and readible, let alone having dynamic basePath
.
sample api-docs.yml
---
info:
title: "a sojourner's api"
description: "Ut? Amet, et eros nascetur parturient diam scelerisque, egestas, pulvinar sit cum, rhoncus eros vel urna aliquam massa! Turpis purus auctor proin aliquam nunc, nec proin vel enim est, scelerisque! Ac vel integer proin sed in."
contact: "sojourner@world.net"
apiVersion: "1.0.0"
swaggerVersion: "1.2"
authorizations: {}
sample account.yml
---
produces:
- application/json
apis:
-
path: /account
description: create or update a account
operations:
-
method: POST
summary: create or update an account
nickname: PostAccount
parameters:
-
name: body
required: true
schema: test/doc/schemas/account_post_request_body.schema.json
paramType: body
responseMessages:
-
code: 200
message: account creation success
schema: test/doc/schemas/account_post_creation_success.schema.json
-
code: 400
message: account creation failure
schema: test/doc/schemas/account_post_creation_failure.schema.json
Command-line utility
Sometimes, you only want to see the documentation without starting the entire application. All you need to do is to run the following command in your documentation directory:
$ bundle exec swagui
optionally, you can specify the directory name as a command line argument:
$ bundle exec swagui doc
You will then able to view the swagger-ui loaded with your api documentation at http://localhost:9292
Contributing
- Fork it ( https://github.com/jackxxu/swagui/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request