-
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 include_data :via_include_parameter #1797
Closed
richmolj
wants to merge
11
commits into
rails-api:master
from
richmolj:include_data_via_include_param
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
bb797b8
Add include_data :if_sideloaded
81983df
Add helpful testing gems
6c59dac
Ensure generator picks up ApplicationSerializer
fed2557
Fix typo (#1916)
amiedes f3fd3d8
Disable pagination links via config
ee91d72
Add docs for links (#1909)
vasilakisfil b0e8d1c
Add info on setting polymorphic serializers (#1906)
Rio517 9e89cca
Update ember-and-json-api.md (#1894)
Ikariusrb b823499
Make railties an optional dependency
833f9e8
Add chengelog entry about the issue
fb288d5
add bm_adapter (#1914)
NullVoxPopuli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
[Back to Guides](../README.md) | ||
|
||
# How to add relationship links | ||
|
||
ActiveModelSerializers offers you many ways to add links in your JSON, depending on your needs. | ||
The most common use case for links is supporting nested resources. | ||
|
||
The following examples are without included relationship data (`include` param is empty), | ||
specifically the following Rails controller was used for these examples: | ||
|
||
```ruby | ||
class Api::V1::UsersController < ApplicationController | ||
def show | ||
render jsonapi: User.find(params[:id]), | ||
serializer: Api::V1::UserSerializer, | ||
include: [] | ||
end | ||
``` | ||
|
||
Bear in mind though that ActiveModelSerializers are [framework-agnostic](outside_controller_use.md), Rails is just a common example here. | ||
|
||
### Links as an attribute of a resource | ||
**This is applicable to JSONAPI, JSON and Attributes adapters** | ||
|
||
You can define an attribute in the resource, named `links`. | ||
|
||
```ruby | ||
class Api::V1::UserSerializer < ActiveModel::Serializer | ||
attributes :id, :name, :links | ||
|
||
def links | ||
{ | ||
self: api_v1_user_path(object.id), | ||
microposts: api_v1_microposts_path(user_id: object.id) | ||
} | ||
end | ||
end | ||
``` | ||
|
||
This will resilt in (example is in jsonapi adapter): | ||
```json | ||
{ | ||
"data": { | ||
"id": "1", | ||
"type": "users", | ||
"attributes": { | ||
"name": "Example User", | ||
"links": { | ||
"self": "/api/v1/users/1", | ||
"microposts": "/api/v1/microposts?user_id=1" | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
|
||
### Links as a property of the resource definiton | ||
**This is only applicable to JSONAPI adapter** | ||
|
||
You can use the `links` class method to define the links you need in the resource's primary data. | ||
|
||
```ruby | ||
class Api::V1::UserSerializer < ActiveModel::Serializer | ||
attributes :id, :name | ||
|
||
link(:self) { api_v1_user_path(object.id) } | ||
link(:microposts) { api_v1_microposts_path(user_id: object.id) } | ||
end | ||
``` | ||
|
||
This will resilt in (example is in jsonapi adapter): | ||
```json | ||
{ | ||
"data": { | ||
"id": "1", | ||
"type": "users", | ||
"attributes": { | ||
"name": "Example User" | ||
}, | ||
"links": { | ||
"self": "/api/v1/users/1", | ||
"microposts": "/api/v1/microposts?user_id=1" | ||
} | ||
} | ||
} | ||
``` | ||
|
||
### Links that follow the JSONAPI spec | ||
**This is only applicable to JSONAPI adapter** | ||
|
||
If you have a JSONAPI-strict client that you are working with (like `ember-data`) | ||
you need to construct the links inside the relationships. Also the link to fetch the | ||
relationship data must be under the `related` attribute, whereas to manipulate the | ||
relationship (in case of many-to-many relationship) must be under the `self` attribute. | ||
|
||
You can find more info in the [spec](http://jsonapi.org/format/#document-resource-object-relationships). | ||
|
||
Here is how you can do this: | ||
|
||
```ruby | ||
class Api::V1::UserSerializer < ActiveModel::Serializer | ||
attributes :id, :name | ||
|
||
has_many :microposts, serializer: Api::V1::MicropostSerializer do | ||
link(:related) { api_v1_microposts_path(user_id: object.id) } | ||
end | ||
|
||
#this is needed to avoid n+1, gem core devs are working to remove this necessity | ||
#more on: https://github.com/rails-api/active_model_serializers/issues/1325 | ||
def microposts | ||
object.microposts.loaded ? object.microposts : object.microposts.none | ||
end | ||
end | ||
``` | ||
|
||
This will result in: | ||
|
||
```json | ||
{ | ||
"data": { | ||
"id": "1", | ||
"type": "users", | ||
"attributes": { | ||
"name": "Example User" | ||
}, | ||
"relationships": { | ||
"microposts": { | ||
"data": [], | ||
"links": { | ||
"related": "/api/v1/microposts?user_id=1" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,13 +22,15 @@ def config.array_serializer | |
config.default_includes = '*' | ||
config.adapter = :attributes | ||
config.key_transform = nil | ||
config.jsonapi_pagination_links_enabled = true | ||
config.jsonapi_resource_type = :plural | ||
config.jsonapi_namespace_separator = '-'.freeze | ||
config.jsonapi_version = '1.0' | ||
config.jsonapi_toplevel_meta = {} | ||
# Make JSON API top-level jsonapi member opt-in | ||
# ref: http://jsonapi.org/format/#document-top-level | ||
config.jsonapi_include_toplevel_object = false | ||
config.include_data_default = true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. jsonapi adapter should default to false, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. current default is true. per the PR description, I vote we change that default but up to you guys |
||
|
||
config.schema_path = 'test/support/schemas' | ||
end | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
in this context, when would
include_directive.key?(key)
not be equal toinclude_slice.key?(key)
?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 is one of the gross aspects of the current adapter but
That's opposed to
include_slice
, which is always the slice of the real include directive passed torender
.In other words, the current code has two 'include directives'. One real one that is what you think it is, and a separate one that is derived from a relationship's
fields
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.
So in this case, for instance, let's say your include directive was:
When you are iterating the relations and you hit the
comments
portion, theinclude_directive
(the current code) is actually '*', assuming no sparse fieldsets requested. But theinclude_slice
would be {}.