ruby-grape / ruby-grape/grape

requires doc_attrs and a block

Open
#751 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I was trying to write something along the lines of

params do 
  requires :request, type:Hash, desc: 'Request Wrapper required for all API requests', documentation:{example: 'request:{}'} do 
    requires :dealer_id, type:Integer,default: 0, desc: 'Dealer Id Number', documentation:{example: 100001}
  end
end

Which seemed like it should work appropriately as I need to document the wrapper as well as the parameters inside it but I was receiving an standard ArgumentError. So I started digging through the source and traced it all down to Grape::Validations::ParamsScope#new_scope

def new_scope(attrs, optional = false, &block)
  opts = attrs[1] || { type: Array }
  raise ArgumentError unless opts.keys.to_set.subset? [:type].to_set
  ParamsScope.new(api: @api, element: attrs.first, parent: self, optional: optional, type: opts[:type], &block)
end

My issue is why are we checking to make sure that my options are a subset? of a single element wouldn't equality work better here? Unless of course we are looking to see if [:type].to_set is a #subset? of my opts which seems more like the intended behavior in which case I am proposing changing #new_scope to

def new_scope(attrs, optional = false, &block)
  opts = attrs[1] || { type: Array }
  raise ArgumentError unless opts.keys.to_set.superset? [:type].to_set
  ParamsScope.new(api: @api, element: attrs.first, parent: self, optional: optional, type: opts[:type], &block)
end

This would then allow me to use #requires with doc_attrs and a block. Does this some how cause interference elsewhere that I am overlooking? Can someone explain to me why you would check to see if {type: Array}.keys.to_set is a subset of [:type].to_set when this truely evaluates to {type: Array}.keys.to_set == [:type].to_set? because a subset of a single item must be equal to that single item.

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 at Grape::Validations::ParamsScope#new_scope and reproduce the nested requires example from the issue. Inspect how its option-key check handles doc_attrs and blocks; the work is done when the example no longer raises ArgumentError and the behavior is covered by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.