mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Merge pull request #213 from michihuber/fix_rendering_nil_with_custom_serializer
Fix rendering nil with custom serializer
This commit is contained in:
commit
06c65871a8
@ -274,6 +274,7 @@ module ActiveModel
|
|||||||
# Returns a hash representation of the serializable
|
# Returns a hash representation of the serializable
|
||||||
# object without the root.
|
# object without the root.
|
||||||
def serializable_hash
|
def serializable_hash
|
||||||
|
return nil if @object.nil?
|
||||||
instrument(:serialize, :serializer => self.class.name) do
|
instrument(:serialize, :serializer => self.class.name) do
|
||||||
@node = attributes
|
@node = attributes
|
||||||
instrument :associations do
|
instrument :associations do
|
||||||
|
|||||||
@ -63,6 +63,10 @@ class RenderJsonTest < ActionController::TestCase
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class DummyCustomSerializer < ActiveModel::Serializer
|
||||||
|
attributes :id
|
||||||
|
end
|
||||||
|
|
||||||
class HypermediaSerializable
|
class HypermediaSerializable
|
||||||
def active_model_serializer
|
def active_model_serializer
|
||||||
HypermediaSerializer
|
HypermediaSerializer
|
||||||
@ -117,6 +121,11 @@ class RenderJsonTest < ActionController::TestCase
|
|||||||
render :json => ActiveSupport::JSON.encode(:hello => 'world')
|
render :json => ActiveSupport::JSON.encode(:hello => 'world')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def render_json_nil_with_custom_serializer
|
||||||
|
render :json => nil, :serializer => DummyCustomSerializer
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
def render_json_with_extra_options
|
def render_json_with_extra_options
|
||||||
render :json => JsonRenderable.new, :except => [:c, :e]
|
render :json => JsonRenderable.new, :except => [:c, :e]
|
||||||
end
|
end
|
||||||
@ -216,6 +225,11 @@ class RenderJsonTest < ActionController::TestCase
|
|||||||
assert_equal '[]', @response.body
|
assert_equal '[]', @response.body
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_render_json_nil_with_custom_serializer
|
||||||
|
get :render_json_nil_with_custom_serializer
|
||||||
|
assert_equal "{\"dummy_custom\":null}", @response.body
|
||||||
|
end
|
||||||
|
|
||||||
def test_render_json
|
def test_render_json
|
||||||
get :render_json_hello_world
|
get :render_json_hello_world
|
||||||
assert_equal '{"hello":"world"}', @response.body
|
assert_equal '{"hello":"world"}', @response.body
|
||||||
|
|||||||
@ -276,6 +276,28 @@ class SerializerTest < ActiveModel::TestCase
|
|||||||
assert_equal({ :my_blog => { :author => nil } }, serializer.new(blog, :scope => user).as_json)
|
assert_equal({ :my_blog => { :author => nil } }, serializer.new(blog, :scope => user).as_json)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_nil_root_object
|
||||||
|
user = User.new
|
||||||
|
blog = nil
|
||||||
|
|
||||||
|
serializer = Class.new(BlogSerializer) do
|
||||||
|
root false
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_equal(nil, serializer.new(blog, :scope => user).as_json)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_custom_root_with_nil_root_object
|
||||||
|
user = User.new
|
||||||
|
blog = nil
|
||||||
|
|
||||||
|
serializer = Class.new(BlogSerializer) do
|
||||||
|
root :my_blog
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_equal({ :my_blog => nil }, serializer.new(blog, :scope => user).as_json)
|
||||||
|
end
|
||||||
|
|
||||||
def test_false_root
|
def test_false_root
|
||||||
user = User.new
|
user = User.new
|
||||||
blog = Blog.new
|
blog = Blog.new
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user