-
Notifications
You must be signed in to change notification settings - Fork 988
Fix instances of frozen empty string literals #1040
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 instances of frozen empty string literals #1040
Conversation
@iMacTia we'll likely still want to merge this |
Sorry @BobbyMcWho I haven't had the time to look into this, but it seems like we're making the response body mutable. I'm not sure on what's the rationale behind it? |
This was one of the many issues that presented itself when that 0.16.0 rollout was released. There are other projects that want to do things like force the encoding which were broken by introducing these non mutable bodies |
Ah I see your point, thanks for clarifying. From an application point of view, I think having the body immutable is actually the correct behaviour in this case (faster performance, more security). But I'm happy to take in different point of views |
Is the body always immutable when returned from Faraday or is it only immutable in the case when the body is this initial empty string? |
I'd personally expect it to always be immutable, at least it would be consistent and as I said 99% of times you probably don't need to do any change to it (and you can always override it on that remaining 1%) and would keep performance faster |
is the body always immutable when we parse the response today? |
You mean in 0.17.x? No it's not, but that's the old standard behaviour. My point is that I don't think it's worth keeping backwards compatibility in this case as you have performance on the other side. If we don't change this behaviour in a major release, we won't be able to change it anymore |
@iMacTia here's a script that demonstrates what I'm alluding to: require 'bundler/inline'
gemfile do
source 'https://www.rubygems.org'
gem 'faraday', '~> 1.0-rc1'
gem 'minitest'
end
require 'minitest/autorun'
require 'faraday'
class FaradayTest < Minitest::Test
def test_faraday_version
assert Gem::Version.new(Faraday::VERSION) == Gem::Version.new('1.0-rc1') # pass
end
def test_faraday_frozen_body
assert Faraday.get('http://www.example.com').body.frozen? # fail
end
end The default behavior is that the response body is not frozen, it is only frozen in these cases where we default the response body to an empty string. Edit: I was testing the whole response and not just the body 🤦♂️ Double edit: Testing if the body is frozen, my original claim stands that the body is not frozen. |
This depends on the adapter and underlying gem. For example, the Excon adapter simply passes the response body to faraday/lib/faraday/adapter/excon.rb Lines 29 to 32 in cccc145
faraday/lib/faraday/adapter.rb Lines 56 to 59 in cccc145
|
In that case I definitely think the default empty string body should be mutable like my PR suggests |
Agree we should be consistent on it, and currently we're relying on underlying adapters by just returning what they return us. Eventually one day everyone will move towards frozen strings as that's supposed to be the standard as older Ruby versions are abandoned, but we're not there just yet.
|
@iMacTia If we are to do anything to the body returned, I think it is to freeze it (or do nothing). |
I'm down with frozen strings, but I think this is a really important point.
By freezing empty strings Faraday, we place the burden on our users to I'm going with option 2, that we merge this PR and keep those response bodies mutable. Response bodies are used very differently than hash keys or constants, so I don't think the memory/performance profile will change much. While I think there's some merit in freezing the strings to maintain the integrity of the response body, I don't think it's worth breaking |
That's a good reason, I retract my note. |
Thanks @BobbyMcWho for the contribution and sorry for the loooong discussion (plus the extra issues with Github Actions) |
No need to apologize, I can definitely appreciate being thorough and weighing the options, especially with a library as widely used as Faraday. |
Same fix as #1039, this should go into master as well for 1.0