requires doc_attrs and a block
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.
Triaging old issues: this works on current master (0521ea5). The ArgumentError from new_scope went away when group options stopped being restricted to :type (1351d665, "don't restrict options to Hash/Array types"), so desc: and documentation: can now sit alongside a block:
class A < Grape::API
format :json
params do
requires :request, type: Hash, desc: 'Wrapper', documentation: { example: 'request:{}' } do
requires :dealer_id, type: Integer, default: 0, desc: 'Dealer', documentation: { example: 100001 }
end
end
post('/a') { declared(params) }
end
POST /a request[dealer_id]=3 -> 201 {"request":{"dealer_id":3}}
Closing. Please reopen if you still see it.