Skip to content

Commit d714094

Browse files
committed
Merge pull request #1007 from jiajiawang/array_serializer_json_key_fix
Bug fix for ArraySerializer json_key
2 parents 954e4c5 + 91ffec4 commit d714094

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

lib/active_model/serializer/array_serializer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def json_key
2929
if @objects.first
3030
@objects.first.json_key.pluralize
3131
else
32-
@resource.name.downcase.pluralize if @resource.try(:name)
32+
@resource.name.underscore.pluralize if @resource.try(:name)
3333
end
3434
end
3535

test/array_serializer_test.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ def test_meta_and_meta_key_attr_readers
3838
assert_equal @serializer.meta, "the meta"
3939
assert_equal @serializer.meta_key, "the meta key"
4040
end
41+
42+
def test_json_key_when_resource_is_empty
43+
Array.class_eval do
44+
def name
45+
'PostComment'
46+
end
47+
end
48+
@post_comments = []
49+
@serializer = ArraySerializer.new(@post_comments)
50+
assert_equal @serializer.json_key, "post_comments"
51+
end
4152
end
4253
end
4354
end

0 commit comments

Comments
 (0)