ruby-grape / ruby-grape/grape

Default param values from endpoint variables

Open
#785 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

You can access things like cookies and request from inside of an endpoint definition, but for my particular application, I need to give the requester the ability to override some params that should otherwise default to Rack environment values.

Example:

# config.ru
require 'grape'

class API < Grape::API
  format :json
  rescue_from :all

  params do
    optional :id, type: Integer, default: -> { cookies[:id] }
    optional :ip, default: -> { request.ip }
  end

  get do
    {
      params_id: params[:id],
      endpoint_id: cookies[:id],
      params_ip: params[:ip],
      endpoint_ip: request.ip,
    }
  end
end

run API
$ rackup
$ curl 'localhost:9292' && echo
{"error":"undefined local variable or method `cookies' for #<Grape::Validations::ParamsScope:0x007faec2846fe8>"}
$ curl 'localhost:9292?id=1' && echo
{"error":"undefined local variable or method `request' for #<Grape::Validations::ParamsScope:0x007faec2846fe8>"}
$ curl 'localhost:9292?id=1&ip=whatever' && echo
{"params_id":1,"endpoint_id":null,"params_ip":"whatever","endpoint_ip":"127.0.0.1"}

I could accomplish this with one-off lines like params[:id] ||= cookies[:id] in the endpoint, but I have to do this for a bunch of different endpoints, so I'd like to just be able to reuse the params (via use :such_and_such).

How hard would it be to allow defaults access to these Grape::Endpoint instance variables? It looks like you'd have access to cookies, headers, env, request, etc. at the point where you run the validators, and default values are populated via validation. But I don't want to just kludge a patch in there myself if there's a more elegant way of doing it than passing every-variable-under-the-sun to the validate! method.

Let me know what you think!

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 in lib/grape/endpoint.rb around the validator call linked in the issue, then read lib/grape/validations/validators/default.rb where default values are populated. Trace how validation context is passed and compare the documented cookies, request, headers, and env examples. Done means reusable parameter defaults can access endpoint request values while explicitly supplied parameters still take precedence.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api, backend
Issue type
Feature
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.