Skip to content

Commit

Permalink
Merge pull request #2412 from tashirosota/v0.10/add_raise_cannot_infe…
Browse files Browse the repository at this point in the history
…r_root_key_error_option

Add raise_cannot_infer_root_key_error to config
  • Loading branch information
wasifhossain committed Mar 3, 2021
2 parents f495f44 + 7a80f98 commit 01c0d69
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
2 changes: 2 additions & 0 deletions lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ def config.array_serializer
config.jsonapi_include_toplevel_object = false
config.jsonapi_use_foreign_key_on_belongs_to_relationship = false
config.include_data_default = true
# Raise ActiveModel::Serializer::CollectionSerializer::CannotInferRootKeyError when cannot infer root key from collection type
config.raise_cannot_infer_root_key_error = true

# For configuring how serializers are found.
# This should be an array of procs.
Expand Down
13 changes: 11 additions & 2 deletions lib/active_model/serializer/collection_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ def json_key
key ||= object.respond_to?(:name) ? object.name && object.name.underscore : nil
# 4. key may be nil for empty collection and no serializer option
key &&= key.pluralize
# 5. fail if the key cannot be determined
key || fail(ArgumentError, 'Cannot infer root key from collection type. Please specify the root or each_serializer option, or render a JSON String')
if raise_cannot_infer_root_key_error?
# 5. fail if the key cannot be determined
key || fail(CannotInferRootKeyError, 'Cannot infer root key from collection type. Please specify the root or each_serializer option, or render a JSON String')
end
key
end
# rubocop:enable Metrics/CyclomaticComplexity

Expand All @@ -60,12 +63,18 @@ def paginated?
object.respond_to?(:size)
end

class CannotInferRootKeyError < StandardError; end

protected

attr_reader :serializers, :options

private

def raise_cannot_infer_root_key_error?
ActiveModelSerializers.config.raise_cannot_infer_root_key_error
end

def serializers_from_resources
serializer_context_class = options.fetch(:serializer_context_class, ActiveModel::Serializer)
object.map do |resource|
Expand Down
24 changes: 22 additions & 2 deletions test/collection_serializer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class MessagesSerializer < ActiveModel::Serializer
type 'messages'
end

class NonTypeSerializer < ActiveModel::Serializer; end

def setup
@singular_model = SingularModel.new
@has_many_model = HasManyModel.new
Expand Down Expand Up @@ -95,18 +97,36 @@ def test_json_key_with_resource_with_nil_name_and_no_serializers
resource = []
resource.define_singleton_method(:name) { nil }
serializer = collection_serializer.new(resource)
assert_raise ArgumentError do
assert_raise ActiveModel::Serializer::CollectionSerializer::CannotInferRootKeyError do
serializer.json_key
end
end

def test_json_key_with_resource_without_name_and_no_serializers
serializer = collection_serializer.new([])
assert_raise ArgumentError do
assert_raise ActiveModel::Serializer::CollectionSerializer::CannotInferRootKeyError do
serializer.json_key
end
end

def test_json_key_with_empty_resources_with_non_type_serializer
resource = []
serializer = collection_serializer.new(resource, serializer: NonTypeSerializer)
assert_raise ActiveModel::Serializer::CollectionSerializer::CannotInferRootKeyError do
serializer.json_key
end
end

def test_json_key_with_empty_resources_with_non_type_serializer_when_raise_cannot_infer_root_key_error_is_false
previous_raise_cannot_infer_root_key_error = ActiveModelSerializers.config.raise_cannot_infer_root_key_error
ActiveModelSerializers.config.raise_cannot_infer_root_key_error = false
resource = []
serializer = collection_serializer.new(resource, serializer: NonTypeSerializer)
assert_nil serializer.json_key
ensure
ActiveModelSerializers.config.raise_cannot_infer_root_key_error = previous_raise_cannot_infer_root_key_error
end

def test_json_key_with_empty_resources_with_serializer
resource = []
serializer = collection_serializer.new(resource, serializer: MessagesSerializer)
Expand Down

0 comments on commit 01c0d69

Please sign in to comment.