From 682af8fbf672e7b3009e650da776c85cdfe78d39 Mon Sep 17 00:00:00 2001 From: John Nunemaker Date: Sat, 21 Apr 2012 18:45:03 -0400 Subject: [PATCH] Deep copy in the superclass merging to ensure no sharing of nested hashes. Fixes #83 --- lib/httparty/module_inheritable_attributes.rb | 2 +- spec/httparty_spec.rb | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/httparty/module_inheritable_attributes.rb b/lib/httparty/module_inheritable_attributes.rb index e69286f7..9f4584ad 100644 --- a/lib/httparty/module_inheritable_attributes.rb +++ b/lib/httparty/module_inheritable_attributes.rb @@ -22,7 +22,7 @@ def inherited(subclass) if instance_variable_get(ivar).respond_to?(:merge) method = <<-EOM def self.#{inheritable_attribute} - #{ivar} = superclass.#{inheritable_attribute}.merge #{ivar} + #{ivar} = superclass.#{inheritable_attribute}.merge Marshal.load(Marshal.dump(#{ivar})) end EOM subclass.class_eval method diff --git a/spec/httparty_spec.rb b/spec/httparty_spec.rb index ec90ace3..41d2bd7b 100644 --- a/spec/httparty_spec.rb +++ b/spec/httparty_spec.rb @@ -526,6 +526,14 @@ def self.name @parent.default_options.should == {:basic_auth => {:username => 'user', :password => 'password'}} end + it "doesn't modify hashes in the parent's default options" do + @parent.headers 'Accept' => 'application/json' + @child1.headers 'Accept' => 'application/xml' + + @parent.default_options[:headers].should == {'Accept' => 'application/json'} + @child1.default_options[:headers].should == {'Accept' => 'application/xml'} + end + it "inherits default_cookies from the parent class" do @parent.cookies 'type' => 'chocolate_chip' @child1.default_cookies.should == {"type" => "chocolate_chip"}