Grape: Helpers are not being inherited from parent classes

Created on 15 Apr 2017  路  8Comments  路  Source: ruby-grape/grape

Suppose I have a Base class with some basic stuff I want to include in every endpoint:

module Example
  module V1
    class Base < Grape::API
      helpers do
        params :user_params do
          requires :foo, type: Integer
        end
      end
    end
  end
end

I have a Root class which inherits from Base, where I mount my Users endpoint:

module Example
  module V1
    class Root < Base
      format :json
      mount Users::Root
    end
  end
end

The Users endpoint also inherits from Base:

module Example
  module V1
    module Users
      class Root < Base
        resource :users do
          mount Index
          mount Show
        end
      end
    end
  end
end

And here's the Show:

module Example
  module V1
    module Users
      class Show < Base
        desc 'Shows one user'
        params do
          requires :id, type: Integer
          use :user_params # <= This is the line that blows up
        end
        get ':id' do
          User.find(params[:id])
        end
      end
    end
  end
end

I would expect that the use statement in the Show endpoint would work because I expect that my :user_params helper is available, having been declared on Base. However, it does not. In fact the whole API fails to load because of that statement.

I am not sure if this is a bug, a feature, or user error, but I don't understand what's going on here. My only working solution is to put those helpers in a module that extends Grape::API::Helpers and include it on every single endpoint which needs that helper. In small projects this is trivial (and I probably wouldn't even bother with the helper module), but it would be very helpful to have this behavior in large projects.

Edit: I made a test project illustrating this here: https://github.com/flanger001/grape_example

bug? feature request

Most helpful comment

Please @pablonahuelgomez !

All 8 comments

I think helpers should be inherited, so feel free to fix this.

@dblock Cool, thanks for the confirmation. I don't know offhand how I'd go about fixing it, but I will try.

Begin by writing a failing spec!

Of course! I just meant I don't know what I will have to touch after said spec is written. 馃槃

@flanger001 @dblock gentlemen, I can take a look to this one if you don't mind

Please @pablonahuelgomez !

That would be awesome @pablonahuelgomez - I imagine this has something to do with load order, but I'm not sure how to go about fixing it.

@flanger001 @dblock please see #1665, it performs helpers inheritance.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jellybob picture jellybob  路  4Comments

dblock picture dblock  路  7Comments

jereynolds picture jereynolds  路  5Comments

nab0310 picture nab0310  路  4Comments

dblock picture dblock  路  7Comments