Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide ability to restrict declared(params) to local endpoint #530

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Next Release
* [#525](https://github.com/intridea/grape/pull/525): The default status code returned from `error!` has been changed from 403 to 500 - [@dblock](https://github.com/dblock).
* [#526](https://github.com/intridea/grape/pull/526): Allowed specifying headers in `error!` - [@dblock](https://github.com/dblock).
* [#527](https://github.com/intridea/grape/pull/527): The `before_validation` callback is now a distinct one - [@myitcv](https://github.com/myitcv).
* [#530](https://github.com/intridea/grape/pull/530): Provide ability to restrict declared(params) to local endpoint - [@myitcv](https://github.com/myitcv).
* [#531](https://github.com/intridea/grape/pull/531): Helpers are now available to auth middleware, executing in the context of the endpoint - [@joelvh](https://github.com/joelvh).
* [#540](https://github.com/intridea/grape/pull/540): Ruby 2.1.0 is now supported - [@salimane](https://github.com/salimane).
* [#544](https://github.com/intridea/grape/pull/544): The `rescue_from` keyword now handles subclasses of exceptions by default - [@xevix](https://github.com/xevix).
Expand Down
14 changes: 11 additions & 3 deletions lib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,20 @@ def call!(env)

# A filtering method that will return a hash
# consisting only of keys that have been declared by a
# `params` statement.
# `params` statement against the current/target endpoint or parent
# namespaces
#
# @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.gather(:declared_params))
# @param options [Hash] Can pass `:include_missing`, `:stringify` and `:include_parent_namespaces`
# options. `:include_parent_namespaces` defaults to true, hence must be set to false if
# you want only to return params declared against the current/target endpoint
def declared(params, options = {}, declared_params = nil)
options[:include_missing] = true unless options.key?(:include_missing)
options[:include_parent_namespaces] = true unless options.key?(:include_parent_namespaces)
if declared_params.nil?
declared_params = !options[:include_parent_namespaces] ? settings[:declared_params] :
settings.gather(:declared_params)
end

unless declared_params
raise ArgumentError, "Tried to filter for declared parameters but none exist."
Expand Down
20 changes: 19 additions & 1 deletion spec/grape/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,16 @@ def app
declared_params: declared(params)
}
end
params do
requires :happy
optional :days
end
get '/test' do
{
params: params,
declared_params: declared(params, include_parent_namespaces: false)
}
end
end
end
end
Expand All @@ -291,7 +301,15 @@ def app
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
expect(json[:declared_params].keys).to match_array [:foo, :bar, :id]
end

it 'does not include params defined in the parent namespace with include_parent_namespaces: false' do
get '/something/123/test', happy: 'test', extra: 'hello'
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 match_array [:happy, :days]
end
end

Expand Down