File tree Expand file tree Collapse file tree 4 files changed +47
-6
lines changed Expand file tree Collapse file tree 4 files changed +47
-6
lines changed Original file line number Diff line number Diff line change @@ -39,6 +39,7 @@ module ActiveResource
3939 autoload :HttpMock
4040 autoload :Schema
4141 autoload :Singleton
42+ autoload :InheritingHash
4243 autoload :Validations
4344 autoload :Collection
4445end
Original file line number Diff line number Diff line change @@ -697,12 +697,11 @@ def connection(refresh = false)
697697 end
698698
699699 def headers
700- headers_state = self . _headers || { }
701- if superclass != Object
702- self . _headers = superclass . headers . merge ( headers_state )
703- else
704- headers_state
705- end
700+ self . _headers ||= if superclass != Object
701+ InheritingHash . new ( superclass . headers )
702+ else
703+ { }
704+ end
706705 end
707706
708707 attr_writer :element_name
Original file line number Diff line number Diff line change 1+ # frozen_string_literal: true
2+
3+ module ActiveResource
4+ class InheritingHash < Hash
5+ def initialize ( parent_hash = { } )
6+ # Default hash value must be nil, which allows fallback lookup on parent hash
7+ super ( nil )
8+ @parent_hash = parent_hash
9+ end
10+
11+ def []( key )
12+ super || @parent_hash [ key ]
13+ end
14+ end
15+ end
Original file line number Diff line number Diff line change @@ -706,6 +706,32 @@ def test_header_inheritance_should_not_leak_upstream
706706 assert_nil fruit . headers [ "key2" ]
707707 end
708708
709+ def test_header_inheritance_can_override_upstream
710+ fruit = Class . new ( ActiveResource ::Base )
711+ apple = Class . new ( fruit )
712+ fruit . site = "http://market"
713+
714+ fruit . headers [ "key" ] = "fruit-value"
715+ assert_equal "fruit-value" , apple . headers [ "key" ]
716+
717+ apple . headers [ "key" ] = "apple-value"
718+ assert_equal "apple-value" , apple . headers [ "key" ]
719+ assert_equal "fruit-value" , fruit . headers [ "key" ]
720+ end
721+
722+
723+ def test_header_inheritance_should_not_override_upstream_on_read
724+ fruit = Class . new ( ActiveResource ::Base )
725+ apple = Class . new ( fruit )
726+ fruit . site = "http://market"
727+
728+ fruit . headers [ "key" ] = "value"
729+ assert_equal "value" , apple . headers [ "key" ]
730+
731+ fruit . headers [ "key" ] = "new-value"
732+ assert_equal "new-value" , apple . headers [ "key" ]
733+ end
734+
709735 def test_header_should_be_copied_to_main_thread_if_not_defined
710736 fruit = Class . new ( ActiveResource ::Base )
711737
You can’t perform that action at this time.
0 commit comments