ruby-grape / ruby-grape/grape

Shared params don't get inherited like helper methods

Open
#1,761 1 comment 7 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug?
Dominant language
Ruby
Stars
10k
Forks
1.2k
Avg merge
14h 38m
Merged PRs (30d)
92

Description

The following code does not work:

# pagination.rb
module Pagination
  extend Grape::API::Helpers

  params :pagination do
    optional :page, type: Integer
    optional :per_page, type: Integer
  end

  def paginate(collection, page: params[:page], per_page: params[:per_page])
    collection.limit(per_page).offset((page - 1) * per_page)
  end
end

# posts.rb
module API
  class Posts < Grape::API
    namespace :posts do
      params do
        use :pagination
      end
      get do
        present :posts, paginate(Post.all)
      end
    end
  end
end

# base.rb
module API
  class Base < Grape::API
    helpers Pagination

    mount Posts
  end
end

when ran, it gives this error:

Params :pagination not found! (RuntimeError)

However this will work:

# posts.rb
module API
  class Posts < Grape::API
    helpers Pagination

    namespace :posts do
      params do
        use :pagination
      end
      get do
        present :posts, paginate(Post.all)
      end
    end
  end
end

and so will this:

# posts.rb
module API
  class Posts < Grape::API
    namespace :posts do
      params do
        optional :page, type: Integer
        optional :per_page, type: Integer
      end
      get do
        present :posts, paginate(Post.all)
      end
    end
  end
end

So the methods in helpers are available to mounted apis, but not the shared params.

I want to give a try at allowing this to work, could someone give me a starting point who is familiar with helpers / namespace stackable settings?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the helper and namespace stackable-settings behavior described in pagination.rb, posts.rb, and base.rb, then reproduce the Params :pagination not found! error with the mounted API example. Compare shared-parameter lookup with helper lookup and verify that the mounted API can resolve the shared params while preserving the existing working examples.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.