Grape: Still having issues with helper inheritance

Created on 12 Nov 2018  路  6Comments  路  Source: ruby-grape/grape

I thought this was supposed to be fixed with #1665, but I am still having issues with it. I have a few classes inheriting from Grape::API and I want to avoid having to include the helpers in every endpoint class:

require "#{Rails.root}/app/api/v2/helpers/authentication_helper.rb"

module API
  module V2

    class Base < Grape::API
      prefix 'v2'
      format :json
    end

    class AuthenticatedBase < Base
       helpers ApiHelpers::AuthenticationHelper
       before { authenticate! }
    end

    class AdminBase < Base
      # other things in here for higher security routes
    end

  end
end

However, when I go to make my end point class inherit, I can't use the AuthenticationHelper methods like current_user, for example.

require "#{Rails.root}/app/api/v2/base_classes.rb"

module API
    module V2

        class Sensors < AuthenticatedBase

          desc 'End-points for Sensors'

          namespace :sensors do

...

Is this intended? What am I missing?

bug?

Most helpful comment

Any news on that? I'm also running into that issue. :(

All 6 comments

Try to turn this into a spec?

Just want to confirm I am seeing this as well. If I have a bit to spare I'll try to write a spec. But basically any helpers block I declare in a base class does not appear to be in my child class.

I went to upgrade today and before (in 1.0~) I had

API < Grape::API
  helpers SomeHelper
  mount AnEndpoint
  mount AnotherEndpoint
end

And the helper would be picked up by all of the mounts, but now that doesn't seem to be the case. The sub-mounts can't seem to find the helpers. If this is just something I'm missing from the upgrade on my end that could help you. But maybe it just broke because of the change in how Grape mounts works with the new Grape::API::instance

Have you upgraded to 1.2.2 ? Or are you in a prior version? (Can we open a new issue?)

On my test - this seems to fail to load Params :requires_toggle_prm not found! on all versions.
It doesn't seem to be specific to the latest one.
Not really sure what am I doing wrong?
#helpers_spec.rb

        context 'when including a helper on a top-level api' do
          let(:api) do
            Class.new(Grape::API) do
              params { use :requires_toggle_prm }
              get('ping') { 'ok' }
            end
          end

          let(:top_level) do
            mounted_api = api
            Class.new(Grape::API) do
              helpers BooleanParam
              mount mounted_api
            end
          end

          def app
            top_level
          end

          it 'provides access to the top level helpers in the API' do
            expect((get 'ping').status).to eq 400
          end
        end

Any news on that? I'm also running into that issue. :(

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dblock picture dblock  路  7Comments

nab0310 picture nab0310  路  4Comments

dblock picture dblock  路  6Comments

zherr picture zherr  路  4Comments

wxianfeng picture wxianfeng  路  8Comments