Skip to content

Commit

Permalink
Merge pull request #1517 from segiddins/segiddins/nil-param
Browse files Browse the repository at this point in the history
Avoid `TypeError` when params contain a key without a value on Ruby < 2.4
  • Loading branch information
namusyaka committed Feb 7, 2019
2 parents 4ceed83 + d8c1839 commit 2e36823
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,7 @@
## Unreleased

* Avoid `TypeError` when params contain a key without a value on Ruby < 2.4 [#1516](https://github.com/sinatra/sinatra/pull/1516) by Samuel Giddins

## 2.0.5 / 2018-12-22

* Avoid FrozenError when params contains frozen value [#1506](https://github.com/sinatra/sinatra/pull/1506) by Kunpei Sakai
Expand Down
2 changes: 1 addition & 1 deletion lib/sinatra/base.rb
Expand Up @@ -1089,7 +1089,7 @@ def invoke

# Dispatch a request with error handling.
def dispatch!
@params.merge!(@request.params).each { |key, val| @params[key] = force_encoding(val.dup) }
@params.merge!(@request.params).each { |key, val| @params[key] = val && force_encoding(val.dup) }

invoke do
static! if settings.static? && (request.get? || request.head?)
Expand Down
11 changes: 11 additions & 0 deletions test/routing_test.rb
Expand Up @@ -311,6 +311,17 @@ class RoutingTest < Minitest::Test
assert_equal 'well, alright', body
end

it "handles params without a value" do
mock_app {
get '/' do
assert_nil params.fetch('foo')
"Given: #{params.keys.sort.join(',')}"
end
}
get '/?foo'
assert_equal 'Given: foo', body
end

it "merges named params and query string params in params" do
mock_app {
get '/:foo' do
Expand Down

0 comments on commit 2e36823

Please sign in to comment.