I googled a little bit to find a proper and easy to implement way to generate Swagger documentation for an existing Rails API app. To be short, there are 2 ways of implementation: either via controllers, models or via Rspec controller/request specs. The list is not too long and the choice is not so easy to make:
Does anybody know other gems to generate Swagger documentation for an existing Rails API app ? Any suggestions are welcome ! Thank you.
The solution I came to is as to use rswag gem and rspec-rails-swagger gem - install rswag gem by adding the following in your Gemfile:
#Gemfile
gem 'rswag-api'
gem 'rswag-ui'
group :development, :test do
gem 'rspec-rails', '~> 3.8.1'
gem 'rspec-rails-swagger', '~> 0.1.5'
...
end
rails g rswag:install
to generate swagger_helper.rb
rails generate rspec:swagger PostsController
(adapt the name to your won controller you want to write the spec).bundle exec rake swagger
to generate a swagger.json
file.routes.rb
file:#../config/routes.rb
Rails.application.routes.draw do
mount Rswag::Ui::Engine => '/api-docs'
mount Rswag::Api::Engine => '/api-docs'
...#other routes come here
end
rails s
localhost:3000/api-docs
to see the generated Swagger documentation.Note: it works pretty good and replies to the client requirements, i.e.:
I removed rswag-specs
gem from Gemfile
because it could not validate response schema returned in JSON API format by active_model_serializers gem I use in my Rails API app. I had always to:
Now request specs are validated by RSpec and rspec-rails-swagger at the same time without hassle.
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With