-
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
Pass :include to Serializer #1555
Comments
so you don't even want the list of comment ids unless comments_included is true? |
Correct. The list of comment IDs is irrelevant to me, and not always feasible. Imagine a post that has 100,000 comments I'd rather not load (even just IDs), since I already have a |
+1 |
The adapter shouldn't be loading the association unless it's included. That would definitely be a bug. The adapter asks the serializer for the 'included' associations. The bug would be in how the serializer is answering that question. (So, the issue isn't that the serializer needs an |
Appears this is a duplicate of #1552 |
I'd say so. |
unclosing, cause the other issue is actually a bug report specific to belongs_to. oops. |
Yeah, sounds like same type of bug, probably same place in code On Wed, Mar 9, 2016 at 6:51 AM L. Preston Sego III notifications@github.com
|
Just some quick clarification:
This conflicts with your statement in #1325 (comment):
Per the ticket this was just (temporarily) consolidated with, one reason for the conflict may be the difference between One way to simplify this is to follow this decision process:
Separately, even if AMS didn't do this automatically under the hood, I'd be happy just exposing the necessary options so I can write a mixin to do it myself. Hence exposing In summary:
Does this make sense? |
Looks about right. An commuting now. Adapter json should include assoc, On Wed, Mar 9, 2016 at 8:46 AM Lee Richmond notifications@github.com
|
Should that also happen with JSON/Attributes serializers? Because I am working on a similar PR.. Personally I agree to that. |
If I don't specify any include in the serializer, JSON adapter will include '*'. Which means only first level includes. What should be the default here? Is this still correct (which I don't like personally, I think it should either be '**', meaning include everything recursively unless an include is specified or include nothing unless an include is specified). |
|
@NullVoxPopuli I disagree. I think it should be the default, and you should have a maximum nest level (which is configurable, but with default value = 3 which is equivelant with The default |
I guess decisions like this is why we are strongly encouraging people to switch to JSON api |
I don't get it, how JSON api would help on that ? |
it has strict defaults, and there is no debating over what should be done, so, with JSON API, if you want relationships, you MUST be explicit about it for each request. |
ok this is JSON API and I think it's a good thing it has strict defaults, it serves a purpose. Regarding JSON adapter, it serves another purpose to give you a flexibility to inherit it and create an adapter for another spec. So what is the purpose of allowing 1 level associations ? I think it's a poor decision. Why not 2 level and it's 1 level? The correct is either don't allow anything or allow everything (by default). |
Or add yet another configurable option, where people could say something like. AMS.json_adapter_include_default = '*.*.*' # for 3 levels |
Why don't you like the max nested level configuration ? I don't know, but I feel kinda weird being in ruby and parsing strings to figure out the configuration. |
Just because of potential for infinite loops -- that's all. My example actually could be passed directly to the include option in the serializer. @beauby and I already worked out the include string parsing :-) |
Joining in a bit late here, but the described behavior is not a bug (serializing a collection of related ids implies querying the corresponding has_many relationship), although we definitely need to provide an easy way to do "link only" relationships, as those have lots of real world use cases. |
Tried to get a PR together and had the following issues:
Agreed. However, the bug would be that AMS tries to serialize a collection of related IDs at all (when not given
render json: blog, include: {posts: :comments}` In this example the post comments should be included, the blog comments should not. In order to accomplish this we actually need the level of the include tree we're currently processing, not the raw
|
@richmolj I kinda disagree with
in the general case (example: you have a list of |
@beauby good use case! I agree this should be supported, makes good sense. Personal preference is populating |
The current implementation does support conditionally sideloading relationships based on the 'include' URL param. However, omitting the relationship still loads the relationship (to populate the type/id 'relationships' payload), somewhat defeating the purpose. Instead, this changes the flow to: 1. If the relationship is included, load it and include it the response. 2. If the relationship is not included but there is a JSONAPI link, include the link in the response but do not load the relationship or include data. 3. If the relationship is not in the URL param and there is no link, do not include this node in the 'relationships' response. The `current_include_tree` edits in json_api.rb are to pass the current nested includes. This is to support when multiple entities have the same relationship, e.g. `/blogs/?include=posts.tags,tags` should include both blog tags and post tags, but `/blogs/?include=posts.tags` should only include post tags. This API is opt-in to support users who always want to load `relationships` data. To opt-in: ```ruby class BlogSerializer < ActiveModel::Serializer associations_via_include_param(true) # opt-in to this pattern has_many :tags has_many :posts do link :self, '//example.com/blogs/relationships/posts' end end ``` JSONAPI include parameters (http://jsonapi.org/format/#fetching-includes). Fixes rails-api#1707 Fixes rails-api#1555
The current implementation does support conditionally sideloading relationships based on the 'include' URL param. However, omitting the relationship still loads the relationship (to populate the type/id 'relationships' payload), somewhat defeating the purpose. Instead, this changes the flow to: 1. If the relationship is included, load it and include it the response. 2. If the relationship is not included but there is a JSONAPI link, include the link in the response but do not load the relationship or include data. 3. If the relationship is not in the URL param and there is no link, do not include this node in the 'relationships' response. The `current_include_tree` edits in json_api.rb are to pass the current nested includes. This is to support when multiple entities have the same relationship, e.g. `/blogs/?include=posts.tags,tags` should include both blog tags and post tags, but `/blogs/?include=posts.tags` should only include post tags. This API is opt-in to support users who always want to load `relationships` data. To opt-in: ```ruby class BlogSerializer < ActiveModel::Serializer associations_via_include_param(true) # opt-in to this pattern has_many :tags has_many :posts do link :self, '//example.com/blogs/relationships/posts' end end ``` JSONAPI include parameters (http://jsonapi.org/format/#fetching-includes). Fixes rails-api#1707 Fixes rails-api#1555
The current implementation does support conditionally sideloading relationships based on the 'include' URL param. However, omitting the relationship still loads the relationship (to populate the type/id 'relationships' payload), somewhat defeating the purpose. Instead, this changes the flow to: 1. If the relationship is included, load it and include it the response. 2. If the relationship is not included but there is a JSONAPI link, include the link in the response but do not load the relationship or include data. 3. If the relationship is not in the URL param and there is no link, do not include this node in the 'relationships' response. The `current_include_tree` edits in json_api.rb are to pass the current nested includes. This is to support when multiple entities have the same relationship, e.g. `/blogs/?include=posts.tags,tags` should include both blog tags and post tags, but `/blogs/?include=posts.tags` should only include post tags. This API is opt-in to support users who always want to load `relationships` data. To opt-in: ```ruby class BlogSerializer < ActiveModel::Serializer associations_via_include_param(true) # opt-in to this pattern has_many :tags has_many :posts do link :self, '//example.com/blogs/relationships/posts' end end ``` JSONAPI include parameters (http://jsonapi.org/format/#fetching-includes). Fixes rails-api#1707 Fixes rails-api#1555
The current implementation does support conditionally sideloading relationships based on the 'include' URL param. However, omitting the relationship still loads the relationship (to populate the type/id 'relationships' payload), somewhat defeating the purpose. Instead, this changes the flow to: 1. If the relationship is included, load it and include it the response. 2. If the relationship is not included but there is a JSONAPI link, include the link in the response but do not load the relationship or include data. 3. If the relationship is not in the URL param and there is no link, do not include this node in the 'relationships' response. The `current_include_tree` edits in json_api.rb are to pass the current nested includes. This is to support when multiple entities have the same relationship, e.g. `/blogs/?include=posts.tags,tags` should include both blog tags and post tags, but `/blogs/?include=posts.tags` should only include post tags. This API is opt-in to support users who always want to load `relationships` data. To opt-in: ```ruby class BlogSerializer < ActiveModel::Serializer associations_via_include_param(true) # opt-in to this pattern has_many :tags has_many :posts do link :self, '//example.com/blogs/relationships/posts' end end ``` JSONAPI include parameters (http://jsonapi.org/format/#fetching-includes). Fixes rails-api#1707 Fixes rails-api#1555
The current implementation does support conditionally sideloading relationships based on the 'include' URL param. However, omitting the relationship still loads the relationship (to populate the type/id 'relationships' payload), somewhat defeating the purpose. Instead, this changes the flow to: 1. If the relationship is included, load it and include it the response. 2. If the relationship is not included but there is a JSONAPI link, include the link in the response but do not load the relationship or include data. 3. If the relationship is not in the URL param and there is no link, do not include this node in the 'relationships' response. The `current_include_tree` edits in json_api.rb are to pass the current nested includes. This is to support when multiple entities have the same relationship, e.g. `/blogs/?include=posts.tags,tags` should include both blog tags and post tags, but `/blogs/?include=posts.tags` should only include post tags. This API is opt-in to support users who always want to load `relationships` data. To opt-in: ```ruby class BlogSerializer < ActiveModel::Serializer associations_via_include_param(true) # opt-in to this pattern has_many :tags has_many :posts do link :self, '//example.com/blogs/relationships/posts' end end ``` JSONAPI include parameters (http://jsonapi.org/format/#fetching-includes). Fixes rails-api#1707 Fixes rails-api#1555
The current implementation does support conditionally sideloading relationships based on the 'include' URL param. However, omitting the relationship still loads the relationship (to populate the type/id 'relationships' payload), somewhat defeating the purpose. Instead, this changes the flow to: 1. If the relationship is included, load it and include it the response. 2. If the relationship is not included but there is a JSONAPI link, include the link in the response but do not load the relationship or include data. 3. If the relationship is not in the URL param and there is no link, do not include this node in the 'relationships' response. The `current_include_tree` edits in json_api.rb are to pass the current nested includes. This is to support when multiple entities have the same relationship, e.g. `/blogs/?include=posts.tags,tags` should include both blog tags and post tags, but `/blogs/?include=posts.tags` should only include post tags. This API is opt-in to support users who always want to load `relationships` data. To opt-in: ```ruby class BlogSerializer < ActiveModel::Serializer associations_via_include_param(true) # opt-in to this pattern has_many :tags has_many :posts do link :self, '//example.com/blogs/relationships/posts' end end ``` JSONAPI include parameters (http://jsonapi.org/format/#fetching-includes). Fixes rails-api#1707 Fixes rails-api#1555
The current implementation does support conditionally sideloading relationships based on the 'include' URL param. However, omitting the relationship still loads the relationship (to populate the type/id 'relationships' payload), somewhat defeating the purpose. Instead, this changes the flow to: 1. If the relationship is included, load it and include it the response. 2. If the relationship is not included but there is a JSONAPI link, include the link in the response but do not load the relationship or include data. 3. If the relationship is not in the URL param and there is no link, do not include this node in the 'relationships' response. The `current_include_tree` edits in json_api.rb are to pass the current nested includes. This is to support when multiple entities have the same relationship, e.g. `/blogs/?include=posts.tags,tags` should include both blog tags and post tags, but `/blogs/?include=posts.tags` should only include post tags. This API is opt-in to support users who always want to load `relationships` data. To opt-in: ```ruby class BlogSerializer < ActiveModel::Serializer associations_via_include_param(true) # opt-in to this pattern has_many :tags has_many :posts do link :self, '//example.com/blogs/relationships/posts' end end ``` JSONAPI include parameters (http://jsonapi.org/format/#fetching-includes). Fixes rails-api#1707 Fixes rails-api#1555
The current implementation does support conditionally sideloading relationships based on the 'include' URL param. However, omitting the relationship still loads the relationship (to populate the type/id 'relationships' payload), somewhat defeating the purpose. Instead, this changes the flow to: 1. If the relationship is included, load it and include it the response. 2. If the relationship is not included but there is a JSONAPI link, include the link in the response but do not load the relationship or include data. 3. If the relationship is not in the URL param and there is no link, do not include this node in the 'relationships' response. The `current_include_tree` edits in json_api.rb are to pass the current nested includes. This is to support when multiple entities have the same relationship, e.g. `/blogs/?include=posts.tags,tags` should include both blog tags and post tags, but `/blogs/?include=posts.tags` should only include post tags. This API is opt-in to support users who always want to load `relationships` data. To opt-in: ```ruby class BlogSerializer < ActiveModel::Serializer associations_via_include_param(true) # opt-in to this pattern has_many :tags has_many :posts do link :self, '//example.com/blogs/relationships/posts' end end ``` JSONAPI include parameters (http://jsonapi.org/format/#fetching-includes). Fixes rails-api#1707 Fixes rails-api#1555
The current implementation does support conditionally sideloading relationships based on the 'include' URL param. However, omitting the relationship still loads the relationship (to populate the type/id 'relationships' payload), somewhat defeating the purpose. Instead, this changes the flow to: 1. If the relationship is included, load it and include it the response. 2. If the relationship is not included but there is a JSONAPI link, include the link in the response but do not load the relationship or include data. 3. If the relationship is not in the URL param and there is no link, do not include this node in the 'relationships' response. The `current_include_tree` edits in json_api.rb are to pass the current nested includes. This is to support when multiple entities have the same relationship, e.g. `/blogs/?include=posts.tags,tags` should include both blog tags and post tags, but `/blogs/?include=posts.tags` should only include post tags. This API is opt-in to support users who always want to load `relationships` data. To opt-in: ```ruby class BlogSerializer < ActiveModel::Serializer associations_via_include_param(true) # opt-in to this pattern has_many :tags has_many :posts do link :self, '//example.com/blogs/relationships/posts' end end ``` JSONAPI include parameters (http://jsonapi.org/format/#fetching-includes). Fixes rails-api#1707 Fixes rails-api#1555
The current implementation does support conditionally sideloading relationships based on the 'include' URL param. However, omitting the relationship still loads the relationship (to populate the type/id 'relationships' payload), somewhat defeating the purpose. Instead, this changes the flow to: 1. If the relationship is included, load it and include it the response. 2. If the relationship is not included but there is a JSONAPI link, include the link in the response but do not load the relationship or include data. 3. If the relationship is not in the URL param and there is no link, do not include this node in the 'relationships' response. The `current_include_tree` edits in json_api.rb are to pass the current nested includes. This is to support when multiple entities have the same relationship, e.g. `/blogs/?include=posts.tags,tags` should include both blog tags and post tags, but `/blogs/?include=posts.tags` should only include post tags. This API is opt-in to support users who always want to load `relationships` data. To opt-in: ```ruby class BlogSerializer < ActiveModel::Serializer associations_via_include_param(true) # opt-in to this pattern has_many :tags has_many :posts do link :self, '//example.com/blogs/relationships/posts' end end ``` JSONAPI include parameters (http://jsonapi.org/format/#fetching-includes). Fixes rails-api#1707 Fixes rails-api#1555
The current implementation does support conditionally sideloading relationships based on the 'include' URL param. However, omitting the relationship still loads the relationship (to populate the type/id 'relationships' payload), somewhat defeating the purpose. Instead, this changes the flow to: 1. If the relationship is included, load it and include it the response. 2. If the relationship is not included but there is a JSONAPI link, include the link in the response but do not load the relationship or include data. 3. If the relationship is not in the URL param and there is no link, do not include this node in the 'relationships' response. The `current_include_tree` edits in json_api.rb are to pass the current nested includes. This is to support when multiple entities have the same relationship, e.g. `/blogs/?include=posts.tags,tags` should include both blog tags and post tags, but `/blogs/?include=posts.tags` should only include post tags. This API is opt-in to support users who always want to load `relationships` data. To opt-in: ```ruby class BlogSerializer < ActiveModel::Serializer associations_via_include_param(true) # opt-in to this pattern has_many :tags has_many :posts do link :self, '//example.com/blogs/relationships/posts' end end ``` JSONAPI include parameters (http://jsonapi.org/format/#fetching-includes). Fixes rails-api#1707 Fixes rails-api#1555
The current implementation does support conditionally sideloading relationships based on the 'include' URL param. However, omitting the relationship still loads the relationship (to populate the type/id 'relationships' payload), somewhat defeating the purpose. Instead, this changes the flow to: 1. If the relationship is included, load it and include it the response. 2. If the relationship is not included but there is a JSONAPI link, include the link in the response but do not load the relationship or include data. 3. If the relationship is not in the URL param and there is no link, do not include this node in the 'relationships' response. The `current_include_tree` edits in json_api.rb are to pass the current nested includes. This is to support when multiple entities have the same relationship, e.g. `/blogs/?include=posts.tags,tags` should include both blog tags and post tags, but `/blogs/?include=posts.tags` should only include post tags. This API is opt-in to support users who always want to load `relationships` data. To opt-in: ```ruby class BlogSerializer < ActiveModel::Serializer associations_via_include_param(true) # opt-in to this pattern has_many :tags has_many :posts do link :self, '//example.com/blogs/relationships/posts' end end ``` JSONAPI include parameters (http://jsonapi.org/format/#fetching-includes). Fixes rails-api#1707 Fixes rails-api#1555
For JSONAPI, `include_data` currently means, "should we populate the 'data'" key for this relationship. Current options are true/false. This adds the `:via_include_parameter` option. This means "only populate the 'data' key when we are sideloading this relationship." This is because 'data' is often only relevant to sideloading, and causes a database hit. Addresses rails-api#1555
For JSONAPI, `include_data` currently means, "should we populate the 'data'" key for this relationship. Current options are true/false. This adds the `:via_include_parameter` option. This means "only populate the 'data' key when we are sideloading this relationship." This is because 'data' is often only relevant to sideloading, and causes a database hit. Addresses rails-api#1555
For JSONAPI, `include_data` currently means, "should we populate the 'data'" key for this relationship. Current options are true/false. This adds the `:via_include_parameter` option. This means "only populate the 'data' key when we are sideloading this relationship." This is because 'data' is often only relevant to sideloading, and causes a database hit. Addresses rails-api#1555
For JSONAPI, `include_data` currently means, "should we populate the 'data'" key for this relationship. Current options are true/false. This adds the `:via_include_parameter` option. This means "only populate the 'data' key when we are sideloading this relationship." This is because 'data' is often only relevant to sideloading, and causes a database hit. Addresses rails-api#1555
For JSONAPI, `include_data` currently means, "should we populate the 'data'" key for this relationship. Current options are true/false. This adds the `:if_sideloaded` option. This means "only populate the 'data' key when we are sideloading this relationship." This is because 'data' is often only relevant to sideloading, and causes a database hit. Addresses rails-api#1555
For JSONAPI, `include_data` currently means, "should we populate the 'data'" key for this relationship. Current options are true/false. This adds the `:if_sideloaded` option. This means "only populate the 'data' key when we are sideloading this relationship." This is because 'data' is often only relevant to sideloading, and causes a database hit. Addresses rails-api#1555
For JSONAPI, `include_data` currently means, "should we populate the 'data'" key for this relationship. Current options are true/false. This adds the `:if_sideloaded` option. This means "only populate the 'data' key when we are sideloading this relationship." This is because 'data' is often only relevant to sideloading, and causes a database hit. Addresses rails-api#1555
For JSONAPI, `include_data` currently means, "should we populate the 'data'" key for this relationship. Current options are true/false. This adds the `:if_sideloaded` option. This means "only populate the 'data' key when we are sideloading this relationship." This is because 'data' is often only relevant to sideloading, and causes a database hit. Addresses rails-api#1555
For JSONAPI, `include_data` currently means, "should we populate the 'data'" key for this relationship. Current options are true/false. This adds the `:if_sideloaded` option. This means "only populate the 'data' key when we are sideloading this relationship." This is because 'data' is often only relevant to sideloading, and causes a database hit. Addresses rails-api#1555
For JSONAPI, `include_data` currently means, "should we populate the 'data'" key for this relationship. Current options are true/false. This adds the `:if_sideloaded` option. This means "only populate the 'data' key when we are sideloading this relationship." This is because 'data' is often only relevant to sideloading, and causes a database hit. Addresses #1555
Using JSONAPI, I never want to load
relationships
unless they are alsoinclude
'd. By default:Does nothing for me but cause an unnecessary DB hit. Instead, by desired behavior would be:
Unfortunately the
include
option is passed to the adapter, but unavailable on the serializer.Can we add
include
to the options passed to the serializer?The text was updated successfully, but these errors were encountered: