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 top-level jsonapi member #1050

Merged
merged 1 commit into from
Oct 2, 2015
Merged

Add top-level jsonapi member #1050

merged 1 commit into from
Oct 2, 2015

Conversation

bf4
Copy link
Member

@bf4 bf4 commented Aug 12, 2015

Ref: http://jsonapi.org/format/#document-jsonapi-object

  • jsonapi_include_toplevel_object: Whether to include a top level JSON API member
    in the response document.
    Default: false.
  • Used when jsonapi_include_toplevel_object is true:
    • jsonapi_version: The latest version of the spec the API conforms to.
      Default: '1.0'.
    • jsonapi_toplevel_meta: Optional metadata. Not included if empty.
      Default: {}.

Example meta usage:

              jsonapi_include_toplevel_object = true,
              jsonapi_toplevel_meta = {
                'copyright' => 'Copyright 2015 Example Corp.'
              }

@joaomdmoura
Copy link
Member

Yup! I'm interest on this @bf4

@joaomdmoura joaomdmoura added this to the 0.10 milestone Aug 17, 2015
@bf4
Copy link
Member Author

bf4 commented Aug 21, 2015

This is a little harder than I thought due the somewhat recursive way adapters work.

@joaomdmoura
Copy link
Member

No worries, take your time, let me know if there is something I can do in order to help you.

@bf4
Copy link
Member Author

bf4 commented Aug 21, 2015

Well, I think I'm going to work on other stuff for a bit. I was just looking at warnings, and also at how using class Adapter as both a namespace and subclass causes some circular requires, and if maybe we should separate AbstractAdapter out of Adapter, leaving just the create and adapter_class methods (plus those added by the #1017 )

@joaomdmoura
Copy link
Member

Yep, please, a warnings clearing would be great! 😄

@@ -54,7 +78,7 @@ def serializable_hash_for_single_resource(serializer, options)
primary_data = primary_data_for(serializer, options)
relationships = relationships_for(serializer)
included = included_for(serializer)
hash = { data: primary_data }
hash[:data] = primary_data # FIXME: should be an [], not a {}
Copy link
Member Author

Choose a reason for hiding this comment

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

ugh

Copy link
Member Author

Choose a reason for hiding this comment

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

n/m, I guess this is legal

Primary data MUST be either:

  • a single resource object, a single resource identifier object, or null, for requests that target single resources
  • an array of resource objects, an array of resource identifier objects, or an empty array ([]), for requests that target resource collections

Copy link
Contributor

Choose a reason for hiding this comment

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

It is, indeed.

Copy link
Contributor

Choose a reason for hiding this comment

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

More than legal, it is actually the right behavior when targeting a single resource.

Copy link
Member Author

Choose a reason for hiding this comment

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

learning..

Copy link
Member Author

Choose a reason for hiding this comment

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

rebased to remove comment, but this comment remains...

