Always dup body before mutating it#56
Merged
Merged
Conversation
`body = body.dup if body.frozen?` isn't a good pattern because while it works to not fail if the string is frozen, it doesn't prevent triggering deprecation warnings if the `body` happened to be a mutable literal string (AKA chilled string). Either way, it's preferable to always dup the string, Ruby will take care to share the buffer instead of copying if the string is large enough.
iMacTia
approved these changes
May 20, 2026
iMacTia
left a comment
Member
There was a problem hiding this comment.
Ruby will take care to share the buffer instead of copying if the string is large enough
That is great to hear and it makes total sense.
I'll trust your expertise on this one, thank you for the contribution 🙇!
Contributor
|
we started getting frozen string errors for responses with no response body and no content-type. was the change at line 190 necessary? edit: we can mitigate the issue on our side, just wanted to flag it. |
Contributor
Author
|
Right, line 190 could be reverted, sorry about that. |
3 tasks
kevinebaugh
added a commit
to kevinebaugh/faraday-net_http
that referenced
this pull request
May 28, 2026
PR lostisland#56 changed the fallback in `encoded_body` from `+''` to `''`. With `frozen_string_literal: true`, the bare literal is frozen, so a response with no body and no Content-Type returns a frozen empty string that raises FrozenError when downstream middleware mutates it. Restore the unary `+` to keep the fallback mutable, as suggested by the maintainer. Co-Authored-By: jakeonfire <68826+jakeonfire@users.noreply.github.com> Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
olleolleolle
pushed a commit
that referenced
this pull request
May 29, 2026
) PR #56 changed the fallback in `encoded_body` from `+''` to `''`. With `frozen_string_literal: true`, the bare literal is frozen, so a response with no body and no Content-Type returns a frozen empty string that raises FrozenError when downstream middleware mutates it. Restore the unary `+` to keep the fallback mutable, as suggested by the maintainer. Co-authored-by: jakeonfire <68826+jakeonfire@users.noreply.github.com> Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Ref: #13
body = body.dup if body.frozen?isn't a good pattern because while it works to not fail if the string is frozen, it doesn't prevent triggering deprecation warnings if thebodyhappened to be a mutable literal string (AKA chilled string).Either way, it's preferable to always dup the string, Ruby will take care to share the buffer instead of copying if the string is large enough.