1
votes

I am trying to use grape-swagger gem for creating the auto-generated documentation for my Grape API.

Using 'grape' gem I have created few APIs.

Example:

http://localhost:9292/api/v1/charges

This API is working fine.

Following documentation of 'grape-sagger', I am not able to generate the API documentation properly.

My steps:

1) I added below in the gemfile

gem 'grape-swagger'

2) I also used rack-cors to enable CORS by adding below in config.ru

require 'rack/cors'
use Rack::Cors do
  allow do
    origins '*'
    resource '*', headers: :any, methods: [ :get, :post, :put, :delete, :options ]
  end
end

Also added below in gemfile.

gem 'rack-cors', :require => 'rack/cors'

3) Also I added below at the end in my API class

add_swagger_documentation

But when I run http://localhost:9292/api/v1/swagger_doc, I am not getting the proper paths. I need the API path like http://localhost:9292/api/v1/charges, but it is returning as http://localhost:9292/api/v1/swagger_doc/charges

Do I need to set any other configuration?

1

1 Answers

1
votes

In end of api class add below params for hide documentation path:

add_swagger_documentation hide_documentation_path: true, api_version: 'v1'

If you want to use this gem grape-swagger-rails then use this configuration as per documentation:

Then create file named config/intializers/swagger.rb and add this line for customization:

GrapeSwaggerRails.options.url      = "/api/v1/swagger_doc"
GrapeSwaggerRails.options.app_url  = "#{ENV['APPLICATION_URL']}"

where you can set APPLICATION_URL in environment variables like : localhost:3000

And your config/routes.rb file set route as

mount GrapeSwaggerRails::Engine => '/swagger'

Then you can access all docs from here: localhost:3000/swagger

This way you can customize your swagger documentation with grape api.