File tree Expand file tree Collapse file tree 3 files changed +39
-5
lines changed Expand file tree Collapse file tree 3 files changed +39
-5
lines changed Original file line number Diff line number Diff line change @@ -270,14 +270,14 @@ def _merge_block(key)
270
270
def _merge_values ( current_value , updates )
271
271
if _blank? ( updates )
272
272
current_value
273
- elsif _blank? ( current_value ) || updates . nil?
273
+ elsif _blank? ( current_value ) || updates . nil? || current_value . empty? && :: Array === updates
274
274
updates
275
- elsif ::Array === updates
276
- :: Array === current_value ? current_value + updates : updates
277
- elsif ::Hash === current_value
275
+ elsif ::Array === current_value && :: Array === updates
276
+ current_value + updates
277
+ elsif ::Hash === current_value && :: Hash === updates
278
278
current_value . merge ( updates )
279
279
else
280
- raise "Can't merge #{ updates . inspect } with #{ current_value . inspect } "
280
+ raise MergeError . build ( current_value , updates )
281
281
end
282
282
end
283
283
Original file line number Diff line number Diff line change @@ -14,4 +14,11 @@ def self.build(key)
14
14
new ( message )
15
15
end
16
16
end
17
+
18
+ class MergeError < ::StandardError
19
+ def self . build ( current_value , updates )
20
+ message = "Can't merge #{ updates . inspect } into #{ current_value . inspect } "
21
+ new ( message )
22
+ end
23
+ end
17
24
end
Original file line number Diff line number Diff line change @@ -686,4 +686,31 @@ class JbuilderTest < ActiveSupport::TestCase
686
686
end
687
687
end
688
688
end
689
+
690
+ test "throws MergeError when trying to merge array with non-empty hash" do
691
+ assert_raise Jbuilder ::MergeError do
692
+ jbuild do |json |
693
+ json . name "Daniel"
694
+ json . merge! [ ]
695
+ end
696
+ end
697
+ end
698
+
699
+ test "throws MergeError when trying to merge hash with array" do
700
+ assert_raise Jbuilder ::MergeError do
701
+ jbuild do |json |
702
+ json . array!
703
+ json . merge! ( { } )
704
+ end
705
+ end
706
+ end
707
+
708
+ test "throws MergeError when trying to merge invalid objects" do
709
+ assert_raise Jbuilder ::MergeError do
710
+ jbuild do |json |
711
+ json . name "Daniel"
712
+ json . merge! "Nope"
713
+ end
714
+ end
715
+ end
689
716
end
You can’t perform that action at this time.
0 commit comments