Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/faraday/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ def success?
end

def to_hash
return {} unless finished?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @yykamei, good catch. A more explicit alternative would be to reuse the status, body and headers methods already defined in this class and adding def url. It also gives us an explicit reference that env = nil by retaining the hash keys and the nil values and not only returning an empty hash. What do you think?

    def url
      finished? ? env.url : nil
    end
    def to_hash
      {
        status: status, body: body,
        response_headers: headers,
        url: url
      }
    end

Copy link
Contributor Author

@yykamei yykamei Aug 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, then I fix it and make the method return the data like this:

{
  status: nil,
  body: nil,
  response_headers: {},
  url: nil
}

Thank you for reviewing!

Copy link
Contributor Author

@yykamei yykamei Aug 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the pull request with this commit: a857c26

{
status: env.status, body: env.body,
response_headers: env.response_headers,
Expand Down
6 changes: 6 additions & 0 deletions spec/faraday/response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
it { expect(hash[:response_headers]).to eq(subject.headers) }
it { expect(hash[:body]).to eq(subject.body) }
it { expect(hash[:url]).to eq(subject.env.url) }

context 'when response is not finished' do
subject { Faraday::Response.new.to_hash }

it { is_expected.to eq({}) }
end
end

describe 'marshal serialization support' do
Expand Down