Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't pluralize the CollectionSerializer#root for #json_key #1418

Merged
merged 1 commit into from
Jan 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ Breaking changes:
Adapter functions.
* named `Base` because it's a Rails-ism.
* It helps to isolate and highlight what the Adapter interface actually is.
- [#1418](https://github.com/rails-api/active_model_serializers/pull/1418)
serialized collections now use the root option as is; now, only the
root derived from the serializer or object is always pluralized.

Features:

Expand Down
10 changes: 8 additions & 2 deletions lib/active_model/serializer/collection_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ def initialize(resources, options = {})
end

def json_key
key = root || serializers.first.try(:json_key) || object.try(:name).try(:underscore)
key.try(:pluralize)
root || derived_root
end

def paginated?
Expand All @@ -36,6 +35,13 @@ def paginated?
protected

attr_reader :serializers

private

def derived_root
key = serializers.first.try(:json_key) || object.try(:name).try(:underscore)
key.try(:pluralize)
end
end
end
end
4 changes: 2 additions & 2 deletions test/action_controller/serialization_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def test_render_array_using_custom_root
with_adapter :json do
get :render_array_using_custom_root
end
expected = { custom_roots: [{ name: 'Name 1', description: 'Description 1' }] }
expected = { custom_root: [{ name: 'Name 1', description: 'Description 1' }] }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically this is a breaking change. I think this is ok since we still haven't released 0.10 but a note in the changelog would be good

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see this breaking a lot of apps.. I'm wondering how we can communicate that? @rails-api/ams

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's in the changelog under breaking changes so I think it's good now.

assert_equal 'application/json', @response.content_type
assert_equal expected.to_json, @response.body
end
Expand All @@ -181,7 +181,7 @@ def test_render_array_that_is_empty_using_custom_root
get :render_array_that_is_empty_using_custom_root
end

expected = { custom_roots: [] }
expected = { custom_root: [] }
assert_equal 'application/json', @response.content_type
assert_equal expected.to_json, @response.body
end
Expand Down
15 changes: 9 additions & 6 deletions test/collection_serializer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ def test_root_with_no_serializers
assert_equal expected, @serializer.root
end

def test_json_key
assert_equal 'comments', @serializer.json_key
def test_json_key_with_resource_with_serializer
singular_key = @serializer.send(:serializers).first.json_key
assert_equal singular_key.pluralize, @serializer.json_key
end

def test_json_key_with_resource_with_name_and_no_serializers
Expand All @@ -84,13 +85,15 @@ def test_json_key_with_resource_without_name_and_no_serializers
end

def test_json_key_with_root
serializer = collection_serializer.new(@resource, root: 'custom_root')
assert_equal 'custom_roots', serializer.json_key
expected = 'custom_root'
serializer = collection_serializer.new(@resource, root: expected)
assert_equal expected, serializer.json_key
end

def test_json_key_with_root_and_no_serializers
serializer = collection_serializer.new(build_named_collection, root: 'custom_root')
assert_equal 'custom_roots', serializer.json_key
expected = 'custom_root'
serializer = collection_serializer.new(build_named_collection, root: expected)
assert_equal expected, serializer.json_key
end
end
end
Expand Down