-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Fix for FrozenError when force_encode a frozen string literal #1479
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
Fix for FrozenError when force_encode a frozen string literal #1479
Conversation
|
Is it possible to add a test for this? |
|
I think I've isolated a test case. I made a fix to my PR, and will work on adding a test as soon as I can. |
|
Not all rubies have the unary + operator for strings. 🤕 So still need a way to unfreeze the string while keeping the same encoding and that's backwards compatible. |
…oding to throw a FrozenError
|
Kinda stuck here because not sure how to support ruby 2.2.9. Using the unary operator Alternatively, using According to RuboCop, the unary operator behaves slightly differently than
|
|
@programmarchy Sorry for the late reply. I have been thinking about the issue. At first, unfortunately, your patch won't work on Ruby-2.2.x as you said. What is the root essence of the issue? I guess, it's the API design. The issue shouldn't be resolved by tricky hack and it should be treated as an API design issue. |
|
@namusyaka Thanks for looking into this. Regarding API design issue, I think it would be strange to force the user to pass the param value as mutable. As a user of the API, I am not expecting a side effect to occur on that param value -- I'm expecting it to be a value that is read by sinatra, not mutated. That said, how should it be addressed? One option could be for sinatra to keep an internal copy of the param, and mutate the copy with Another option could for sinatra to not force encoding, and instead force the caller of |
|
To process the user input, sinatra has already encoded params internally as you're doing. What's your concern? In additions to the comment for design, I don't like new flag idea because not only that it increase complexity of sinatra, your suggestion breaks backward compatibility. It really looks not reasonable. My idea is just simple that changes code place of encoding and it's enough for fixing the issue. I'm going to write a details as code example by EOW. |
|
@programmarchy How about this? diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb
index eb94d3ed..4875e0ba 100644
--- a/lib/sinatra/base.rb
+++ b/lib/sinatra/base.rb
@@ -1089,7 +1089,7 @@ module Sinatra
# Dispatch a request with error handling.
def dispatch!
- force_encoding(@params.merge!(@request.params))
+ @params.merge!(@request.params).each { |key, val| @params[key] = force_encoding(val.dup) }
invoke do
static! if settings.static? && (request.get? || request.head?) |
The error can be fixed by using dupped values. The original code for testing is implemented by @programmarchy in #1479 Fixes #1478 Closes #1479
|
Hello guys, this breaks our tests when we try to pass nil parameter value. Granted it's probably dumb test and nil values are not possible during runtime, but it's a regression anyway: def test_api_with_nil_param
get "/#{host}/test", { 'a_param' => nil }
assert last_response.ok?, "Last response was not ok"
endLeading to: I am going to change our test and remove |
Issue #1478