Grape API + Grape Swagger versioning issue

时间:2018-07-25 05:13:30

标签: ruby-on-rails swagger grape

Trying to take an unversioned Grape + Rails API and break it up into v1 and v2, and have followed a few different patterns posted online. But I can't quite get it to work correctly. I keep getting this unhelpful error when I got to get the swagger documentation:

Can't read swagger JSON from http://localhost:3000/api/v1/swagger_doc

I'm not sure where this is coming from, whether from my Grape API structure, the routes, or something with swagger. What am I doing wrong?

Gems are:

gem 'grape'
gem 'grape-entity'
gem 'grape-swagger'
gem 'grape-swagger-ui'

Files Structure:

app/
  api/
    v1/
      api.rb
      calendars.rb
      schedule_cycles.rb
      scheduled_events.rb
    v2/
      api.rb
      calendars.rb
      schedule_cycles.rb
      scheduled_events.rb
    api.rb

app/api/api.rb:

Dir[File.dirname(__FILE__) + '/v1/*.rb'].each do |file| 
  require file
end

Dir[File.dirname(__FILE__) + '/v2/*.rb'].each do |file| 
  require file
end

module API
  class Root < Grape::API
    prefix 'api'
    mount API::V1::Root 
    mount API::V2::Root

    add_swagger_documentation
  end
end

app/api/v1/api.rb:

Dir[File.dirname(__FILE__) + '/*.rb'].each do |file| 
  require file
end

module API
    module V1
      class Root < Grape::API
        prefix 'v1'
        mount API::V1::Calendars
        mount API::V1::ScheduleCycles
        mount API::V1::ScheduledEvents
      end
    end
end

app/api/v2/api.rb:

Dir[File.dirname(__FILE__) + '/*.rb'].each do |file| 
  require file
end

module API
    module V2
      class Root < Grape::API
        mount API::V2::Calendars
        mount API::V2::ScheduleCycles
        mount API::V2::ScheduledEvents
      end
    end
end

config/routes.rb:

Rails.application.routes.draw do
    mount API::Root => '/'

    namespace :api do
      namespace :v1 do

            resources :calendars do
                resources :schedule_cycles
                resources :scheduled_events
            end

        end
    end

    namespace :api do
      namespace :v2 do

            resources :calendars do
                resources :schedule_cycles
                resources :scheduled_events
            end

        end
    end

end

Then, my actual Grape API classes follow the format like this, from app/api/v1/schedule_cycles.rb:

module API
    module V1       
        class ScheduleCycles < Grape::API
            # configure whether it's developers only here
          helpers ApiHelpers::AuthenticationHelper
          # before { restrict_access_to_developers }
          before { authenticate! }

          format :json

            # ScheduleCycle endpoints here
          desc 'End-points for ScheduleCycles'
          namespace :schedule_cycles do
            desc 'Retrieve schedule_cycles'
            params do
              requires :token, type: String, desc: 'user token'
            end
            get do
                schedule_cycles = ScheduleCycle.all
                present schedule_cycles, with: Entities::ScheduleCycleEntity
            end

..

  end
end

1 个答案:

答案 0 :(得分:0)

我发现了这个问题,并发布了对我有用的方法,以防其他人发现它有用。这确实只是一个大刀阔斧的问题。首先,我需要将add_swagger_documentation移到实际版本Root类中,而不是移到父API Root类中,其次,我需要添加api_version参数来进行调整。我也将prefix参数移到了这些版本类。结果变为:

app / api / api.rb:

Dir[File.dirname(__FILE__) + '/v1/*.rb'].each do |file| 
  require file
end

Dir[File.dirname(__FILE__) + '/v2/*.rb'].each do |file| 
  require file
end

module API
  class Root < Grape::API
    mount API::V1::Root 
    mount API::V2::Root
  end
end

app / api / v1 / api.rb:

Dir[File.dirname(__FILE__) + '/*.rb'].each do |file| 
  require file
end

module API
    module V1
      class Root < Grape::API
        prefix 'api/v1'

        mount API::V1::Calendars
        mount API::V1::ScheduleCycles
        mount API::V1::ScheduledEvents

        add_swagger_documentation api_version: 'v1'
      end
    end
end

app / api / v2 / api.rb:

Dir[File.dirname(__FILE__) + '/*.rb'].each do |file| 
  require file
end

module API
    module V2
      class Root < Grape::API
        prefix 'api/v2'

        mount API::V2::Calendars
        mount API::V2::ScheduleCycles
        mount API::V2::ScheduledEvents

        add_swagger_documentation api_version: 'v2'
      end
    end
end