bploetz / versionist

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

Versionist doesn't deal with Rails::Application.reload_routes! correctly #3

Closed nicolas-brousse closed 12 years ago

nicolas-brousse commented 12 years ago

I've started a new project, and I want to define one default api version. But when I launch the server, I have "attempt set more than one default api version" error. While I have juste one api version defined.

MyProject::Application.routes.draw do

  #
  # ApiVersions
  #
  api_version(:module => "API::V0__1__0",
              :header => "Accept", 
              :value => "application/vnd.mycompany.com-v0.1.0",
              :default => true) do
    get '/test.(:format)' => 'base#test'
  end

  root :to => 'index#index'
end
bploetz commented 12 years ago

Hey Nicolas,

I've tried reproducing this issue locally without any luck. The defaulting behavior works just fine for me.

I created a fresh Rails 3.0 app, and my config/routes.rb file looks like:

TestApi::Application.routes.draw do
 api_version(:module => "API::V0__1__0",
             :header => "Accept",
             :value => "application/vnd.mycompany.com-v0.1.0",
             :default => true) do
   get '/test.(:format)' => 'base#test'
 end

 root :to => 'index#index'
end

Server startup:

[bploetz:~/workspace/test-api]> rails server
=> Booting WEBrick
=> Rails 3.0.0 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2012-03-15 09:40:03] INFO  WEBrick 1.3.1
[2012-03-15 09:40:03] INFO  ruby 1.9.2 (2010-08-18) [x86_64-darwin10.8.0]
[2012-03-15 09:40:03] INFO  WEBrick::HTTPServer#start: pid=2043 port=3000

The controller (in app/controllers/api/v0_1_0/base_controller.rb):

class API::V0_1_0::BaseController < ApplicationController
  def test
    render :text => "default"
  end
end

The request:

[bploetz:~]> curl localhost:3000/test.json
default

Server log output for the request:

Started GET "/test.json" for 127.0.0.1 at 2012-03-15 09:40:06 -0400
  Processing by API::V0_1_0::BaseController#test as JSON
Rendered text template (0.0ms)
Completed 200 OK in 3ms (Views: 2.8ms | ActiveRecord: 0.0ms)

Can you push the entire Rails app where you're seeing this issue up to Github so that I can clone it to take a look?

Also, can you tell me:

Thanks.

nicolas-brousse commented 12 years ago

Thanks a lot for your quick response.

I use :

RUBYGEMS VERSION: 1.8.6
RUBY VERSION: 1.9.2 (2011-07-09 patchlevel 290) [x86_64-darwin11.0.0]
RAILS : 3.1.4

Bundle list :

Gems included by the bundle:
  * actionmailer (3.1.4)
  * actionpack (3.1.4)
  * activeadmin (0.4.3)
  * activemodel (3.1.4)
  * activerecord (3.1.4)
  * activeresource (3.1.4)
  * activesupport (3.1.4)
  * ansi (1.4.2)
  * arel (2.2.3)
  * bcrypt-ruby (3.0.1)
  * bourbon (1.4.0)
  * builder (3.0.0)
  * bundler (1.1.0)
  * coffee-rails (3.1.1)
  * coffee-script (2.2.0)
  * coffee-script-source (1.2.0)
  * devise (2.0.4)
  * erubis (2.7.0)
  * execjs (1.3.0)
  * fastercsv (1.5.4)
  * formtastic (2.1.1)
  * has_scope (0.5.1)
  * hike (1.2.1)
  * i18n (0.6.0)
  * inherited_resources (1.3.1)
  * jquery-rails (1.0.19)
  * json (1.6.5)
  * kaminari (0.13.0)
  * mail (2.3.3)
  * meta_search (1.1.3)
  * mime-types (1.17.2)
  * multi_json (1.1.0)
  * orm_adapter (0.0.6)
  * pg (0.13.2)
  * polyamorous (0.5.0)
  * polyglot (0.3.3)
  * rack (1.3.6)
  * rack-cache (1.2)
  * rack-mount (0.8.3)
  * rack-ssl (1.3.2)
  * rack-test (0.6.1)
  * rails (3.1.4)
  * railties (3.1.4)
  * rake (0.9.2.2)
  * rdoc (3.12)
  * responders (0.8.0)
  * sass (3.1.15)
  * sass-rails (3.1.5)
  * sprockets (2.0.3)
  * thor (0.14.6)
  * tilt (1.3.3)
  * treetop (1.4.10)
  * turn (0.9.3)
  * tzinfo (0.3.32)
  * uglifier (1.2.3)
  * versionist (0.1.1)
  * warden (1.1.1)
bploetz commented 12 years ago

OK, let me try with this combo on my end. Stay tuned......

bploetz commented 12 years ago

Can you also send me what's in your Gemfile?

nicolas-brousse commented 12 years ago

Yes

source 'http://rubygems.org'

gem 'rails', '3.1.4'

# Bundle edge Rails instead:
# gem 'rails',     :git => 'git://github.com/rails/rails.git'

gem 'pg'
gem 'activeadmin'
gem 'versionist'

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails', "  ~> 3.1.4"
  gem 'coffee-rails', "~> 3.1.1"
  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'

# Use unicorn as the web server
# gem 'unicorn'

# Deploy with Capistrano
# gem 'capistrano'

# To use debugger
# gem 'ruby-debug19', :require => 'ruby-debug'

group :test do
  # Pretty printed test output
  gem 'turn', :require => false
end
bploetz commented 12 years ago

OK, I created a new Rails 3.1.4 app running on Ruby 1.9.2-p290 and am still not seeing this error on server startup:

[bploetz:~/workspace/test-api]> ruby -v
ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin10.8.0]

[bploetz:~/workspace/test-api]> rails server
=> Booting WEBrick
=> Rails 3.1.4 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2012-03-15 10:10:39] INFO  WEBrick 1.3.1
[2012-03-15 10:10:39] INFO  ruby 1.9.2 (2011-07-09) [x86_64-darwin10.8.0]
[2012-03-15 10:10:39] INFO  WEBrick::HTTPServer#start: pid=3833 port=3000

The defaulting seems to still work as well. The request:

[bploetz:~]> curl localhost:3000/test.json
default
Started GET "/test.json" for 127.0.0.1 at 2012-03-15 10:10:41 -0400
  Processing by API::V0_1_0::BaseController#test as JSON
Rendered text template (0.0ms)
Completed 200 OK in 26ms (Views: 25.7ms | ActiveRecord: 0.0ms)

However, I do see this error when running rake routes:

[bploetz:~/workspace/test-api]> rake routes
rake aborted!
attempt set more than one default api version

Digging into why that's happening.....

nicolas-brousse commented 12 years ago

I have a little idea. Before start Webrick server, do you have launch the activeadmin installation ? It's possible that activeadmin has a setting that conflicts with versionists...

bploetz commented 12 years ago

