From 0f0ef2baf5494ed505a336407530982f1e07af2f Mon Sep 17 00:00:00 2001 From: Edward Loveall Date: Wed, 10 Jun 2015 18:20:34 -0400 Subject: [PATCH] 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 --- lib/active_model/serializer.rb | 2 +- .../explicit_serializer_test.rb | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/lib/active_model/serializer.rb b/lib/active_model/serializer.rb index 00eed5bc..0da1755b 100644 --- a/lib/active_model/serializer.rb +++ b/lib/active_model/serializer.rb @@ -217,7 +217,7 @@ module ActiveModel if serializer_class serializer = serializer_class.new( association_value, - options.merge(serializer_from_options(association_options)) + options.except(:serializer).merge(serializer_from_options(association_options)) ) elsif !association_value.nil? && !association_value.instance_of?(Object) association_options[:association_options][:virtual_value] = association_value diff --git a/test/action_controller/explicit_serializer_test.rb b/test/action_controller/explicit_serializer_test.rb index ff0a07e6..5fafafa5 100644 --- a/test/action_controller/explicit_serializer_test.rb +++ b/test/action_controller/explicit_serializer_test.rb @@ -55,6 +55,14 @@ module ActionController render json: [@post], each_serializer: PostPreviewSerializer end + + def render_using_explicit_each_serializer + @comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' }) + @author = Author.new(id: 1, name: 'Joao Moura.') + @post = Post.new({ id: 1, title: 'New Post', body: 'Body', comments: [@comment], author: @author }) + + render json: @post, each_serializer: PostSerializer + end end tests MyController @@ -105,6 +113,31 @@ module ActionController assert_equal expected.to_json, @response.body end + + def test_render_using_explicit_each_serializer + get :render_using_explicit_each_serializer + + expected = { + id: 1, + title: 'New Post', + body: 'Body', + comments: [ + { + id: 1, + body: 'ZOMG A COMMENT' } + ], + blog: { + id: 999, + name: 'Custom blog' + }, + author: { + id: 1, + name: 'Joao Moura.' + } + } + + assert_equal expected.to_json, @response.body + end end end end