diff --git a/lib/active_model/serializer/adapter/json_api.rb b/lib/active_model/serializer/adapter/json_api.rb index 71327b3c..0d93cac5 100644 --- a/lib/active_model/serializer/adapter/json_api.rb +++ b/lib/active_model/serializer/adapter/json_api.rb @@ -8,7 +8,7 @@ module ActiveModel end def serializable_hash(options = {}) - @root = (options[:root] || serializer.json_key).to_s.pluralize.to_sym + @root = (options[:root] || serializer.json_key.to_s.pluralize).to_sym @hash = {} if serializer.respond_to?(:each) diff --git a/test/action_controller/serialization_test.rb b/test/action_controller/serialization_test.rb index 4103e273..fbbb48f9 100644 --- a/test/action_controller/serialization_test.rb +++ b/test/action_controller/serialization_test.rb @@ -24,6 +24,16 @@ module ActionController ActiveModel::Serializer.config.adapter = old_adapter end + def render_using_custom_root_in_adapter_with_a_default + old_adapter = ActiveModel::Serializer.config.adapter + # JSON-API adapter sets root by default + ActiveModel::Serializer.config.adapter = ActiveModel::Serializer::Adapter::JsonApi + @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) + render json: @profile, root: "profile" + ensure + ActiveModel::Serializer.config.adapter = old_adapter + end + def render_array_using_implicit_serializer array = [ Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }), @@ -57,6 +67,13 @@ module ActionController assert_equal '{"profiles":{"name":"Name 1","description":"Description 1"}}', @response.body end + def test_render_using_custom_root_in_adapter_with_a_default + get :render_using_custom_root_in_adapter_with_a_default + + assert_equal 'application/json', @response.content_type + assert_equal '{"profile":{"name":"Name 1","description":"Description 1"}}', @response.body + end + def test_render_array_using_implicit_serializer get :render_array_using_implicit_serializer assert_equal 'application/json', @response.content_type