grape icon indicating copy to clipboard operation
grape copied to clipboard

Reusable params helpers are not inherited.

Open dim opened this issue 5 years ago • 2 comments

If you mount a child API within a parent API, all helper methods are correctly inherited from parent to child, but not params helpers, for example:

class Child < Grape::API
  get '/child' do
    respond_with_ok
  end
end

class Parent < Grape::API
  helpers do
    def respond_with_ok
      { status: 'ok' }
    end
  end

  get '/parent' do
    respond_with_ok
  end

  mount Child
end
$ curl http://127.0.0.1:8080/parent  # => {:status=>"ok"}
$ curl http://127.0.0.1:8080/child    # => {:status=>"ok"}

The same doesn't really work for params, e.g:

class Child < Grape::API
  params do 
    use :page
  end
  get '/child' do
    params
  end
end

class Parent < Grape::API
  helpers do
    params :page do
      optional :page, type: Integer
    end
  end

  params do 
    use :page
  end
  get '/parent' do
    params
  end

  mount Child
end

Results in a:

RuntimeError: Params :page not found!

dim avatar Oct 08 '20 14:10 dim

Turn it into a spec on HEAD?

dblock avatar Oct 08 '20 15:10 dblock

Sure, please see https://github.com/ruby-grape/grape/pull/2114 but I am not sure it's a simple fix. The params blocks are being evaluated at load time (and not e.g. on first access) which means that shared helpers are not even defined at this point.

dim avatar Oct 08 '20 15:10 dim