Closed
Description
Global function definitions pollute Grape::API scope on Ruby 2.3.0
require 'spec_helper'
def namespace
raise 'should never be called'
end
describe Grape::API do
subject do
Class.new(Grape::API) do
format :json
get do
{ ok: true }
end
end
end
def app
subject
end
context 'with Rake::DSL' do
it 'works' do
get '/'
expect(last_response.status).to eq 200
end
end
end
This fails on Ruby 2.3.0 with
1) Grape::API with Rake::DSL works
Failure/Error: raise 'should never be called'
RuntimeError:
should never be called
# ./spec/grape/integration/global_namespace_function_spec.rb:4:in `namespace'
# ./lib/grape/router/attribute_translator.rb:29:in `block in to_h'
# ./lib/grape/router/attribute_translator.rb:28:in `each'
# ./lib/grape/router/attribute_translator.rb:28:in `each_with_object'
# ./lib/grape/router/attribute_translator.rb:28:in `to_h'
# ./lib/grape/endpoint.rb:134:in `block (2 levels) in mount_in'
# ./lib/grape/endpoint.rb:132:in `each'
# ./lib/grape/endpoint.rb:132:in `block in mount_in'
# ./lib/grape/endpoint.rb:127:in `each'
# ./lib/grape/endpoint.rb:127:in `mount_in'
# ./lib/grape/api.rb:103:in `block in initialize'
# ./lib/grape/api.rb:102:in `each'
# ./lib/grape/api.rb:102:in `initialize'
# ./lib/grape/api.rb:26:in `new'
# ./lib/grape/api.rb:26:in `compile'
# ./lib/grape/api.rb:39:in `block in call'
# ./lib/grape/api.rb:39:in `synchronize'
# ./lib/grape/api.rb:39:in `call'
# /Users/dblock/.rvm/gems/ruby-2.3.0/gems/rack-test-0.6.3/lib/rack/mock_session.rb:30:in `request'
# /Users/dblock/.rvm/gems/ruby-2.3.0/gems/rack-test-0.6.3/lib/rack/test.rb:244:in `process_request'
# /Users/dblock/.rvm/gems/ruby-2.3.0/gems/rack-test-0.6.3/lib/rack/test.rb:58:in `get'
# ./spec/grape/integration/global_namespace_function_spec.rb:23:in `block (3 levels) in <top (required)>'
Thanks to @dylanfareed for the repro, took a while to narrow this down.
The namespace
function is also defined by Rake, so the most common way of seeing this is with include Rake::DSL
which is something that fabricator does, for example.