Skip to content

Subclasses stomp on Base class's headers hash #83

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

3 participants

@ms-ati
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
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
Something went wrong with that request. Please try again.