Contrib: fix required params implementation

When required_params is called in such a way
that it expects a key in the params to be a hash,
it fails with error

undefined method has_key?

when it is not indeed a hash in the params in a
request.

Now, it's been ensured that the key can respond to
has_key? before has_key? is called on it.

So,

required_params :p1, :p2 => [:p3]

now works properly even if :p2 turns out
not to point to a hash, e. g. if the params hash is

{
  p1: 2,
  p2: 3
}

it will properly halt with status 400.
This commit is contained in:
Arpit 2018-05-29 00:17:44 +05:30
parent df87572b0d
commit 55858e6c4d
No known key found for this signature in database
GPG Key ID: 71E79889EB580242
1 changed files with 1 additions and 1 deletions

View File

@ -60,7 +60,7 @@ module Sinatra
elsif key.is_a?(Array)
_required_params(p, *key)
else
halt 400 unless p.has_key?(key.to_s)
halt 400 unless p && p.respond_to?(:has_key?) && p.has_key?(key.to_s)
end
end
true