Skip to content

Commit c059f98

Browse files
Arkanaindblock
authored andcommitted
fix for declared(params) inside route_param bug. (#1430)
1 parent 864568d commit c059f98

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#### Fixes
1313

14+
* [#1430](https://github.com/ruby-grape/grape/pull/1430): Fix for `declared(params)` inside `route_param` - [@Arkanain](https://github.com/Arkanain).
1415
* [#1405](https://github.com/ruby-grape/grape/pull/1405): Fix priority of `rescue_from` clauses applying - [@hedgesky](https://github.com/hedgesky).
1516
* [#1365](https://github.com/ruby-grape/grape/pull/1365): Fix finding exception handler in error middleware - [@ktimothy](https://github.com/ktimothy).
1617
* [#1380](https://github.com/ruby-grape/grape/pull/1380): Fix `allow_blank: false` for `Time` attributes with valid values causes `NoMethodError` - [@ipkes](https://github.com/ipkes).

lib/grape/validations/params_scope.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ def configure_declared_params
190190
@api.namespace_stackable(:declared_params, @declared_params)
191191

192192
@api.route_setting(:declared_params, []) unless @api.route_setting(:declared_params)
193-
@api.route_setting(:declared_params).concat @declared_params
193+
@api.route_setting(:declared_params, @api.namespace_stackable(:declared_params).flatten)
194194
end
195195
end
196196

spec/grape/endpoint_spec.rb

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,79 @@ def app
582582
end
583583
end
584584

585+
describe '#declared; with multiple route_param' do
586+
before do
587+
mounted = Class.new(Grape::API)
588+
mounted.namespace :albums do
589+
get do
590+
declared(params)
591+
end
592+
end
593+
594+
subject.format :json
595+
subject.namespace :artists do
596+
route_param :id, type: Integer do
597+
get do
598+
declared(params)
599+
end
600+
601+
params do
602+
requires :filter, type: String
603+
end
604+
get :some_route do
605+
declared(params)
606+
end
607+
end
608+
609+
route_param :artist_id, type: Integer do
610+
namespace :compositions do
611+
get do
612+
declared(params)
613+
end
614+
end
615+
end
616+
617+
route_param :compositor_id, type: Integer do
618+
mount mounted
619+
end
620+
end
621+
end
622+
623+
it 'return only :id without :artist_id' do
624+
get '/artists/1'
625+
json = JSON.parse(last_response.body, symbolize_names: true)
626+
627+
expect(json.key?(:id)).to be_truthy
628+
expect(json.key?(:artist_id)).not_to be_truthy
629+
end
630+
631+
it 'return only :artist_id without :id' do
632+
get '/artists/1/compositions'
633+
json = JSON.parse(last_response.body, symbolize_names: true)
634+
635+
expect(json.key?(:artist_id)).to be_truthy
636+
expect(json.key?(:id)).not_to be_truthy
637+
end
638+
639+
it 'return :filter and :id parameters in declared for second enpoint inside route_param' do
640+
get '/artists/1/some_route', filter: 'some_filter'
641+
json = JSON.parse(last_response.body, symbolize_names: true)
642+
643+
expect(json.key?(:filter)).to be_truthy
644+
expect(json.key?(:id)).to be_truthy
645+
expect(json.key?(:artist_id)).not_to be_truthy
646+
end
647+
648+
it 'return :compositor_id for mounter in route_param' do
649+
get '/artists/1/albums'
650+
json = JSON.parse(last_response.body, symbolize_names: true)
651+
652+
expect(json.key?(:compositor_id)).to be_truthy
653+
expect(json.key?(:id)).not_to be_truthy
654+
expect(json.key?(:artist_id)).not_to be_truthy
655+
end
656+
end
657+
585658
describe '#params' do
586659
it 'is available to the caller' do
587660
subject.get('/hey') do

0 commit comments

Comments
 (0)