-
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
Refactor JsonApi adapter in order to avoid recomputation of hashes fo… #1300
Conversation
e2de359
to
6f54bb4
Compare
Benchmark |
@bf4 Would love to provide one, do not have time to build the required infrastructure though. I think @joaomdmoura started to work on something though. Moreover, I think this PR is beneficial for code clarity and simplicity as well (code is now shorter and more streamlined). |
end | ||
end |
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 block could also be rewritten in the following shorter form:
primary_data, included = resources.partition { |_, r| r[:is_primary] }.map { |s| s.map { |_, r| r.except(:is_primary) } }
which I am sure @bf4 would love haha
6facf88
to
97b7f7e
Compare
@@ -44,21 +44,36 @@ def object | |||
def initialize(serializer, options = {}) | |||
super | |||
@include_tree = IncludeTree.from_include_args(options[:include]) | |||
@fieldset = options[:fieldset] || ActiveModel::Serializer::Fieldset.new(options.delete(:fields)) | |||
@fieldset = options[:fieldset] || ActiveModel::Serializer::Fieldset.new(options[: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.
Do we officially support the fieldset
option? (that is, providing an instance of Fieldset
, rather than a Hash of Arrays of Symbols)
It seems it was introduced only to handle the recursive call to the adapter for collections of resources (here).
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.
cc @bf4
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.
offhand I don't know. It's not part of the codebase I've looked much
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.
Then I'm up to remove it in a subsequent PR, as keeping it around would force us to keep the FieldSet
interface constant, which we may not necessarily want.
3fdc17e
to
67df2ff
Compare
|
||
attr_reader :fieldset | ||
# Build the requested resource objects. | ||
# @return [Array] [primary, included] Pair of arrays containing primary and 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.
perfect!
I'm unsure why the travis run is being reported as a failure. all green here: https://travis-ci.org/rails-api/active_model_serializers/builds/87493621 I'm ok to merge this. Any objections? |
80ef70d
to
a41acec
Compare
@@ -39,26 +39,93 @@ def object | |||
object | |||
end | |||
end | |||
|
|||
ResourceIdentifier = Struct.new(:id, :type) do |
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.
Can this be in a different file?
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.
Sure it can, but I wanted to see whether we liked it first.
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.
cool.
so what is a ResourceIdentifier
what does it represent?
(I think your answer to this should be coupled with inline comments as class header comments)
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 representes a resource identifier object. But yeah, agreed, should be in the class header.
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.
cool. this link is helpful (esp since I'm new to json api)
needs rebase, and then I'll merge |
6b649b4
to
602266e
Compare
Painful rebase over. |
looks good. could you squash? :-) |
602266e
to
c1a94d0
Compare
@bf4 Should we put |
@beauby I think it makes sense to have everything that makes up an api response document under its own namespace. ApiObjects is kind of ambiguous, though (unsure, by looking at just the ApiObjects namespace, if we are even on the server side) What are your thoughts on |
I think it's a good idea to group them together And to make clear they corresponds to object definitions inthe jsonschema Beyond that, i'm less picky. ApiObjects describes what the are is the only argument for it B mobile phone
|
630d71f
to
e417141
Compare
e417141
to
0740d45
Compare
Done. |
|
||
ApiObjects::JsonApi.add!(hash) | ||
ApiObjects::JsonApiObject.add!(hash) |
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, I suppose that even under the ApiObjects namespace JsonApi doesn't signal it's a json object strongly enough :)
I've only skimmed the implementation details but I really like the big picture of it! Dunno if it's worth referencing any details in https://github.com/rails-api/active_model_serializers/pull/1301/files but the PR is relevant |
Closing in favor of #1447. |
…r included resources.
Ref: #1239
Plus make the method names closer to the JSON API terminology.
Main changes:
attributes_for
method (for symmetry withrelationships_for
)relationship_value_for
intolinkage_for
primary
orincluded
resource, so that no hash is ever computed twice, and there is not need for theserializable_hash_for_single_resource
/serializable_hash_for_collection
distinction. The adapter no longer calls itself recursively.process_resource
which does the job ofadd_included_resources_for
,included_for
,primary_data_for
,serializable_hash_for_single_resource
,serializable_hash_for_collection
(and removed those)