I've never used activeadmin before, but I think I know what's going on. Something is reloading the routes (i.e. Rails::Application.reload_routes!, which is what happens when you run rake routes, where I'm seeing the error as noted above), and Versionist is not dealing with this correctly. Working on a fix now, stay tuned......

bploetz commented 12 years ago

Yup, confirmed that setting up activeadmin then starting server causes this error, and it is in fact caused by routes reloading:

[bploetz:~/workspace/test-api]> rails server
=> Booting WEBrick
=> Rails 3.1.4 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Exiting
/Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/versionist-0.1.1/lib/versionist/versioning_strategy/base.rb:14:in `initialize': attempt set more than one default api version (ArgumentError)
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/versionist-0.1.1/lib/versionist/versioning_strategy/header.rb:12:in `initialize'
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/versionist-0.1.1/lib/versionist/routing.rb:28:in `new'
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/versionist-0.1.1/lib/versionist/routing.rb:28:in `configure_header'
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/versionist-0.1.1/lib/versionist/routing.rb:16:in `api_version'
    from /Users/bploetz/workspace/test-api/config/routes.rb:9:in `block in <top (required)>'
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/actionpack-3.1.4/lib/action_dispatch/routing/route_set.rb:258:in `instance_exec'
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/actionpack-3.1.4/lib/action_dispatch/routing/route_set.rb:258:in `eval_block'
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/actionpack-3.1.4/lib/action_dispatch/routing/route_set.rb:235:in `draw'
    from /Users/bploetz/workspace/test-api/config/routes.rb:1:in `<top (required)>'
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/activesupport-3.1.4/lib/active_support/dependencies.rb:234:in `load'
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/activesupport-3.1.4/lib/active_support/dependencies.rb:234:in `block in load'
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/activesupport-3.1.4/lib/active_support/dependencies.rb:223:in `block in load_dependency'
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/activesupport-3.1.4/lib/active_support/dependencies.rb:640:in `new_constants_in'
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/activesupport-3.1.4/lib/active_support/dependencies.rb:223:in `load_dependency'
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/activesupport-3.1.4/lib/active_support/dependencies.rb:234:in `load'
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/railties-3.1.4/lib/rails/application/routes_reloader.rb:29:in `block in load_paths'
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/railties-3.1.4/lib/rails/application/routes_reloader.rb:29:in `each'
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/railties-3.1.4/lib/rails/application/routes_reloader.rb:29:in `load_paths'
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/railties-3.1.4/lib/rails/application/routes_reloader.rb:13:in `reload!'
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/railties-3.1.4/lib/rails/application/routes_reloader.rb:7:in `block in initialize'
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/activesupport-3.1.4/lib/active_support/file_update_checker.rb:32:in `call'
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/activesupport-3.1.4/lib/active_support/file_update_checker.rb:32:in `execute_if_updated'
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/railties-3.1.4/lib/rails/application/finisher.rb:63:in `block (2 levels) in <module:Finisher>'
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/railties-3.1.4/lib/rails/application/finisher.rb:64:in `call'
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/railties-3.1.4/lib/rails/application/finisher.rb:64:in `block in <module:Finisher>'
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/railties-3.1.4/lib/rails/initializable.rb:30:in `instance_exec'
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/railties-3.1.4/lib/rails/initializable.rb:30:in `run'
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/railties-3.1.4/lib/rails/initializable.rb:55:in `block in run_initializers'
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/railties-3.1.4/lib/rails/initializable.rb:54:in `each'
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/railties-3.1.4/lib/rails/initializable.rb:54:in `run_initializers'
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/railties-3.1.4/lib/rails/application.rb:96:in `initialize!'
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/railties-3.1.4/lib/rails/railtie/configurable.rb:30:in `method_missing'
    from /Users/bploetz/workspace/test-api/config/environment.rb:5:in `<top (required)>'
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/activesupport-3.1.4/lib/active_support/dependencies.rb:240:in `require'
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/activesupport-3.1.4/lib/active_support/dependencies.rb:240:in `block in require'
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/activesupport-3.1.4/lib/active_support/dependencies.rb:223:in `block in load_dependency'
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/activesupport-3.1.4/lib/active_support/dependencies.rb:640:in `new_constants_in'
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/activesupport-3.1.4/lib/active_support/dependencies.rb:223:in `load_dependency'
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/activesupport-3.1.4/lib/active_support/dependencies.rb:240:in `require'
    from /Users/bploetz/workspace/test-api/config.ru:4:in `block in <main>'
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/rack-1.3.6/lib/rack/builder.rb:51:in `instance_eval'
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/rack-1.3.6/lib/rack/builder.rb:51:in `initialize'
    from /Users/bploetz/workspace/test-api/config.ru:1:in `new'
    from /Users/bploetz/workspace/test-api/config.ru:1:in `<main>'
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/rack-1.3.6/lib/rack/builder.rb:40:in `eval'
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/rack-1.3.6/lib/rack/builder.rb:40:in `parse_file'
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/rack-1.3.6/lib/rack/server.rb:200:in `app'
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/railties-3.1.4/lib/rails/commands/server.rb:46:in `app'
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/rack-1.3.6/lib/rack/server.rb:301:in `wrapped_app'
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/rack-1.3.6/lib/rack/server.rb:252:in `start'
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/railties-3.1.4/lib/rails/commands/server.rb:70:in `start'
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/railties-3.1.4/lib/rails/commands.rb:54:in `block in <top (required)>'
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/railties-3.1.4/lib/rails/commands.rb:49:in `tap'
    from /Users/bploetz/.rvm/gems/ruby-1.9.2-p290@test-api/gems/railties-3.1.4/lib/rails/commands.rb:49:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

Working on a fix. Stay tuned......

bploetz commented 12 years ago

This has been fixed in versionist 0.1.2. Can you update your dependency on versionist and let me know if it's working for you now?

nicolas-brousse commented 12 years ago

I just tested, and I think it's good :) I thank you for your speed.

bploetz commented 12 years ago

Awesome, thanks