Skip to content

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

Merged
iMacTia merged 2 commits intolostisland:mainfrom
xkwd:merge-connection-options-with-default-connection-options-via-deep-merge
Nov 30, 2021
Merged

Replace Hash#merge with Utils#deep_merge for connection options#1343
iMacTia merged 2 commits intolostisland:mainfrom
xkwd:merge-connection-options-with-default-connection-options-via-deep-merge

Conversation

@xkwd
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

2 participants