mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 23:06:50 +00:00
Merge pull request #160 from byroot/143-throw-decriptive-error
[PR] Throw a descriptive error if attempting to serialize an array with Serializer not ArraySerializer
This commit is contained in:
commit
720f1dabf8
@ -44,6 +44,11 @@ module ActionController
|
|||||||
(json.respond_to?(:active_model_serializer) && json.active_model_serializer)
|
(json.respond_to?(:active_model_serializer) && json.active_model_serializer)
|
||||||
|
|
||||||
if json.respond_to?(:to_ary)
|
if json.respond_to?(:to_ary)
|
||||||
|
unless serializer <= ActiveModel::ArraySerializer
|
||||||
|
raise ArgumentError.new("#{serializer.name} is not an ArraySerializer. " +
|
||||||
|
"You may want to use the :each_serializer option instead.")
|
||||||
|
end
|
||||||
|
|
||||||
if options[:root] != false && serializer.root != false
|
if options[:root] != false && serializer.root != false
|
||||||
# default root element for arrays is serializer's root or the controller name
|
# default root element for arrays is serializer's root or the controller name
|
||||||
# the serializer for an Array is ActiveModel::ArraySerializer
|
# the serializer for an Array is ActiveModel::ArraySerializer
|
||||||
|
|||||||
@ -152,6 +152,10 @@ class RenderJsonTest < ActionController::TestCase
|
|||||||
render :json => [Object.new], :each_serializer => CustomSerializer
|
render :json => [Object.new], :each_serializer => CustomSerializer
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def render_json_array_with_wrong_option
|
||||||
|
render :json => [Object.new], :serializer => CustomSerializer
|
||||||
|
end
|
||||||
|
|
||||||
def render_json_with_links
|
def render_json_with_links
|
||||||
render :json => HypermediaSerializable.new
|
render :json => HypermediaSerializable.new
|
||||||
end
|
end
|
||||||
@ -286,6 +290,12 @@ class RenderJsonTest < ActionController::TestCase
|
|||||||
assert_match '{"test":[{"hello":true}]}', @response.body
|
assert_match '{"test":[{"hello":true}]}', @response.body
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_render_json_array_with_wrong_option
|
||||||
|
assert_raise ArgumentError do
|
||||||
|
get :render_json_array_with_wrong_option
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def test_render_json_with_links
|
def test_render_json_with_links
|
||||||
get :render_json_with_links
|
get :render_json_with_links
|
||||||
assert_match '{"link":"http://www.nextangle.com/hypermedia"}', @response.body
|
assert_match '{"link":"http://www.nextangle.com/hypermedia"}', @response.body
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user