diff --git a/lib/action_controller/serialization.rb b/lib/action_controller/serialization.rb index b2c605f5..4ac32093 100644 --- a/lib/action_controller/serialization.rb +++ b/lib/action_controller/serialization.rb @@ -64,9 +64,8 @@ module ActionController def build_json_serializer(resource, options) options = default_serializer_options.merge(options || {}) - serializer = - options.delete(:serializer) || - ActiveModel::Serializer.serializer_for(resource) + serializer = options.delete(:serializer) + serializer = ActiveModel::Serializer.serializer_for(resource) if serializer.nil? return unless serializer diff --git a/test/integration/action_controller/serialization_test.rb b/test/integration/action_controller/serialization_test.rb index 024dc0ab..c790e4a0 100644 --- a/test/integration/action_controller/serialization_test.rb +++ b/test/integration/action_controller/serialization_test.rb @@ -146,10 +146,26 @@ module ActionController end end + class JSONDumpSerializerTest < ActionController::TestCase + class MyController < ActionController::Base + def render_using_json_dump + render json: JSON.dump(hello: 'world') + end + end + + tests MyController + + def test_render_using_json_dump + get :render_using_json_dump + assert_equal 'application/json', @response.content_type + assert_equal '{"hello":"world"}', @response.body + end + end + class RailsSerializerTest < ActionController::TestCase class MyController < ActionController::Base def render_using_rails_behavior - render json: JSON.dump(hello: 'world') + render json: [Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })], serializer: false end end @@ -158,7 +174,7 @@ module ActionController def test_render_using_rails_behavior get :render_using_rails_behavior assert_equal 'application/json', @response.content_type - assert_equal '{"hello":"world"}', @response.body + assert_equal '[{"attributes":{"name":"Name 1","description":"Description 1","comments":"Comments 1"}}]', @response.body end end