-
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
Add include_data :via_include_parameter #1797
Conversation
@@ -235,17 +235,17 @@ def resource_objects_for(serializers) | |||
@primary = [] | |||
@included = [] | |||
@resource_identifiers = Set.new | |||
serializers.each { |serializer| process_resource(serializer, true) } | |||
serializers.each { |serializer| process_resource(serializer, true, @include_directive) } |
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.
here it begins
I feel like we shouldn't need to introduce the include_slice directive (i.e have two include directives) for this to work. Otherwise, the public interface change is good. |
) | ||
end | ||
|
||
def test_relationship_not_loaded_when_not_included |
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 think this test is the central feature in this PR, at least for the JSONAPI adapter
I feel you, but not sure how to avoid it. If you have an |
It should be modifying the include and fields params as each serializer calls the next, no? B mobile phone
|
@bf4 This may be a matter of phrasing, but to be clear - since I feel like the current PR is already doing what you are suggesting, so let me try to rephrase - I think the confusion is with the current codebase, not this PR.
IOW, you could actually refer to it as Hope this helps - as always, your time and effort here are much appreciated. |
Some thoughts: I don't think What if the API was this: # current behavior
has_many :comments
has_many :comments do
# must explicitly be included
# or maybe something related to the resource identifier object?
# http://jsonapi.org/format/#document-resource-identifier-objects
skip_unless_included
end I feel like I don't like the additional |
Hmm, fields_directive... B mobile phone
|
at least initially, we could just have fields span attributes and relationships, flatly. No need for a fields directive, I don't think. at least.. not yet. |
@NullVoxPopuli correct me if I'm wrong, but
👍 to
I think that is something everyone, including me, agrees on 😂 My thing is: what's the alternative? I can't see how you avoid it without fundamentally changing how |
I think we're on the same page here.
how do you mean?
ah, I see. that is obnoxious then. |
|
i.e. # https://github.com/rails-api/active_model_serializers/blob/master/lib/active_model/serializer.rb
def serializable_hash(adapter_options = nil, options = {}, adapter_instance = self.class.serialization_adapter_instance)
resource = fetch_attributes(options[:fields], cached_attributes, adapter_instance)
# https://github.com/rails-api/active_model_serializers/blob/master/lib/active_model/serializer/attributes.rb
def attributes(requested_attrs = nil, reload = false)
next if attr.excluded?(self)
next unless requested_attrs.nil? || requested_attrs.include?(key) means that the JSON API adapter should pass in |
@NullVoxPopuli regarding |
@bf4, do you mean we shouldn't include the id/type for relationships (by default) (no options passed)
@richmolj I actually didn't know that was a thing. ha |
@bf4 while I agree One - I think the payload output would be something like:
Which means the resource identifiers would be included, a DB hit is still happening, and we haven't solved the issue. That said, even if we got the payload to look like this:
It's still a no-go. This is telling clients "this object does not have an author", versus "this object may or may not have an author, we're just not including author information in the response". |
I need this so bad, is there anything I can do to help get something to solve this problem released? /edit Great work @richmolj 👍 |
0ab4c89
to
49a87bb
Compare
49a87bb
to
474322b
Compare
25d2ca0
to
5023df6
Compare
@bf4 - per our discussion yesterday:
Though we both agree this should be the default, I didn't change the current default out of backwards compatibility concerns. If you're OK with it I'm OK with it, though. |
@@ -29,6 +29,7 @@ def config.array_serializer | |||
# 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 comment
The 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 comment
The 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
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
5023df6
to
bb797b8
Compare
Is there any concern for ensuring that the relationship object contains at least of of Looking forward to this feature BTW. I just implemented my own monkey patch to do exactly this. This solution is better. |
@kevintyll - definitely agree. If the relation is not influded you should see an empty meta key, eg 'comments: { meta: {} }'. I'm on mobile right now but I should add a test asserting that is the case. |
ApplicationSerializer could exist, but not be loaded. So, we check existence by looking at the filesystem instead of defined?. Fixes rails-api#1890
Add docs for links Add docs for links Add docs for links Add docs for links Add docs for links Add controller info Grammar fixing Improve docs some small wording changes Add pr to changelog
While this same document provides details on how to override the serializer_for, not all users will realize this could be used to set the sterilizers for polymorphic relationships. This change just adds a link to that documentation and makes that point obvious.
* Update ember-and-json-api.md Removed ember-data adapter change to support include directives, as it's now built-in. Updated the documentation for how to add include directives to ember store queries, and added documentation covering how to tell Rails that we are consuming and generating jsonapi data * Fix up format for parameter restrictions * Update ember-and-json-api.md Updates to address comments; explain why Rails should know what format we are consuming/generating, reword introduction to include: examples, and fix render statement to specify jsonapi instead of json.
@richmolj issue title /description should also be updated, I think, no? |
@bf4 agreed, will close this and submit a new one tonight. |
Moved to #1931 |
@NullVoxPopuli @bf4, taking a final stab at simplifying the code from #1720. I created a separate PR since the original solves more issues and I didn't want to blow away the tests if you wanted to use them. Feel free to just close this PR if you still want to go in another direction, don't want this to seem pushy 😄 I believe this is the simplest possible solution, so maybe helpful to understand the problem if nothing else.
This now adds only one new variable name,
include_slice
. This is the slice of the include directive hash being processed by the current serializer. All the changes tolib/active_model/serializer/associations.rb
andlib/active_model_serializers/adapter/json_api.rb
are just passing that slice around so we can access it when we finally decide whether or not to have thedata
key in the response. The changes tolib/active_model/serializer/reflection.rb
access that variable to make the decision.Why do we need the current slice instead of the whole directive? Because multiple entities can 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 tagsUsers now opt-in to this pattern using the current DSL. The current DSL conditionally includes the
data
key in the response viainclude_data true/false
:We now add another option
:via_include_parameter
. This means we only include thedata
key when the relationship is being sideloaded: