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

Nicer syntax for jsonapi include #1131

Merged
merged 1 commit into from
Sep 13, 2015

Conversation

beauby
Copy link
Contributor

@beauby beauby commented Sep 10, 2015

This PR removes keeps the old string format for includes (include: 'posts, posts.author, posts.comments, posts.comments.author, post.comments.likes, friends) and introduces a new format instead as 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 of Utils 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 now are 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).

@willcosgrove
Copy link

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.

@beauby
Copy link
Contributor Author

beauby commented Sep 10, 2015

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

@beauby beauby changed the title [WIP] Nicer syntax for jsonapi include Nicer syntax for jsonapi include Sep 10, 2015
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 } })
Copy link

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

Copy link
Contributor Author

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!

@joaomdmoura
Copy link
Member

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
Copy link
Member

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.

Copy link
Contributor Author

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.

@beauby beauby force-pushed the jsonapi-include-tree branch 2 times, most recently from f4f74ac to 366012c Compare September 11, 2015 17:39
@beauby
Copy link
Contributor Author

beauby commented Sep 11, 2015

All comments taken into account + squashed.

collection.respond_to?(:total_pages) &&
collection.respond_to?(:size)
end
extend ActiveSupport::Autoload
Copy link
Member

Choose a reason for hiding this comment

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

indent!!!

Copy link
Contributor Author

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.

Copy link
Member

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

Copy link
Contributor Author

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.

@bf4
Copy link
Member

bf4 commented Sep 11, 2015

Overall, looks good, should have docs added, if it's not too much

@beauby beauby force-pushed the jsonapi-include-tree branch 3 times, most recently from a4a3ba2 to 0603208 Compare September 11, 2015 18:41
when Symbol
{ included => {} }
when Hash
included.each_with_object({}) { |(key, value), hash| hash[key] = include_args_to_hash(value) }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bf4: How about this?

Copy link
Contributor

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?

Copy link
Contributor Author

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?

Copy link
Contributor

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.

Copy link
Contributor Author

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.

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 think there should be a logger output for when that happens? maybe not in this PR, but in another?

Copy link
Contributor Author

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.

@beauby beauby force-pushed the jsonapi-include-tree branch 3 times, most recently from 72460e7 to 802898c Compare September 13, 2015 20:33
@@ -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.
Copy link
Contributor

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)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch, thanks @bacarini!

@beauby beauby force-pushed the jsonapi-include-tree branch from 802898c to a07792b Compare September 13, 2015 20:42
@beauby beauby force-pushed the jsonapi-include-tree branch from a07792b to ce7a839 Compare September 13, 2015 20:46
beauby added a commit that referenced this pull request Sep 13, 2015
Extended format for JSONAPI `include` option
@beauby beauby merged commit 2789a57 into rails-api:master Sep 13, 2015
@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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants