Grape version issue

359 Views Asked by At

I have two simple API classes:

class API::Root < Grape::API
    version 'v1', :using => :path

    mount API::Appointments => '/appointments'
end

and

class API::Appointments < API::Base
    get do
    end 
end

The issue is that API::Appointments generates route:

GET        /appointments/v1

as opposed to

GET        /v1/appointments

Is this how it's suppose to be? What am I doing wrong? Shouldn't version path component be before any other path component as oppose to in the end?

Thanks

1

There are 1 best solutions below

0
On

From the example you gave, your resource file (appointments) is inheriting from the root.rb file, which uses a :path to get the version. Additionally, you are mounting to a path by using mount API::Appointments => '/appointments', this acts much like a prefix and thus will prefix your routes with /appointments, so the routes you are seeing are expected given your set up. When I version a JSON api using Grape, I have a base root.rb class which will mount my versioned APIs.

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

Then for each additional API version, I have another root.rb file which then mounts the resource files for that particular API.

module API
  module V1
    class Root < Grape::API
      version 'v1' using: :path
      mount API::V1::Appointments
    end
  end
end

With this structure you can have a resource class

module API
  module V1
    class Appointments < Grape::API
      get do
      end 
    end
  end
end

and it will be mounted to the route /v1/appointments