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

Subclasses stomp on Base class's headers hash #83

Closed
ms-ati opened this issue May 17, 2011 · 1 comment
Closed

Subclasses stomp on Base class's headers hash #83

ms-ati opened this issue May 17, 2011 · 1 comment

Comments

@ms-ati
Copy link

ms-ati commented May 17, 2011

Say you have a base class Base which includes HTTParty, sets some default headers, and then a subclass which inherits from Base. If one of the subclasses overrides headers, they are overridden for all three classes.

AFAICT, this defeats the whole purpose of the magic in HTTParty::ModuleInheritableAttributes, and so I must assume it's a bug.

class Base
  include HTTParty
  headers 'Accept'       => 'application/json'
end

class Subclass < Base
  headers 'Accept'       => 'application/xml'
end
ruby-1.8.7-p330 :004 > pp Base.headers
{"Content-Type"=>"application/xml", "Accept"=>"application/xml"}
# SHOULD HAVE stayed as json!!!

Proposed Fixes:

  1. change the clone call at line 21 of module_inheritable_attributes.rb.rb (https://github.com/jnunemaker/httparty/blob/master/lib/httparty/module_inheritable_attributes.rb) to instead be a deep_clone, through one of the various strategies to achieve that result that are available.
  2. change the merge! call at line 175 of httparty.rb (https://github.com/jnunemaker/httparty/blob/master/lib/httparty.rb) to NOT merge into the existing hash, but into a clone, like this:
default_options[:headers] = default_options[:headers].merge(h) # STOPS altering the original hash
@maletor
Copy link

maletor commented Aug 17, 2011

I approve.

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

No branches or pull requests

3 participants