Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global function definitions pollute Grape::API scope on Ruby 2.3.0 #1348

Closed
dblock opened this issue Apr 5, 2016 · 2 comments
Closed

Global function definitions pollute Grape::API scope on Ruby 2.3.0 #1348

dblock opened this issue Apr 5, 2016 · 2 comments

Comments

@dblock
Copy link
Member

dblock commented Apr 5, 2016

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.

@dblock
Copy link
Member Author

dblock commented Apr 5, 2016

I opened https://bugs.ruby-lang.org/issues/12251, lets at least work-around this.

@dblock
Copy link
Member Author

dblock commented Apr 5, 2016

cc: @namusyaka, should we just replace this with a Hashie::Mash or something else...?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant