Skip to content

Commit 00fd12e

Browse files
author
Chris Peters
committed
Translate dasherized include keys to underscored in JSON API adapter
1 parent 070d58e commit 00fd12e

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
lines changed

lib/active_model/serializer/include_tree.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module Parsing
1818
# @return [Hash] a Hash representing the same tree structure
1919
def include_string_to_hash(included)
2020
# TODO: Needs comment walking through the process of what all this is doing.
21-
included.delete(' ').split(',').reduce({}) do |hash, path|
21+
included.gsub('-', '_').delete(' ').split(',').reduce({}) do |hash, path|
2222
include_tree = path.split('.').reverse_each.reduce({}) { |a, e| { e.to_sym => a } }
2323
hash.deep_merge!(include_tree)
2424
end
@@ -49,6 +49,7 @@ def include_args_to_hash(included)
4949
{ included => {} }
5050
when Hash
5151
included.each_with_object({}) do |(key, value), hash|
52+
key = key.is_a?(String) ? key.gsub('-', '_').to_sym : key
5253
hash[key] = include_args_to_hash(value)
5354
end
5455
when Array

test/include_tree/from_include_args_test.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,20 @@ def test_simple_array
1111
assert(actual.key?(:comments))
1212
end
1313

14+
def test_simple_array_with_dasherized_keys
15+
input = [:comments, 'related-posts']
16+
actual = ActiveModel::Serializer::IncludeTree.from_include_args(input)
17+
assert(actual.key?(:comments))
18+
assert(actual.key?(:related_posts))
19+
end
20+
1421
def test_nested_array
15-
input = [:comments, posts: [:author, comments: [:author]]]
22+
input = [:comments, posts: [:author, 'related-posts', comments: [:author]]]
1623
actual = ActiveModel::Serializer::IncludeTree.from_include_args(input)
1724
assert(actual.key?(:posts))
1825
assert(actual[:posts].key?(:author))
1926
assert(actual[:posts].key?(:comments))
27+
assert(actual[:posts].key?(:related_posts))
2028
assert(actual[:posts][:comments].key?(:author))
2129
assert(actual.key?(:comments))
2230
end

test/include_tree/from_string_test.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,26 @@ def test_single_string
1010
assert(actual.key?(:author))
1111
end
1212

13+
def test_single_dasherized_string
14+
input = 'related-posts'
15+
actual = ActiveModel::Serializer::IncludeTree.from_string(input)
16+
assert(actual.key?(:related_posts))
17+
end
18+
1319
def test_multiple_strings
1420
input = 'author,comments'
1521
actual = ActiveModel::Serializer::IncludeTree.from_string(input)
1622
assert(actual.key?(:author))
1723
assert(actual.key?(:comments))
1824
end
1925

26+
def test_multiple_dasherized_strings
27+
input = 'related-posts,content-templates'
28+
actual = ActiveModel::Serializer::IncludeTree.from_string(input)
29+
assert(actual.key?(:related_posts))
30+
assert(actual.key?(:content_templates))
31+
end
32+
2033
def test_multiple_strings_with_space
2134
input = 'author, comments'
2235
actual = ActiveModel::Serializer::IncludeTree.from_string(input)
@@ -31,6 +44,13 @@ def test_nested_string
3144
assert(actual[:posts].key?(:author))
3245
end
3346

47+
def test_nested_hyphenated_string
48+
input = 'posts.related-posts'
49+
actual = ActiveModel::Serializer::IncludeTree.from_string(input)
50+
assert(actual.key?(:posts))
51+
assert(actual[:posts].key?(:related_posts))
52+
end
53+
3454
def test_multiple_nested_string
3555
input = 'posts.author,posts.comments.author,comments'
3656
actual = ActiveModel::Serializer::IncludeTree.from_string(input)

test/include_tree/include_args_to_hash_test.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,13 @@ def test_include_args_to_hash_from_array_of_hashes
4747

4848
def test_array_of_string
4949
expected = {
50-
comments: { author: {}, attachment: {} }
50+
comments: { author: {}, attachment: {} },
51+
posts: { related_posts: {} }
5152
}
5253
input = [
5354
'comments.author',
54-
'comments.attachment'
55+
'comments.attachment',
56+
'posts.related-posts'
5557
]
5658
actual = Parsing.include_args_to_hash(input)
5759

0 commit comments

Comments
 (0)