diff --git a/lib/active_model_serializers/adapter/json_api.rb b/lib/active_model_serializers/adapter/json_api.rb index 3d241e349..04bf5ca74 100644 --- a/lib/active_model_serializers/adapter/json_api.rb +++ b/lib/active_model_serializers/adapter/json_api.rb @@ -1,3 +1,4 @@ +# coding: utf-8 # {http://jsonapi.org/format/ JSON API specification} # rubocop:disable Style/AsciiComments # TODO: implement! @@ -135,7 +136,7 @@ def success_document # ] hash[:included] = included if included.any? - Jsonapi.add!(hash) + Jsonapi.add!(hash, instance_options) if instance_options[:links] hash[:links] ||= {} @@ -170,7 +171,7 @@ def success_document def failure_document hash = {} # PR Please :) - # Jsonapi.add!(hash) + # Jsonapi.add!(hash, instance_options) # toplevel_errors # definition: diff --git a/lib/active_model_serializers/adapter/json_api/jsonapi.rb b/lib/active_model_serializers/adapter/json_api/jsonapi.rb index e94578af6..9425e05b7 100644 --- a/lib/active_model_serializers/adapter/json_api/jsonapi.rb +++ b/lib/active_model_serializers/adapter/json_api/jsonapi.rb @@ -23,12 +23,15 @@ class JsonApi < Base module Jsonapi module_function - def add!(hash) - hash.merge!(object) if include_object? + def add!(hash, instance_options) + hash.merge!(object) if include_object?(instance_options) end - def include_object? - ActiveModelSerializers.config.jsonapi_include_toplevel_object + def include_object?(instance_options) + instance_options.fetch( + :jsonapi_include_toplevel_object, + ActiveModelSerializers.config.jsonapi_include_toplevel_object + ) end # TODO: see if we can cache this diff --git a/lib/active_model_serializers/serializable_resource.rb b/lib/active_model_serializers/serializable_resource.rb index f67cf2385..6d9ce59cf 100644 --- a/lib/active_model_serializers/serializable_resource.rb +++ b/lib/active_model_serializers/serializable_resource.rb @@ -2,7 +2,7 @@ module ActiveModelSerializers class SerializableResource - ADAPTER_OPTION_KEYS = Set.new([:include, :fields, :adapter, :meta, :meta_key, :links, :serialization_context, :key_transform]) + ADAPTER_OPTION_KEYS = Set.new([:include, :fields, :adapter, :meta, :meta_key, :links, :serialization_context, :key_transform, :jsonapi_include_toplevel_object]) include ActiveModelSerializers::Logging delegate :serializable_hash, :as_json, :to_json, to: :adapter diff --git a/test/adapter/json_api/toplevel_jsonapi_test.rb b/test/adapter/json_api/toplevel_jsonapi_test.rb index 7b0357e52..71645eadd 100644 --- a/test/adapter/json_api/toplevel_jsonapi_test.rb +++ b/test/adapter/json_api/toplevel_jsonapi_test.rb @@ -37,6 +37,13 @@ def test_disable_toplevel_jsonapi end end + def test_disable_toplevel_jsonapi_using_instance_options + with_config(jsonapi_include_toplevel_object: true) do + hash = serialize(@post, jsonapi_include_toplevel_object: false) + assert_nil(hash[:jsonapi]) + end + end + def test_enable_toplevel_jsonapi with_config(jsonapi_include_toplevel_object: true) do hash = serialize(@post) @@ -44,6 +51,13 @@ def test_enable_toplevel_jsonapi end end + def test_enable_toplevel_jsonapi_using_instance_options + with_config(jsonapi_include_toplevel_object: false) do + hash = serialize(@post, jsonapi_include_toplevel_object: true) + refute_nil(hash[:jsonapi]) + end + end + def test_default_toplevel_jsonapi_version with_config(jsonapi_include_toplevel_object: true) do hash = serialize(@post)