-
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
Nicer syntax for jsonapi include
#1131
Conversation
68ac59c
to
f4b9e6e
Compare
f4b9e6e
to
82d7fc4
Compare
ca0085f
to
4d2978e
Compare
Excited for this! Will this continue to work with the JSONAPI spec for an include string? It would still probably be good to be able to accept a url parameter for the include, and pass that directly to the serializer. |
@willcosgrove Yeah, I just brought back the string format as well, as I had not thought about that use case when I decided to get rid of it. |
include
include
included.inject({}) { |a, e| a.merge(include_option_to_hash(e)) } | ||
when String | ||
included.delete(' ').split(',').inject({}) do |hash, path| | ||
hash.deep_merge(path.split('.').reverse_each.inject({}) { |a, e| { e.to_sym => 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.
It allocates many extra hashes. Why not to use a deep_merge!
?
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.
Fixed. Thanks for spotting that!
Would be nice to have the commits with failing test squashed 😁 I'm triggering travis again. |
@@ -43,29 +43,29 @@ def render_resource_without_include | |||
|
|||
def render_resource_with_include | |||
setup_post | |||
render json: @post, include: 'author', adapter: :json_api | |||
render json: @post, include: [:author], adapter: :json_api |
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.
These are the only tests involving action_controller
+ include
would be nice to have some of them or new ones covering the string behavior in to make sure we con't break it in the future.
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 reverted some to the string syntax.
f4f74ac
to
366012c
Compare
All comments taken into account + squashed. |
collection.respond_to?(:total_pages) && | ||
collection.respond_to?(:size) | ||
end | ||
extend ActiveSupport::Autoload |
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.
indent!!!
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.
The indentation of the whole file was wrong.
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's supposed to be so that the diffs are easy to read.. e.g. so we can #1138
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.
Fair enough, will revert it.
Overall, looks good, should have docs added, if it's not too much |
a4a3ba2
to
0603208
Compare
when Symbol | ||
{ included => {} } | ||
when Hash | ||
included.each_with_object({}) { |(key, value), hash| hash[key] = include_args_to_hash(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.
@bf4: How about 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.
this aligns well with https://github.com/rails-api/active_model_serializers/pull/1127/files#diff-2e4bf15928c11c2f4eff9285340b602d
comments would be nice though.
Have you tried seeing what happens if you somehow pass an invalid string as an include option?
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 would pretty much do what it's supposed to. What do you have in mind?
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.
Possibly just adding tests for if someone specified 'authr' instead of 'author' stuff like that.
strings can be a little difficult to catch errors in sometimes, due to silent failing.
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.
Oh I see. Currently, the way it works, it should silently ignore any included relationship that was not defined on the serializer. Moreover, I believe there already are some tests on the adapter for trying to include a missing relationship.
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 think there should be a logger output for when that happens? maybe not in this PR, but in another?
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.
Yeah it would make sense. Also, there should be a logger output when attempting to serialize a related relationship for which there exists no serializer.
72460e7
to
802898c
Compare
@@ -30,6 +30,8 @@ resources in the `"included"` member when the resource names are included in the | |||
render @posts, include: 'authors,comments' | |||
``` | |||
|
|||
The format of the `include` option can be either a String composed of a comma-separated list of [http://jsonapi.org/format/#fetching-includes](relationship paths), an Array of Symbols and Hashes, or a mix of both. |
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 I think this link is wrong, isn't?
I believe the right way is something like that:
[relationship paths](http://jsonapi.org/format/#fetching-includes)
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.
Good catch, thanks @bacarini!
802898c
to
a07792b
Compare
a07792b
to
ce7a839
Compare
Extended format for JSONAPI `include` option
This PR
removeskeeps the old string format for includes (include: 'posts, posts.author, posts.comments, posts.comments.author, post.comments.likes, friends
) and introduces a new formatinsteadas well (include: [ :friends, posts: [:author, comments: [:author, :likes]]]
), as discussed in #1128.The "user-friendly" syntax is transformed into a big hash internally (like
{ friends: {}, posts: { author: {}, comments: { author: {}, likes: {} } } }
), which is much easier to work with. The helper method that does the translation could be extracted and put in some kind ofUtils
module (WIP by @NullVoxPopuli, #1127).The tests, which use a mix of the old string syntax and an hybrid "array of strings" syntax
obviously fail for noware now fixed.Notice that one test was actually changed, since the previous code allowed for including a resource nested 2 levels deep without including the resource in between, which breaks full linkage (see note http://jsonapi.org/format/#fetching-includes).