mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Fixes a bug in the HasOne association where serializing twice in a row causes
an exception.
This commit is contained in:
parent
99677c0c58
commit
8c3866532a
@ -53,7 +53,7 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def build_serializer(object, options = {})
|
||||
if object.respond_to?(:to_ary)
|
||||
if object.respond_to?(:to_ary) && !(@serializer_class && @serializer_class <= ArraySerializer)
|
||||
use_array_serializer!
|
||||
else
|
||||
@serializer_class ||= Serializer.serializer_for(object) || DefaultSerializer
|
||||
|
||||
@ -0,0 +1,24 @@
|
||||
require 'test_helper'
|
||||
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
class Association
|
||||
class BuildSerializerTest < ActiveModel::TestCase
|
||||
def setup
|
||||
@association = Association::HasOne.new('post', serializer: PostSerializer)
|
||||
@post = Post.new({ title: 'Title 1', body: 'Body 1', date: '1/1/2000' })
|
||||
end
|
||||
|
||||
def test_build_serializer_for_array_called_twice
|
||||
2.times do
|
||||
serializer = @association.build_serializer([@post])
|
||||
each_serializer = serializer.serializer_for(@post)
|
||||
|
||||
assert_instance_of(ArraySerializer, serializer)
|
||||
assert_instance_of(PostSerializer, each_serializer)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue
Block a user