Search code examples
ruby-on-railsjsonswaggerswagger-uigrape-api

Grape swagger documentation not loading


Below is the configuration I used to build API's using Grape.

But I am not able to build documentation using Swagger

Gems:

gem 'grape'
grape-0.13.0
gem 'grape-swagger-rails'
grape-swagger-rails-0.1.0
gem 'rack-cors', :require => 'rack/cors'
rack-cors-0.4.0

config/application.rb

require 'rack/cors'
config.paths.add File.join('app', 'api'), glob: File.join('**', '*.rb') 
config.autoload_paths += Dir[Rails.root.join('app', 'api', '**', '*.rb')]

config/initializers/swagger.rb

GrapeSwaggerRails.options.url      = '/swagger_doc.json'
GrapeSwaggerRails.options.app_url  = "http://api.lvh.me:3000"

config/routes.rb

  constraints(subdomain: 'api') do
    mount API::Base => '/'
  end
  mount GrapeSwaggerRails::Engine, at: "/apidocs"

app/api/api/base.rb

require 'grape-swagger'
module API
  class Base < Grape::API
    default_format :json
    mount API::V1::Base

    add_swagger_documentation(
      api_version: "v1",
      hide_documentation_path: true,
      mount_path: "/",
      hide_format: true
    )
  end
end

Issue:

When I hit http://lvh.me:3000/apidocs. I'm getting below error below green Nav bar.

Can't read swagger JSON from http://api.lvh.me:3000/swagger_doc.json

When I hit http://api.lvh.me:3000/swagger_doc.json. Getting below response.

{"error":"Not Found"}

I am not able to figure out what am doing wrong. Need help in rendering Swagger documentation with grape framework.


Solution

  • In config/initializers/swagger.rb you set the URL to your schema to /swagger_doc.json and to mount_path in the method add_swagger_documentation you pass '/'. Shoudn't it be /swagger_doc ?