mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
add raise_cannot_infer_root_key_error to config
This commit is contained in:
parent
8f38571ed9
commit
8fe1aee255
@ -146,6 +146,8 @@ module ActiveModel
|
|||||||
config.jsonapi_include_toplevel_object = false
|
config.jsonapi_include_toplevel_object = false
|
||||||
config.jsonapi_use_foreign_key_on_belongs_to_relationship = false
|
config.jsonapi_use_foreign_key_on_belongs_to_relationship = false
|
||||||
config.include_data_default = true
|
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.
|
# For configuring how serializers are found.
|
||||||
# This should be an array of procs.
|
# This should be an array of procs.
|
||||||
|
|||||||
@ -48,8 +48,11 @@ module ActiveModel
|
|||||||
key ||= object.respond_to?(:name) ? object.name && object.name.underscore : nil
|
key ||= object.respond_to?(:name) ? object.name && object.name.underscore : nil
|
||||||
# 4. key may be nil for empty collection and no serializer option
|
# 4. key may be nil for empty collection and no serializer option
|
||||||
key &&= key.pluralize
|
key &&= key.pluralize
|
||||||
|
if raise_cannot_infer_root_key_error?
|
||||||
# 5. fail if the key cannot be determined
|
# 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')
|
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
|
end
|
||||||
# rubocop:enable Metrics/CyclomaticComplexity
|
# rubocop:enable Metrics/CyclomaticComplexity
|
||||||
|
|
||||||
@ -60,12 +63,18 @@ module ActiveModel
|
|||||||
object.respond_to?(:size)
|
object.respond_to?(:size)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class CannotInferRootKeyError < StandardError; end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
attr_reader :serializers, :options
|
attr_reader :serializers, :options
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def raise_cannot_infer_root_key_error?
|
||||||
|
ActiveModelSerializers.config.raise_cannot_infer_root_key_error
|
||||||
|
end
|
||||||
|
|
||||||
def serializers_from_resources
|
def serializers_from_resources
|
||||||
serializer_context_class = options.fetch(:serializer_context_class, ActiveModel::Serializer)
|
serializer_context_class = options.fetch(:serializer_context_class, ActiveModel::Serializer)
|
||||||
object.map do |resource|
|
object.map do |resource|
|
||||||
|
|||||||
@ -22,6 +22,8 @@ module ActiveModel
|
|||||||
type 'messages'
|
type 'messages'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class NonTypeSerializer < ActiveModel::Serializer; end
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
@singular_model = SingularModel.new
|
@singular_model = SingularModel.new
|
||||||
@has_many_model = HasManyModel.new
|
@has_many_model = HasManyModel.new
|
||||||
@ -95,18 +97,36 @@ module ActiveModel
|
|||||||
resource = []
|
resource = []
|
||||||
resource.define_singleton_method(:name) { nil }
|
resource.define_singleton_method(:name) { nil }
|
||||||
serializer = collection_serializer.new(resource)
|
serializer = collection_serializer.new(resource)
|
||||||
assert_raise ArgumentError do
|
assert_raise ActiveModel::Serializer::CollectionSerializer::CannotInferRootKeyError do
|
||||||
serializer.json_key
|
serializer.json_key
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_json_key_with_resource_without_name_and_no_serializers
|
def test_json_key_with_resource_without_name_and_no_serializers
|
||||||
serializer = collection_serializer.new([])
|
serializer = collection_serializer.new([])
|
||||||
assert_raise ArgumentError do
|
assert_raise ActiveModel::Serializer::CollectionSerializer::CannotInferRootKeyError do
|
||||||
serializer.json_key
|
serializer.json_key
|
||||||
end
|
end
|
||||||
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
|
def test_json_key_with_empty_resources_with_serializer
|
||||||
resource = []
|
resource = []
|
||||||
serializer = collection_serializer.new(resource, serializer: MessagesSerializer)
|
serializer = collection_serializer.new(resource, serializer: MessagesSerializer)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user