Skip to content

Commit

Permalink
Fix for key_format! when using nested hashes (#486)
Browse files Browse the repository at this point in the history
Converts the keys of all hashes within arrays and ones being merged in
to follow the specified key format.

Fixes #473
  • Loading branch information
blackjack26 authored Jan 22, 2021
1 parent 53b386b commit 5c82764
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 5 deletions.
20 changes: 15 additions & 5 deletions lib/jbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ def set!(key, value = BLANK, *args, &block)
# json.age 32
# json.person another_jbuilder
# { "age": 32, "person": { ... }
value.attributes!
_format_keys(value.attributes!)
else
# json.age 32
# { "age": 32 }
value
_format_keys(value)
end
elsif _is_collection?(value)
# json.comments @post.comments, :content, :created_at
Expand Down Expand Up @@ -272,11 +272,11 @@ def _merge_values(current_value, updates)
if _blank?(updates)
current_value
elsif _blank?(current_value) || updates.nil? || current_value.empty? && ::Array === updates
updates
_format_keys(updates)
elsif ::Array === current_value && ::Array === updates
current_value + updates
current_value + _format_keys(updates)
elsif ::Hash === current_value && ::Hash === updates
current_value.deep_merge(updates)
current_value.deep_merge(_format_keys(updates))
else
raise MergeError.build(current_value, updates)
end
Expand All @@ -286,6 +286,16 @@ def _key(key)
@key_formatter ? @key_formatter.format(key) : key.to_s
end

def _format_keys(hash_or_array)
if ::Array === hash_or_array
hash_or_array.map { |value| _format_keys(value) }
elsif ::Hash === hash_or_array
::Hash[hash_or_array.collect { |k, v| [_key(k), _format_keys(v)] }]
else
hash_or_array
end
end

def _set_value(key, value)
raise NullError.build(key) if @attributes.nil?
raise ArrayError.build(key) if ::Array === @attributes
Expand Down
52 changes: 52 additions & 0 deletions test/jbuilder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,58 @@ class JbuilderTest < ActiveSupport::TestCase
assert_equal ['oats and friends'], result.keys
end

test 'key_format! with merge!' do
hash = { camel_style: 'for JS' }
result = jbuild do |json|
json.key_format! camelize: :lower
json.merge! hash
end

assert_equal ['camelStyle'], result.keys
end

test 'key_format! with merge! deep' do
hash = { camel_style: { sub_attr: 'for JS' } }
result = jbuild do |json|
json.key_format! camelize: :lower
json.merge! hash
end

assert_equal ['subAttr'], result['camelStyle'].keys
end

test 'key_format! with set! array of hashes' do
names = [{ first_name: 'camel', last_name: 'case' }]
result = jbuild do |json|
json.key_format! camelize: :lower
json.set! :names, names
end

assert_equal %w[firstName lastName], result['names'][0].keys
end

test 'key_format! with array! of hashes' do
names = [{ first_name: 'camel', last_name: 'case' }]
result = jbuild do |json|
json.key_format! camelize: :lower
json.array! names
end

assert_equal %w[firstName lastName], result[0].keys
end

test 'key_format! with merge! array of hashes' do
names = [{ first_name: 'camel', last_name: 'case' }]
new_names = [{ first_name: 'snake', last_name: 'case' }]
result = jbuild do |json|
json.key_format! camelize: :lower
json.array! names
json.merge! new_names
end

assert_equal %w[firstName lastName], result[1].keys
end

test 'default key_format!' do
Jbuilder.key_format camelize: :lower
result = jbuild{ |json| json.camel_style 'for JS' }
Expand Down

0 comments on commit 5c82764

Please sign in to comment.