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

Replace Hash#merge with Utils#deep_merge for connection options #1343

Conversation

xkwd
Copy link
Contributor

@xkwd xkwd commented Nov 29, 2021

Description

This is an attempt to close the #1248 issue by following the proposed solution #1 in this comment.

Prior to this change:

Faraday.default_connection_options = { headers: { user_agent: 'My Agent 1.2' } }
Faraday.new('http://example.co', headers: { 'CustomHeader' => 'CustomValue' }).headers 
# => { 'CustomHeader' => 'CustomValue', 'User-Agent' => 'Faraday v2.0.0.alpha-2' }
Faraday.new(url: 'http://example.co', headers: { 'CustomHeader' => 'CustomValue' }).headers 
# => { 'CustomHeader' => 'CustomValue', 'User-Agent' => 'Faraday v2.0.0.alpha-2' }

After this change:

Faraday.default_connection_options = { headers: { user_agent: 'My Agent 1.2' } }
Faraday.new('http://example.co', headers: { 'CustomHeader' => 'CustomValue' }).headers 
# => { 'CustomHeader' => 'CustomValue', 'User-Agent' => 'My Agent 1.2' }
Faraday.new(url: 'http://example.co', headers: { 'CustomHeader' => 'CustomValue' }).headers 
# => { 'CustomHeader' => 'CustomValue', 'User-Agent' => 'My Agent 1.2' }

The only issue with the proposed solution was that the deep_merge from Faraday::Utils didn't work out of the box for an Options Struct like RequestOptions and SSLOptions from Faraday::ConnectionOptions.

Copy link
Member

@iMacTia iMacTia left a comment

Choose a reason for hiding this comment

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

Thank you @xkwd, this change is very much welcome!
The biggest issue I had with this solution, as I mentioned in my comment on the issue, was that this is effectively backwards-incompatible and might cause surprise for users.

Good news though is that we're in alpha testing for Faraday 2.0 🎉! So I'm happy to merge this is in and simply drop a comment on the UPGRADING.md guide 🙌

The only downside is that if you need this in one of your projects, you'll need to upgrade to Faraday 2.0

context 'when url is a Hash' do
let(:conn) { Faraday.new(url: 'http://example.co', headers: { 'CustomHeader' => 'CustomValue' }) }

before { Faraday.default_connection_options = { headers: { user_agent: 'My Agent 1.2' } } }
Copy link
Member

@iMacTia iMacTia Nov 30, 2021

Choose a reason for hiding this comment

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

I just realised there's no rollback of the original value after these test have run 😄.
I'll be fixing this on the main branch, so no worries at all, but please keep this in mind next time you write tests for a global configuration 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants