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

Fix duplicate resources inside included in compound document. #1239

Merged
merged 2 commits into from
Oct 8, 2015

Conversation

beauby
Copy link
Contributor

@beauby beauby commented Oct 5, 2015

This fixes #1202.

@beauby
Copy link
Contributor Author

beauby commented Oct 5, 2015

@hut8 Could you test this patch?

@hut8
Copy link
Contributor

hut8 commented Oct 5, 2015

@beauby The test LGTM. It does appear to stop producing the duplicate I saw in the original issue. Thanks!

@NullVoxPopuli
Copy link
Contributor

Thanks for verifying @hut8.

@beauby this looks good to me.

hash[:included] |= result[:included]
end
hash[:included] ||= []
hash[:included] |= result[:included]
Copy link
Contributor

Choose a reason for hiding this comment

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

why bitwise or here?

Copy link
Contributor

Choose a reason for hiding this comment

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

[7] pry(main)> lol ||= []
[]
[8] pry(main)> lol |= [1,2,3]
[
    [0] 1,
    [1] 2,
    [2] 3
]
[9] pry(main)> lol
[
    [0] 1,
    [1] 2,
    [2] 3
]

It's fancy syntax for a union
Sauce: http://ruby-doc.org/core-2.1.2/Array.html#method-i-7C

Copy link
Contributor

Choose a reason for hiding this comment

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

woah. that is fancy. TIL

@beauby beauby force-pushed the fix-duplicate-jsonapi branch 2 times, most recently from c301577 to 16440f1 Compare October 5, 2015 18:27
@beauby
Copy link
Contributor Author

beauby commented Oct 5, 2015

Rebased.

@joaomdmoura joaomdmoura mentioned this pull request Oct 5, 2015
11 tasks
end

included.delete_if { |resource| hash[:data].include?(resource) }
Copy link
Member

Choose a reason for hiding this comment

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

I see we are deleting here the duplicated resource, but it seems hacky, it's not addressing the problem itself, just working around it, did you found the main issue causing this?

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 is a structural issue. When the primary resource is a single resource (serializable_hash_for_single_resource), we make sure we do not add to included any related resource that is the primary resource. However, when the primary resource is a collection (serializable_hash_for_collection), the adapter works by computing the hash for every resource independently, and then combining the hashes into a big one. This has the advantage of clear code, but the disadvantage that when serializing [a, b] where a includes b, we have no other choice than manually removing duplicates when aggregating.

Copy link
Member

Choose a reason for hiding this comment

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

got it 😉

Copy link
Member

Choose a reason for hiding this comment

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

deletes resource from included if is a root resource

this included stuff is getting messy. needs a refactor before it sends even more vines through the class

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 believe it is the best we can achieve without refactoring the adapter so that it does not call itself recursively for collections (which I'm in favor of, in a subsequent PR). The scope of this PR was to fix a behavior breaking JSON API spec, without modifying the adapter too much.

@joaomdmoura
Copy link
Member

@beauby just checked it, made few comments on it for you to check

@beauby
Copy link
Contributor Author

beauby commented Oct 5, 2015

@joaomdmoura Answered ;)

@joaomdmoura
Copy link
Member

LGTM 👍

@@ -1,11 +1,16 @@
require 'test_helper'

NestedPost = Class.new(Model)
class NestedPostSerializer < ActiveModel::Serializer
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd say because this model and serializer are created here, and specific to this test, they should be namespaced under the test. :-\

Copy link
Member

Choose a reason for hiding this comment

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

I think we agree we'll start doing that soon

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. Let's leave them there for now, and take care of it during the Great Test Refactor.

@bf4
Copy link
Member

bf4 commented Oct 8, 2015

I'm ok to merge, but we need to admit that this inclusion stuff has been a source of bugs and increasing complexity. IMHO, it deserves its own object

@beauby
Copy link
Contributor Author

beauby commented Oct 8, 2015

@bf4 We agree. I'm happy to work on a refactor once this is merged.

bf4 added a commit that referenced this pull request Oct 8, 2015
Fix duplicate resources inside included in compound document.
@bf4 bf4 merged commit 49eb531 into rails-api:master Oct 8, 2015
@bf4 bf4 deleted the fix-duplicate-jsonapi branch October 8, 2015 19:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Duplicate resource object in compound document
5 participants