From 673a751506de1162caee84c7d13ac31fe7688071 Mon Sep 17 00:00:00 2001 From: Paul Jolly Date: Wed, 6 Nov 2013 14:45:40 +0000 Subject: [PATCH 1/6] Failing test to show problem --- spec/grape/endpoint_spec.rb | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/spec/grape/endpoint_spec.rb b/spec/grape/endpoint_spec.rb index e0b8d3aeaf..71b7e6c760 100644 --- a/spec/grape/endpoint_spec.rb +++ b/spec/grape/endpoint_spec.rb @@ -254,6 +254,35 @@ def app end end + describe '#declared; call from child namespace' do + it 'should include params defined in the parent namespace' do + subject.namespace :something do + params do + requires :id, type: Integer + end + resource ':id' do + params do + requires :foo + optional :bar + end + get do + params[:id].should == 123 + params[:foo].should == 'test' + params[:extra].should == 'hello' + + declared(params).key?(:foo).should == true + declared(params).key?(:bar).should == true + declared(params).key?(:id).should == true + "" + end + end + end + + get '/something/123', foo: 'test', extra: 'hello' + last_response.status.should == 200 + end + end + describe '#params' do it 'is available to the caller' do subject.get('/hey') do From e843414bdbb07a4085946fd75994ca80ec0991cb Mon Sep 17 00:00:00 2001 From: Paul Jolly Date: Thu, 7 Nov 2013 09:33:59 +0000 Subject: [PATCH 2/6] Move to @dblock preferred testing format --- spec/grape/endpoint_spec.rb | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/spec/grape/endpoint_spec.rb b/spec/grape/endpoint_spec.rb index 71b7e6c760..127d545d56 100644 --- a/spec/grape/endpoint_spec.rb +++ b/spec/grape/endpoint_spec.rb @@ -255,7 +255,7 @@ def app end describe '#declared; call from child namespace' do - it 'should include params defined in the parent namespace' do + before do subject.namespace :something do params do requires :id, type: Integer @@ -265,21 +265,22 @@ def app requires :foo optional :bar end - get do - params[:id].should == 123 - params[:foo].should == 'test' - params[:extra].should == 'hello' - - declared(params).key?(:foo).should == true - declared(params).key?(:bar).should == true - declared(params).key?(:id).should == true - "" + get do + { + params: params, + declared_params: declared(params) + }.to_json end end end + end + it 'should include params defined in the parent namespace' do get '/something/123', foo: 'test', extra: 'hello' - last_response.status.should == 200 + expect(last_response.status).to eq 200 + json = JSON.parse(last_response.body, symbolize_names: true) + expect(json[:params][:id]).to eq 123 + expect(json[:declared_params].keys).to include :foo, :bar, :id end end From 62816441b470ad4eb55ce08610e37105fbf210c3 Mon Sep 17 00:00:00 2001 From: Paul Jolly Date: Thu, 7 Nov 2013 12:32:51 +0000 Subject: [PATCH 3/6] Ignore binstubs bin dir --- .rubocop.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.rubocop.yml b/.rubocop.yml index 95488f91f1..d3487826fe 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,6 +1,7 @@ AllCops: Excludes: - vendor/** + - bin/** LineLength: Enabled: false From cfe5ebae0dacd895db2c8bcb19e3ea04bc29e740 Mon Sep 17 00:00:00 2001 From: Paul Jolly Date: Thu, 7 Nov 2013 12:33:05 +0000 Subject: [PATCH 4/6] Fix rubocop whitespace violation --- spec/grape/endpoint_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/grape/endpoint_spec.rb b/spec/grape/endpoint_spec.rb index 127d545d56..8998585fdc 100644 --- a/spec/grape/endpoint_spec.rb +++ b/spec/grape/endpoint_spec.rb @@ -265,7 +265,7 @@ def app requires :foo optional :bar end - get do + get do { params: params, declared_params: declared(params) From 609212bd0465fcf4464a69d4031fd2df26a82f36 Mon Sep 17 00:00:00 2001 From: Paul Jolly Date: Thu, 7 Nov 2013 14:07:47 +0000 Subject: [PATCH 5/6] Minor fix to specify format of endpoint --- spec/grape/endpoint_spec.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spec/grape/endpoint_spec.rb b/spec/grape/endpoint_spec.rb index 8998585fdc..9879e774b3 100644 --- a/spec/grape/endpoint_spec.rb +++ b/spec/grape/endpoint_spec.rb @@ -256,6 +256,7 @@ def app describe '#declared; call from child namespace' do before do + subject.format :json subject.namespace :something do params do requires :id, type: Integer @@ -269,7 +270,7 @@ def app { params: params, declared_params: declared(params) - }.to_json + } end end end From 739c131cad278c6ae4890cc098f48a68ef03a5e7 Mon Sep 17 00:00:00 2001 From: Paul Jolly Date: Thu, 5 Dec 2013 22:12:19 +0000 Subject: [PATCH 6/6] Proposed fix for #503 --- CHANGELOG.md | 1 + lib/grape/endpoint.rb | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8316d0a78f..1c8fe00166 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ Next Release * [#495](https://github.com/intridea/grape/pull/495): Fix `ParamsScope#params` for parameters nested inside arrays - [@asross](https://github.com/asross). * [#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). * [#500](https://github.com/intridea/grape/pull/500): Skip entity auto-detection when explicitely passed - [@yaneq](https://github.com/yaneq). +* [#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). * [#512](https://github.com/intridea/grape/pull/512): Don't create `Grape::Request` multiple times - [@dblock](https://github.com/dblock). * Your contribution here. diff --git a/lib/grape/endpoint.rb b/lib/grape/endpoint.rb index 51430d89f2..32d3fafc4f 100644 --- a/lib/grape/endpoint.rb +++ b/lib/grape/endpoint.rb @@ -162,7 +162,7 @@ def call!(env) # # @param params [Hash] The initial hash to filter. Usually this will just be `params` # @param options [Hash] Can pass `:include_missing` and `:stringify` options. - def declared(params, options = {}, declared_params = settings[:declared_params]) + def declared(params, options = {}, declared_params = settings.gather(:declared_params)) options[:include_missing] = true unless options.key?(:include_missing) unless declared_params