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

Helpers are not being inherited from parent classes #1617

Closed
flanger001 opened this issue Apr 15, 2017 · 8 comments · Fixed by #1665
Closed

Helpers are not being inherited from parent classes #1617

flanger001 opened this issue Apr 15, 2017 · 8 comments · Fixed by #1665

Comments

@flanger001
Copy link
Contributor

flanger001 commented Apr 15, 2017

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

@dblock
Copy link
Member

dblock commented Apr 15, 2017

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

@flanger001
Copy link
Contributor Author

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

@dblock
Copy link
Member

dblock commented Apr 15, 2017

Begin by writing a failing spec!

@flanger001
Copy link
Contributor Author

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

@pablonahuelgomez
Copy link
Contributor

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

@dblock
Copy link
Member

dblock commented Jul 21, 2017

Please @pablonahuelgomez !

@flanger001
Copy link
Contributor Author

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.

@pablonahuelgomez
Copy link
Contributor

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

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

Successfully merging a pull request may close this issue.

3 participants