Skip to content

Commit

Permalink
fix for declared(params) inside route_param bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
Arkanain committed Jun 29, 2016
1 parent 864568d commit 68b7c53
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#### Fixes

* [#1430](https://github.com/ruby-grape/grape/pull/1430): Fix for declared(params) inside route_param. - [@Arkanain](https://github.com/Arkanain).
* [#1405](https://github.com/ruby-grape/grape/pull/1405): Fix priority of `rescue_from` clauses applying - [@hedgesky](https://github.com/hedgesky).
* [#1365](https://github.com/ruby-grape/grape/pull/1365): Fix finding exception handler in error middleware - [@ktimothy](https://github.com/ktimothy).
* [#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).
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/validations/params_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def configure_declared_params
@api.namespace_stackable(:declared_params, @declared_params)

@api.route_setting(:declared_params, []) unless @api.route_setting(:declared_params)
@api.route_setting(:declared_params).concat @declared_params
@api.route_setting(:declared_params, @api.namespace_stackable(:declared_params).flatten)
end
end

Expand Down
73 changes: 73 additions & 0 deletions spec/grape/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,79 @@ def app
end
end

describe '#declared; with multiple route_param' do
before do
mounted = Class.new(Grape::API)
mounted.namespace :albums do
get do
declared(params)
end
end

subject.format :json
subject.namespace :artists do
route_param :id, type: Integer do
get do
declared(params)
end

params do
requires :filter, type: String
end
get :some_route do
declared(params)
end
end

route_param :artist_id, type: Integer do
namespace :compositions do
get do
declared(params)
end
end
end

route_param :compositor_id, type: Integer do
mount mounted
end
end
end

it 'return only :id without :artist_id' do
get '/artists/1'
json = JSON.parse(last_response.body, symbolize_names: true)

expect(json.key?(:id)).to be_truthy
expect(json.key?(:artist_id)).not_to be_truthy
end

it 'return only :artist_id without :id' do
get '/artists/1/compositions'
json = JSON.parse(last_response.body, symbolize_names: true)

expect(json.key?(:artist_id)).to be_truthy
expect(json.key?(:id)).not_to be_truthy
end

it 'return :filter and :id parameters in declared for second enpoint inside route_param' do
get '/artists/1/some_route', filter: 'some_filter'
json = JSON.parse(last_response.body, symbolize_names: true)

expect(json.key?(:filter)).to be_truthy
expect(json.key?(:id)).to be_truthy
expect(json.key?(:artist_id)).not_to be_truthy
end

it 'return :compositor_id for mounter in route_param' do
get '/artists/1/albums'
json = JSON.parse(last_response.body, symbolize_names: true)

expect(json.key?(:compositor_id)).to be_truthy
expect(json.key?(:id)).not_to be_truthy
expect(json.key?(:artist_id)).not_to be_truthy
end
end

describe '#params' do
it 'is available to the caller' do
subject.get('/hey') do
Expand Down

0 comments on commit 68b7c53

Please sign in to comment.