-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Conversation
Yup! I'm interest on this @bf4 |
This is a little harder than I thought due the somewhat recursive way adapters work. |
No worries, take your time, let me know if there is something I can do in order to help you. |
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 |
Yep, please, a warnings clearing would be great! 😄 |
fadd2b0
to
87d587e
Compare
@@ -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 {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ugh
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is, indeed.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
learning..
There was a problem hiding this comment.
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...
bea3f7e
to
e821811
Compare
@@ -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) |
There was a problem hiding this comment.
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] |
There was a problem hiding this comment.
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]
:)
e821811
to
a48485d
Compare
object = { | ||
jsonapi: { | ||
version: ActiveModel::Serializer.config.jsonapi_version, | ||
meta: ActiveModel::Serializer.config.jsonapi_toplevel_meta |
There was a problem hiding this comment.
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.
7789371
to
4775d8f
Compare
8223f41
to
e34c760
Compare
ActiveModel::Serializer.config.jsonapi_include_toplevel_member | ||
end | ||
|
||
def add!(document) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the side-effects 😿
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what side-effects?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
?
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do immediately.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
There was a problem hiding this comment.
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?
7b22480
to
43c56ac
Compare
serializable_hash_for_single_resource(options) | ||
end | ||
|
||
ApiObjects::JsonApi.include_member? && hash.merge!(ApiObjects::JsonApi.member) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
@bf4 Let's pair on this one when you have time and get it over with. |
👍 |
end | ||
|
||
def object | ||
object = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
change to @object ||=
@beauby squashed to one commit, set you as the 'committer' 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@beauby :)
Add top-level jsonapi member to JSON API adapter
Ref: http://jsonapi.org/format/#document-jsonapi-object
jsonapi_include_toplevel_object
: Whether to include a top level JSON API memberin the response document.
Default:
false
.jsonapi_include_toplevel_object
istrue
: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: