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

Conversation

nilsding
Copy link

@nilsding nilsding commented Dec 2, 2016

Purpose

Sometimes, I have the need to include the JSON-API top level object while it's disabled in the config. This PR adds the functionality to include it on the fly using an option to the serializer:

 ActiveModelSerializers::SerializableResource.new(
  UserSerializer.new(obj),
  adapter:                         :json_api,
  jsonapi_include_toplevel_object: true
).as_json

Changes

Pass instance options to the ActiveModelSerializers::Adapter::JsonApi::Jsonapi model, and modify the include_object? method to use the value of the instance option :jsonapi_include_toplevel_object instead of the global config, if given.

Caveats

Related GitHub issues

Additional helpful information

@mention-bot
Copy link

@nilsding, thanks for your PR! By analyzing the history of the files in this pull request, we identified @bf4, @domitian and @groyoh to be potential reviewers.

@NullVoxPopuli
Copy link
Contributor

@beauby do you have opinions on this?

@beauby
Copy link
Contributor

beauby commented Dec 4, 2016

I agree this should be overridable locally (and although it is weird that jsonapi_include_toplevel_object would be an option on the resource, since it is a property of the document, not of the primary resource, I don't see an other way to do it currently).

@bf4 bf4 changed the title Add jsonapi_include_toplevel_object option to serializer Add jsonapi_include_toplevel_object adapter option Dec 7, 2016
Copy link
Member

@bf4 bf4 left a comment

Choose a reason for hiding this comment

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

Idea is good, needs discussion of option name and distinction between turning on default via an option and passing in the jsonapi object in an option.

@@ -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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants