Subclasses stomp on Base class's headers hash #83

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


None yet
3 participants

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'

class Subclass < Base
  headers 'Accept'       => 'application/xml'
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 ( 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 ( 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

This comment has been minimized.

Show comment
Hide comment

maletor Aug 17, 2011

I approve.

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