Skip to content

Commit 0f0ef2b

Browse files
committed
Don't pass serializer option to associated serializers
Fixes #870 Commit af81a40 introduced passing a serializer's 'options' along to its associated model serializers. Thus, an explicit 'each_serializer' passed to render for a singular resource would be passed on as the implicit 'serializer' for its associations. With @bf4
1 parent 7b0a85f commit 0f0ef2b

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

lib/active_model/serializer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def each_association(&block)
217217
if serializer_class
218218
serializer = serializer_class.new(
219219
association_value,
220-
options.merge(serializer_from_options(association_options))
220+
options.except(:serializer).merge(serializer_from_options(association_options))
221221
)
222222
elsif !association_value.nil? && !association_value.instance_of?(Object)
223223
association_options[:association_options][:virtual_value] = association_value

test/action_controller/explicit_serializer_test.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ def render_array_using_explicit_serializer_and_custom_serializers
5555

5656
render json: [@post], each_serializer: PostPreviewSerializer
5757
end
58+
59+
def render_using_explicit_each_serializer
60+
@comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
61+
@author = Author.new(id: 1, name: 'Joao Moura.')
62+
@post = Post.new({ id: 1, title: 'New Post', body: 'Body', comments: [@comment], author: @author })
63+
64+
render json: @post, each_serializer: PostSerializer
65+
end
5866
end
5967

6068
tests MyController
@@ -105,6 +113,31 @@ def test_render_array_using_explicit_serializer_and_custom_serializers
105113

106114
assert_equal expected.to_json, @response.body
107115
end
116+
117+
def test_render_using_explicit_each_serializer
118+
get :render_using_explicit_each_serializer
119+
120+
expected = {
121+
id: 1,
122+
title: 'New Post',
123+
body: 'Body',
124+
comments: [
125+
{
126+
id: 1,
127+
body: 'ZOMG A COMMENT' }
128+
],
129+
blog: {
130+
id: 999,
131+
name: 'Custom blog'
132+
},
133+
author: {
134+
id: 1,
135+
name: 'Joao Moura.'
136+
}
137+
}
138+
139+
assert_equal expected.to_json, @response.body
140+
end
108141
end
109142
end
110143
end

0 commit comments

Comments
 (0)