Skip to content

Commit 682af8f

Browse files
committed
Deep copy in the superclass merging to ensure no sharing of nested hashes.
Fixes #83
1 parent 939d1d2 commit 682af8f

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

lib/httparty/module_inheritable_attributes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def inherited(subclass)
2222
if instance_variable_get(ivar).respond_to?(:merge)
2323
method = <<-EOM
2424
def self.#{inheritable_attribute}
25-
#{ivar} = superclass.#{inheritable_attribute}.merge #{ivar}
25+
#{ivar} = superclass.#{inheritable_attribute}.merge Marshal.load(Marshal.dump(#{ivar}))
2626
end
2727
EOM
2828
subclass.class_eval method

spec/httparty_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,14 @@ def self.name
526526
@parent.default_options.should == {:basic_auth => {:username => 'user', :password => 'password'}}
527527
end
528528

529+
it "doesn't modify hashes in the parent's default options" do
530+
@parent.headers 'Accept' => 'application/json'
531+
@child1.headers 'Accept' => 'application/xml'
532+
533+
@parent.default_options[:headers].should == {'Accept' => 'application/json'}
534+
@child1.default_options[:headers].should == {'Accept' => 'application/xml'}
535+
end
536+
529537
it "inherits default_cookies from the parent class" do
530538
@parent.cookies 'type' => 'chocolate_chip'
531539
@child1.default_cookies.should == {"type" => "chocolate_chip"}

0 commit comments

Comments
 (0)