bploetz / versionist

A plugin for versioning Rails based RESTful APIs.
MIT License
972 stars 51 forks source link

Duplicated routes #92

Closed mikeheft closed 3 years ago

mikeheft commented 3 years ago

I recently added the gem to a project I'm working on in my spare time. Once I added the api_version to my routes file, it created duplicate routes.

recipe_scale  PUT    /recipes/:recipe_id/scale(.:format)  api/v1/recipes/recipes#scale
recipe_comments GET    /recipes/:recipe_id/comments(.:format) api/v1/recipes/comments#index
                POST   /recipes/:recipe_id/comments(.:format)   api/v1/recipes/comments#create
recipe_comment  GET    /recipes/:recipe_id/comments/:id(.:format)   api/v1/recipes/comments#show

api_v1_recipe_scale PUT  /api/v1/recipes/:recipe_id/scale(.:format)  api/v1/recipes/recipes#scale
api_v1_recipe_comments GET    /api/v1/recipes/:recipe_id/comments(.:format)  api/v1/recipes/comments#index
                                            POST   /api/v1/recipes/:recipe_id/comments(.:format)  api/v1/recipes/comments#create
api_v1_recipe_comment GET    /api/v1/recipes/:recipe_id/comments/:id(.:format) api/v1/recipes/comments#show
                                           PATCH  /api/v1/recipes/:recipe_id/comments/:id(.:format) api/v1/recipes/comments#update

Routes file:

Rails.application.routes.draw do
  api_version(module: 'Api::V1', path: { value: '/api/v1' }, header: { name: 'Content-Type', value: 'application/vnd.breadshop.com; version=1' }) do
    resources :recipes, module: :recipes, only: :show do
      put :scale

      resources :comments, except: %i[new edit]
    end

    namespace :users do
      resource :me, controller: :users, only: %i[create show update]

      resources :recipes, module: :recipes, except: %i[new edit]
    end

    resources :forums, module: :forums, except: %i[new edit] do
      put :activate
      delete :forget

      resources :comments, except: %i[new edit]
    end

  end
end

I'm not really sure what's happened here. I use this gem at work, and in other projects. I'm not sure if it's because I added it to an existing project and there's something, somewhere that needs to be removed. I would like to have the 'version' in the path, but not the extra routes that this is producing. I tried to remove the path, but the duplicated routes are still present.

Also, previously I was not able to successfully make requests without adding the headers. For some reason, now I am.

bploetz commented 3 years ago

This is because you're using multiple versioning strategies at the same time. The routes prefixed with recipe_ are from your header versioning strategy, and the routes prefixed with api_v1_recipe_ are from your path versioning strategy.