mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 14:56:50 +00:00
Handle special-case of Array serializer with unserializable elements
This commit is contained in:
parent
cf77786da2
commit
e5d1e40dbd
@ -42,10 +42,9 @@ module ActionController
|
|||||||
@_serializer_opts[:scope] ||= serialization_scope
|
@_serializer_opts[:scope] ||= serialization_scope
|
||||||
@_serializer_opts[:scope_name] = _serialization_scope
|
@_serializer_opts[:scope_name] = _serialization_scope
|
||||||
|
|
||||||
object = serializer.new(resource, @_serializer_opts)
|
begin
|
||||||
|
object = serializer.new(resource, @_serializer_opts)
|
||||||
if serializer == ActiveModel::Serializer.config.array_serializer
|
rescue ActiveModel::Serializer::ArraySerializer::Error
|
||||||
resource = ActiveModel::Serializer::Adapter.create(object, @_adapter_opts) unless object.objects.all? {|i| i.nil?}
|
|
||||||
else
|
else
|
||||||
resource = ActiveModel::Serializer::Adapter.create(object, @_adapter_opts)
|
resource = ActiveModel::Serializer::Adapter.create(object, @_adapter_opts)
|
||||||
end
|
end
|
||||||
|
|||||||
@ -211,7 +211,7 @@ module ActiveModel
|
|||||||
association_value,
|
association_value,
|
||||||
options.except(:serializer).merge(serializer_from_options(association_options))
|
options.except(:serializer).merge(serializer_from_options(association_options))
|
||||||
)
|
)
|
||||||
rescue NoMethodError
|
rescue ActiveModel::Serializer::ArraySerializer::Error
|
||||||
# 1. Failure to serialize an element in a collection, e.g. [ {hi: "Steve" } ] will fail
|
# 1. Failure to serialize an element in a collection, e.g. [ {hi: "Steve" } ] will fail
|
||||||
# with NoMethodError when the ArraySerializer finds no serializer for the hash { hi: "Steve" },
|
# with NoMethodError when the ArraySerializer finds no serializer for the hash { hi: "Steve" },
|
||||||
# and tries to call new on that nil.
|
# and tries to call new on that nil.
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
module ActiveModel
|
module ActiveModel
|
||||||
class Serializer
|
class Serializer
|
||||||
class ArraySerializer
|
class ArraySerializer
|
||||||
|
Error = Class.new(StandardError)
|
||||||
include Enumerable
|
include Enumerable
|
||||||
delegate :each, to: :@objects
|
delegate :each, to: :@objects
|
||||||
|
|
||||||
@ -14,7 +15,9 @@ module ActiveModel
|
|||||||
ActiveModel::Serializer.serializer_for(object)
|
ActiveModel::Serializer.serializer_for(object)
|
||||||
)
|
)
|
||||||
|
|
||||||
unless serializer_class.nil?
|
if serializer_class.nil?
|
||||||
|
fail Error, "No serializer found for object: #{object.inspect}"
|
||||||
|
else
|
||||||
serializer_class.new(object, options.except(:serializer))
|
serializer_class.new(object, options.except(:serializer))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user