Skip to content

Commit 535fd7d

Browse files
myitcvdblock
authored andcommitted
Fix: calling declared(params) from child namespaces fails to include parent namespace defined params.
1 parent 62b8c90 commit 535fd7d

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Next Release
1818
* [#495](https://github.com/intridea/grape/pull/495): Fix `ParamsScope#params` for parameters nested inside arrays - [@asross](https://github.com/asross).
1919
* [#498](https://github.com/intridea/grape/pull/498): Dry up options and headers logic, allow headers to be passed to OPTIONS requests - [@karlfreeman](https://github.com/karlfreeman).
2020
* [#500](https://github.com/intridea/grape/pull/500): Skip entity auto-detection when explicitely passed - [@yaneq](https://github.com/yaneq).
21+
* [#503](https://github.com/intridea/grape/pull/503): Calling declared(params) from child namespace fails to include parent namespace defined params - [@myitcv](https://github.com/myitcv).
2122
* [#512](https://github.com/intridea/grape/pull/512): Don't create `Grape::Request` multiple times - [@dblock](https://github.com/dblock).
2223
* Your contribution here.
2324

lib/grape/endpoint.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def call!(env)
162162
#
163163
# @param params [Hash] The initial hash to filter. Usually this will just be `params`
164164
# @param options [Hash] Can pass `:include_missing` and `:stringify` options.
165-
def declared(params, options = {}, declared_params = settings[:declared_params])
165+
def declared(params, options = {}, declared_params = settings.gather(:declared_params))
166166
options[:include_missing] = true unless options.key?(:include_missing)
167167

168168
unless declared_params

spec/grape/endpoint_spec.rb

+31
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,37 @@ def app
254254
end
255255
end
256256

257+
describe '#declared; call from child namespace' do
258+
before do
259+
subject.format :json
260+
subject.namespace :something do
261+
params do
262+
requires :id, type: Integer
263+
end
264+
resource ':id' do
265+
params do
266+
requires :foo
267+
optional :bar
268+
end
269+
get do
270+
{
271+
params: params,
272+
declared_params: declared(params)
273+
}
274+
end
275+
end
276+
end
277+
end
278+
279+
it 'should include params defined in the parent namespace' do
280+
get '/something/123', foo: 'test', extra: 'hello'
281+
expect(last_response.status).to eq 200
282+
json = JSON.parse(last_response.body, symbolize_names: true)
283+
expect(json[:params][:id]).to eq 123
284+
expect(json[:declared_params].keys).to include :foo, :bar, :id
285+
end
286+
end
287+
257288
describe '#params' do
258289
it 'is available to the caller' do
259290
subject.get('/hey') do

0 commit comments

Comments
 (0)