Grape API + Grape Swagger 版本控制问题

Grape API + Grape Swagger versioning issue

尝试采用未版本化的 Grape + Rails API 并将其分解为 v1 和 v2,并遵循了在线发布的几种不同模式。但我不能让它正常工作。当我获得 swagger 文档时,我不断收到这个无用的错误:

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

我不确定这是从哪里来的,无论是来自我的 Grape API 结构、路线,还是来自招摇过市的东西。我做错了什么?

宝石是:

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

文件结构:

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

然后,我的实际葡萄API 类遵循这样的格式,来自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

我已经弄明白了,并且正在发布对我有用的内容,以防其他人发现它有用。这真的只是一个招摇的问题。首先,我需要将 add_swagger_documentation 移动到实际版本 Root classes 而不是父版本 API Root class,其次,我需要为 swagger 添加 api_version 参数。我也将 prefix 参数移动到这些版本 classes。结果变为:

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