activeresource icon indicating copy to clipboard operation
activeresource copied to clipboard

Active Resource 6 breaking change: can't share common headers anymore

Open fabianof opened this issue 3 years ago • 9 comments

After PR #359 (I guess) has been merged, we can't define common headers on base classes and make them available to subclasses, as expected.

This way, each subclass must configure the same access token, for example, instead of using parent access token, which would not be very DRY.

Please take a look on following sample code (using fruits and apples just like ARes test cases).

# 5.1

fruit = Class.new(ActiveResource::Base)
apple = Class.new(fruit)

fruit.headers["key"] = "fruit-value"

fruit.headers # => {"key"=>"fruit-value"}
apple.headers # => {"key"=>"fruit-value"}

# 6.0

fruit = Class.new(ActiveResource::Base)
apple = Class.new(fruit)

fruit.headers["key"] = "fruit-value"

fruit.headers # => {"key"=>"fruit-value"}
apple.headers # => {}

Please note that if key was access token, for example, then subclasses couldn't get authorized, since apple.headers is empty.

fabianof avatar Jan 11 '22 21:01 fabianof

I think you are seeing an empty header but if you try to access apple.headers["key"] you will find the value. Did you try that?

rafaelfranca avatar Jan 11 '22 22:01 rafaelfranca

Hi @rafaelfranca. Thanks for your fast response.

Yes, I find the value when manually accessing the key. However, the issue happens when ARes tries to fetch resources (e.g. Apple.all.

See for example line 1108 in lib/active_resource/base.rb:

format.decode(connection.get(path, headers).body)

Here it tries to connect using an empty headers.

fabianof avatar Jan 11 '22 22:01 fabianof

Right, that is a problem indeed. This gem has very low priority for me, so if you want, you could try to fix this case so the values in headers are kept the same.

rafaelfranca avatar Jan 11 '22 23:01 rafaelfranca

Sure thing. Thanks for confirming that that is a problem.

fabianof avatar Jan 12 '22 22:01 fabianof

Just ran into this myself! See #383 - hopefully it's the right fix. I think subclassing Hash in the first place was probably not a good idea, but this solves it without reimplementing it.

meanphil avatar Mar 01 '22 07:03 meanphil

This issue has been automatically marked as stale because it has not been commented on for at least three months. The resources of the Rails team are limited, and so we are asking for your help.

If it is an issue and you can still reproduce this error on the main branch, please reply with all of the information you have about it in order to keep the issue open.

If it is a pull request and you are still interested on having it merged please make sure it can be merged clearly.

Thank you for all your contributions.

rails-bot[bot] avatar May 30 '22 08:05 rails-bot[bot]

Still an issue.

cgriego avatar Jun 01 '22 15:06 cgriego

This issue has been automatically marked as stale because it has not been commented on for at least three months. The resources of the Rails team are limited, and so we are asking for your help.

If it is an issue and you can still reproduce this error on the main branch, please reply with all of the information you have about it in order to keep the issue open.

If it is a pull request and you are still interested on having it merged please make sure it can be merged clearly.

Thank you for all your contributions.

rails-bot[bot] avatar Aug 30 '22 15:08 rails-bot[bot]

Still an issue.

cgriego avatar Aug 30 '22 20:08 cgriego

This issue has been automatically marked as stale because it has not been commented on for at least three months. The resources of the Rails team are limited, and so we are asking for your help.

If it is an issue and you can still reproduce this error on the main branch, please reply with all of the information you have about it in order to keep the issue open.

If it is a pull request and you are still interested on having it merged please make sure it can be merged clearly.

Thank you for all your contributions.

rails-bot[bot] avatar Nov 28 '22 20:11 rails-bot[bot]

This is still an issue for me, is there a way to tackle this?

bellef avatar Jan 23 '23 09:01 bellef

@bellef I moved the modification of the headers into the sub classes and called self.class.headers =

klyonrad avatar Jan 23 '23 09:01 klyonrad