Skip to content
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

Support default json decoder even when nil responds to :load #1563

Merged
Show file tree
Hide file tree
Changes from 2 commits
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: 1 addition & 1 deletion lib/faraday/response/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def process_parser_options
@decoder_options =
if @decoder_options.is_a?(Array) && @decoder_options.size >= 2
@decoder_options.slice(0, 2)
elsif @decoder_options.respond_to?(:load)
elsif @decoder_options&.respond_to?(:load)
Copy link
Member

Choose a reason for hiding this comment

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

Rubocop complains here because nil responds to respond_to, so we need to disable it explicitly.
Could you also please add a comment stating why we're doing this, as I'm sure we'll forget in future and might decide to remove it, introducing a regression 😅

Suggested change
elsif @decoder_options&.respond_to?(:load)
elsif @decoder_options&.respond_to?(:load) # rubocop:disable Lint/RedundantSafeNavigation

[@decoder_options, :load]
else
[::JSON, :parse]
Expand Down
17 changes: 17 additions & 0 deletions spec/faraday/response/json_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,23 @@ def process(body, content_type = 'application/json', options = {})
response = process(body)
expect(response.body).to eq(result)
end

it 'passes relevant options to JSON parse even when nil responds to :load' do
original_allow_message_expectations_on_nil = RSpec::Mocks.configuration.allow_message_expectations_on_nil
RSpec::Mocks.configuration.allow_message_expectations_on_nil = true
allow(nil).to receive(:respond_to?)
.with(:load)
.and_return(true)

expect(JSON).to receive(:parse)
.with(body, { symbolize_names: true })
.and_return(result)

response = process(body)
expect(response.body).to eq(result)
ensure
RSpec::Mocks.configuration.allow_message_expectations_on_nil = original_allow_message_expectations_on_nil
end
end
end
end
Loading