Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add jsonapi_include_toplevel_object adapter option #1991

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/active_model_serializers/adapter/json_api.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# coding: utf-8
# {http://jsonapi.org/format/ JSON API specification}
# rubocop:disable Style/AsciiComments
# TODO: implement!
Expand Down Expand Up @@ -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] ||= {}
Expand Down Expand Up @@ -170,7 +171,7 @@ def success_document
def failure_document
hash = {}
# PR Please :)
# Jsonapi.add!(hash)
# Jsonapi.add!(hash, instance_options)

# toplevel_errors
# definition:
Expand Down
11 changes: 7 additions & 4 deletions lib/active_model_serializers/adapter/json_api/jsonapi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/active_model_serializers/serializable_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should just be jsonapi_object, I think. (jsonapi can't be the key since render uses that

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A question I have is what should be different between wanting to pass in your own jsonapi object vs. telling the adapter to include the default one?

include ActiveModelSerializers::Logging

delegate :serializable_hash, :as_json, :to_json, to: :adapter
Expand Down
14 changes: 14 additions & 0 deletions test/adapter/json_api/toplevel_jsonapi_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,27 @@ 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)
refute_nil(hash[: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)
Expand Down