Skip to content

Commit 251e33a

Browse files
committed
Don't pluralize the CollectionSerializer#root for #json_key
One of three constituents is used to provide the CollectionSerializer's #json_key: 1) the :root option - controlled by the caller 2) the #name of the first resource serializer - the root or underscored model name 3) the underscored #name of the resources object - generally equivalent to the underscored model name of #2 Of the three, only the latter 2 are out of the callers control, and only the latter two are expected to be singular by default. Not pluralizing the root gives the caller additional flexibility in defining the desired root, whether conventionally plural, unconventionally plural (e.g. objects_received:) or singular.
1 parent 34e5faa commit 251e33a

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ Breaking changes:
1313
Adapter functions.
1414
* named `Base` because it's a Rails-ism.
1515
* It helps to isolate and highlight what the Adapter interface actually is.
16+
- [#1418](https://github.com/rails-api/active_model_serializers/pull/1418)
17+
serialized collections now use the root option as is; now, only the
18+
root derived from the serializer or object is always pluralized.
1619

1720
Features:
1821

lib/active_model/serializer/collection_serializer.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ def initialize(resources, options = {})
2323
end
2424

2525
def json_key
26-
key = root || serializers.first.try(:json_key) || object.try(:name).try(:underscore)
27-
key.try(:pluralize)
26+
root || derived_root
2827
end
2928

3029
def paginated?
@@ -36,6 +35,13 @@ def paginated?
3635
protected
3736

3837
attr_reader :serializers
38+
39+
private
40+
41+
def derived_root
42+
key = serializers.first.try(:json_key) || object.try(:name).try(:underscore)
43+
key.try(:pluralize)
44+
end
3945
end
4046
end
4147
end

test/action_controller/serialization_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def test_render_array_using_custom_root
171171
with_adapter :json do
172172
get :render_array_using_custom_root
173173
end
174-
expected = { custom_roots: [{ name: 'Name 1', description: 'Description 1' }] }
174+
expected = { custom_root: [{ name: 'Name 1', description: 'Description 1' }] }
175175
assert_equal 'application/json', @response.content_type
176176
assert_equal expected.to_json, @response.body
177177
end
@@ -181,7 +181,7 @@ def test_render_array_that_is_empty_using_custom_root
181181
get :render_array_that_is_empty_using_custom_root
182182
end
183183

184-
expected = { custom_roots: [] }
184+
expected = { custom_root: [] }
185185
assert_equal 'application/json', @response.content_type
186186
assert_equal expected.to_json, @response.body
187187
end

test/collection_serializer_test.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ def test_root_with_no_serializers
6262
assert_equal expected, @serializer.root
6363
end
6464

65-
def test_json_key
66-
assert_equal 'comments', @serializer.json_key
65+
def test_json_key_with_resource_with_serializer
66+
singular_key = @serializer.send(:serializers).first.json_key
67+
assert_equal singular_key.pluralize, @serializer.json_key
6768
end
6869

6970
def test_json_key_with_resource_with_name_and_no_serializers
@@ -84,13 +85,15 @@ def test_json_key_with_resource_without_name_and_no_serializers
8485
end
8586

8687
def test_json_key_with_root
87-
serializer = collection_serializer.new(@resource, root: 'custom_root')
88-
assert_equal 'custom_roots', serializer.json_key
88+
expected = 'custom_root'
89+
serializer = collection_serializer.new(@resource, root: expected)
90+
assert_equal expected, serializer.json_key
8991
end
9092

9193
def test_json_key_with_root_and_no_serializers
92-
serializer = collection_serializer.new(build_named_collection, root: 'custom_root')
93-
assert_equal 'custom_roots', serializer.json_key
94+
expected = 'custom_root'
95+
serializer = collection_serializer.new(build_named_collection, root: expected)
96+
assert_equal expected, serializer.json_key
9497
end
9598
end
9699
end

0 commit comments

Comments
 (0)