SwaggerCodegenRails
this plugin generate SWAGGER API document's templates for swagger-blocks, swagger_ui_engine.
Installation
Add this line to your application's Gemfile:
gem 'swagger_codegen_rails'And then execute:
$ bundleOr install it yourself as:
$ gem install swagger_codegen_railsUsage
Demo
$ rails g swagger:install
$ rails g swagger:init /api/v1
$ rails g swagger:add /api/v1 get /api/v1/user/:id id:path:integer:trueCreate intializer file
$ rails g swagger:installModify config/intializers/swagger_codegen_rails.rb, example below
# config/initializers/swagger_codegen_rails.rb
SwaggerCodegenRails.configure do |config|
config.versions_url = {
v1: 'api/v1',
v2: 'api/v2'
}
config.concern_dir = "/path/to/your/concern/directory"
# default: /app/controllers/concerns
config.schema_dir = "/path/to/your/schema/dir"
# default: /app/models/concerns
endOptions:
| configurations | type | default | description |
|---|---|---|---|
| config.versions_url | hash | EMPTY | define versioned path |
| config.concern_dir | string | /app/controllers/concern | set root path of endpoint documentations |
| config.schema_dir | string | /app/models/concern | set root path of response schema documentations |
To Create Namespace
$ rails g swagger:init NAMESPACETo add Endpoint
$ rails g swagger:add NAMESPACE HTTP_METHOD URI [PARAMETERS; name:in:type:required]
$ # If you do NOT want namespace, example below
$ rails g swagger:add . get /users id:path:integer:trueParameters:
| description | |
|---|---|
| name | parameter name |
| in | ex) in => /items?id=###, query => parameter is id. |
| type | data type |
| required | required |
To show SwaggerUI
Add these lines in your config/application.rb (If you won't create versioned API, it is no needed.)
# config/application.rb
config.paths.add "#{config.root}/app/controllers/**/*", glob: "**/*", eager_load: trueThen start your server with rails command
$ rails sAnd access http://localhost:3000/swagger in browser.
Contributing
- Fork it ( https://github.com/technobrain/swagger_codegen_rails )
- 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 new Pull Request
License
The gem is available as open source under the terms of the MIT License.