@bf4 bf4 force-pushed the json_api_member branch 2 times, most recently from bea3f7e to e821811 Compare September 16, 2015 14:59
@@ -9,3 +9,9 @@ The following configuration options can be set on `ActiveModel::Serializer.confi
## JSON API

- `jsonapi_resource_type`: Whether the `type` attributes of resources should be singular or plural. Possible values: `:singular, :plural`. Default: `:plural`.
- `jsonapi_toplevel_member`: Whether to include a [top level JSON API member](http://jsonapi.org/format/#document-jsonapi-object)
Copy link
Member Author

Choose a reason for hiding this comment

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

defined in JsonApi adapter

@@ -11,4 +11,5 @@
* remove root key option and split JSON adapter [@joaomdmoura]
* adds FlattenJSON as default adapter [@joaomdmoura]
* adds support for `pagination links` at top level of JsonApi adapter [@bacarini]
* adds extended format for `include` option to JSONAPI adapter [@beauby]
* adds extended format for `include` option to JsonApi adapter [@beauby]
* adds support for top level jsonapi member support [@beauby]
Copy link
Contributor

Choose a reason for hiding this comment

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

There's an extraneous "support" at the end. Plus it should read [@beauby, @bf4] :)

object = {
jsonapi: {
version: ActiveModel::Serializer.config.jsonapi_version,
meta: ActiveModel::Serializer.config.jsonapi_toplevel_meta
Copy link
Contributor

Choose a reason for hiding this comment

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

Meta is optional as per the spec, so it should't be included by default.

@bf4 bf4 force-pushed the json_api_member branch 4 times, most recently from 7789371 to 4775d8f Compare September 18, 2015 04:43
@bf4 bf4 force-pushed the json_api_member branch 2 times, most recently from 8223f41 to e34c760 Compare September 21, 2015 15:41
ActiveModel::Serializer.config.jsonapi_include_toplevel_member
end

def add!(document)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why the side-effects 😿

Copy link
Member Author

Choose a reason for hiding this comment

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

what side-effects?

Copy link
Contributor

Choose a reason for hiding this comment

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

Having add! modify its argument instead of returning a value.

Copy link
Member Author

Choose a reason for hiding this comment

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

But, isn't this question the same as why do I prefer foo.merge!(bar) over foo.merge!(build_bar)?

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm not really, to me, it's more a matter of do_some_magic(foo) vs foo[:attribute] = compute_something.

Copy link
Member Author

Choose a reason for hiding this comment

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

Would you mind just adding a commit to my branch? You obviously have a pretty strong opinions I don't share, so it would be faster if you changed it

Copy link
Member Author

Choose a reason for hiding this comment

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

I really don't care that strongly since I see it as internal for now till we figure out how we'd like to use it, if at all, as long as it's a function

Copy link
Contributor

Choose a reason for hiding this comment

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

Will do immediately.

Copy link
Contributor

Choose a reason for hiding this comment

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

Done.

Copy link
Member Author

Choose a reason for hiding this comment

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

Since you have the (first and) last commit, do you might (rebasing against master and) getting it passing on ci?

@beauby beauby force-pushed the json_api_member branch 4 times, most recently from 7b22480 to 43c56ac Compare September 21, 2015 19:13
serializable_hash_for_single_resource(options)
end

ApiObjects::JsonApi.include_member? && hash.merge!(ApiObjects::JsonApi.member)
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you prefer it like this?

Copy link
Member Author

Choose a reason for hiding this comment

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

I do, esp. if you're ok with it... I'm willing to be persuaded... the whole ApiObjects::JsonApi is just an idea I'm playing with.. not very useful if it's the only one, but might lead to good abstractions for other objects, like errors, paginations, meta, links, etc.

What do you think of renaming member to object? It would align with serializer.object and make sense in context.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm ok with renaming member to object, although both those names are a bit confusing.

@beauby
Copy link
Contributor

beauby commented Sep 30, 2015

@bf4 Let's pair on this one when you have time and get it over with.

@NullVoxPopuli
Copy link
Contributor

👍

end

def object
object = {
Copy link
Member Author

Choose a reason for hiding this comment

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

change to @object ||=

@bf4 bf4 force-pushed the json_api_member branch from c1d8e95 to 06ae191 Compare October 1, 2015 18:23
@bf4
Copy link
Member Author

bf4 commented Oct 1, 2015

@beauby squashed to one commit, set you as the 'committer' git log --pretty='%cn' -n1 HEAD shows you (I'm the 'author')

didn't do the jsonapi.config as it was funky and not high value

ActiveModel::Serializer.config.jsonapi_version = '1.0'
ActiveModel::Serializer.config.jsonapi_toplevel_meta = {}
# Make JSON API top-level jsonapi member opt-in
# ref: http://jsonapi.org/format/#document-top-level
Copy link
Member Author

Choose a reason for hiding this comment

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

@beauby :)

@bf4 bf4 force-pushed the json_api_member branch from 06ae191 to 9d7da8a Compare October 2, 2015 02:22
bf4 added a commit that referenced this pull request Oct 2, 2015
Add top-level jsonapi member to JSON API adapter
@bf4 bf4 merged commit a2bfe19 into rails-api:master Oct 2, 2015
@bf4 bf4 deleted the json_api_member branch October 2, 2015 04:03
@joaomdmoura joaomdmoura mentioned this pull request Oct 6, 2015
11 tasks
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.

4 participants