Skip to content

respect config.default_includes in #serializable_hash #1762

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

Closed
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
2 changes: 1 addition & 1 deletion lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def success?
# serializer.as_json(include: { posts: { include: { comments: { only: :body } }, only: :title } })
def serializable_hash(adapter_opts = nil)
adapter_opts ||= {}
adapter_opts = { include: '*', adapter: :attributes }.merge!(adapter_opts)
adapter_opts = { include: config.default_includes, adapter: :attributes }.merge!(adapter_opts)
Copy link
Member

Choose a reason for hiding this comment

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

good call. would you mind adding a Changelog entry?

Copy link
Member

Choose a reason for hiding this comment

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

but should be ActiveModelSerializer.default_includes, not config.default_includes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You mean ActiveModelSerializers.config.default_includes? ActiveModelSerializers module does not have default_includes method, it is an error to ActiveModelSerializers.default_includes.

adapter = ActiveModelSerializers::Adapter.create(self, adapter_opts)
adapter.serializable_hash(adapter_opts)
end
Expand Down
15 changes: 15 additions & 0 deletions test/serializers/serialization_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ class AuthorSerializer < ActiveModel::Serializer
assert_equal @expected_hash, @serializer_instance.serializable_hash
end

test '#serializable_hash respect config.default_includes' do
with_default_includes '' do
expected_hash = { id: 2, title: 'The Blog' }
assert_equal expected_hash, @serializer_instance.serializable_hash
end
end

test '#as_json is the same as generated by the attributes adapter' do
assert_equal @serializable.as_json, @serializer_instance.as_json
assert_equal @expected_hash, @serializer_instance.as_json
Expand All @@ -50,6 +57,14 @@ class AuthorSerializer < ActiveModel::Serializer
assert_equal @serializable.serializable_hash, @serializer_instance.to_hash
assert_equal @expected_hash, @serializer_instance.to_hash
end

def with_default_includes(include_tree)
original = ActiveModelSerializers.config.default_includes
ActiveModelSerializers.config.default_includes = include_tree
yield
ensure
ActiveModelSerializers.config.default_includes = original
end
end
end
end