Skip to content

Commit 01c0d69

Browse files
authored
Merge pull request #2412 from tashirosota/v0.10/add_raise_cannot_infer_root_key_error_option
Add raise_cannot_infer_root_key_error to config
2 parents f495f44 + 7a80f98 commit 01c0d69

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

lib/active_model/serializer.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ def config.array_serializer
146146
config.jsonapi_include_toplevel_object = false
147147
config.jsonapi_use_foreign_key_on_belongs_to_relationship = false
148148
config.include_data_default = true
149+
# Raise ActiveModel::Serializer::CollectionSerializer::CannotInferRootKeyError when cannot infer root key from collection type
150+
config.raise_cannot_infer_root_key_error = true
149151

150152
# For configuring how serializers are found.
151153
# This should be an array of procs.

lib/active_model/serializer/collection_serializer.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,11 @@ def json_key
4848
key ||= object.respond_to?(:name) ? object.name && object.name.underscore : nil
4949
# 4. key may be nil for empty collection and no serializer option
5050
key &&= key.pluralize
51-
# 5. fail if the key cannot be determined
52-
key || fail(ArgumentError, 'Cannot infer root key from collection type. Please specify the root or each_serializer option, or render a JSON String')
51+
if raise_cannot_infer_root_key_error?
52+
# 5. fail if the key cannot be determined
53+
key || fail(CannotInferRootKeyError, 'Cannot infer root key from collection type. Please specify the root or each_serializer option, or render a JSON String')
54+
end
55+
key
5356
end
5457
# rubocop:enable Metrics/CyclomaticComplexity
5558

@@ -60,12 +63,18 @@ def paginated?
6063
object.respond_to?(:size)
6164
end
6265

66+
class CannotInferRootKeyError < StandardError; end
67+
6368
protected
6469

6570
attr_reader :serializers, :options
6671

6772
private
6873

74+
def raise_cannot_infer_root_key_error?
75+
ActiveModelSerializers.config.raise_cannot_infer_root_key_error
76+
end
77+
6978
def serializers_from_resources
7079
serializer_context_class = options.fetch(:serializer_context_class, ActiveModel::Serializer)
7180
object.map do |resource|

test/collection_serializer_test.rb

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class MessagesSerializer < ActiveModel::Serializer
2222
type 'messages'
2323
end
2424

25+
class NonTypeSerializer < ActiveModel::Serializer; end
26+
2527
def setup
2628
@singular_model = SingularModel.new
2729
@has_many_model = HasManyModel.new
@@ -95,18 +97,36 @@ def test_json_key_with_resource_with_nil_name_and_no_serializers
9597
resource = []
9698
resource.define_singleton_method(:name) { nil }
9799
serializer = collection_serializer.new(resource)
98-
assert_raise ArgumentError do
100+
assert_raise ActiveModel::Serializer::CollectionSerializer::CannotInferRootKeyError do
99101
serializer.json_key
100102
end
101103
end
102104

103105
def test_json_key_with_resource_without_name_and_no_serializers
104106
serializer = collection_serializer.new([])
105-
assert_raise ArgumentError do
107+
assert_raise ActiveModel::Serializer::CollectionSerializer::CannotInferRootKeyError do
106108
serializer.json_key
107109
end
108110
end
109111

112+
def test_json_key_with_empty_resources_with_non_type_serializer
113+
resource = []
114+
serializer = collection_serializer.new(resource, serializer: NonTypeSerializer)
115+
assert_raise ActiveModel::Serializer::CollectionSerializer::CannotInferRootKeyError do
116+
serializer.json_key
117+
end
118+
end
119+
120+
def test_json_key_with_empty_resources_with_non_type_serializer_when_raise_cannot_infer_root_key_error_is_false
121+
previous_raise_cannot_infer_root_key_error = ActiveModelSerializers.config.raise_cannot_infer_root_key_error
122+
ActiveModelSerializers.config.raise_cannot_infer_root_key_error = false
123+
resource = []
124+
serializer = collection_serializer.new(resource, serializer: NonTypeSerializer)
125+
assert_nil serializer.json_key
126+
ensure
127+
ActiveModelSerializers.config.raise_cannot_infer_root_key_error = previous_raise_cannot_infer_root_key_error
128+
end
129+
110130
def test_json_key_with_empty_resources_with_serializer
111131
resource = []
112132
serializer = collection_serializer.new(resource, serializer: MessagesSerializer)

0 commit comments

Comments
 (0)