Skip to content

Helpers are not being inherited from parent classes #1617

Closed
@flanger001

Description

@flanger001

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions