From 4b49d1b13200b2830d94bd7b945ce64e9a41cd58 Mon Sep 17 00:00:00 2001 From: Roman Greshny Date: Wed, 5 Nov 2014 16:34:08 +0200 Subject: [PATCH] fixed issue with rendering Hash which appears in rails 4.2.0.beta4 --- lib/active_model/serializer.rb | 2 +- .../namespaced_serialization_test.rb | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/active_model/serializer.rb b/lib/active_model/serializer.rb index 7a6ee7ca..01b439e7 100644 --- a/lib/active_model/serializer.rb +++ b/lib/active_model/serializer.rb @@ -56,7 +56,7 @@ end attr_reader :key_format def serializer_for(resource, options = {}) - if resource.respond_to?(:each) + if resource.respond_to?(:each) && !resource.is_a?(Hash) if Object.constants.include?(:ArraySerializer) ::ArraySerializer else diff --git a/test/integration/action_controller/namespaced_serialization_test.rb b/test/integration/action_controller/namespaced_serialization_test.rb index 1e758d29..4e29f77c 100644 --- a/test/integration/action_controller/namespaced_serialization_test.rb +++ b/test/integration/action_controller/namespaced_serialization_test.rb @@ -19,6 +19,10 @@ module ActionController def render_comments render json: [Comment.new(content: 'Comment 1')] end + + def render_hash + render json: {message: 'not found'}, status: 404 + end end tests TestNamespace::MyController @@ -42,6 +46,11 @@ module ActionController get :render_comments assert_serializer CommentSerializer end + + def test_render_hash_regression + get :render_hash + assert_equal JSON.parse(response.body), {'message' => 'not found'} + end end class OptionNamespacedSerializationTest < ActionController::TestCase @@ -93,4 +102,4 @@ module ActionController end end -end \ No newline at end of file +end