-
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
Fixes nested has_many links in JSONAPI #730
Merged
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,24 +6,32 @@ class Adapter | |
class JsonApi | ||
class LinkedTest < Minitest::Test | ||
def setup | ||
@author = Author.new(id: 1, name: 'Steve K.') | ||
@bio = Bio.new(id: 1, content: 'AMS Contributor') | ||
@author1 = Author.new(id: 1, name: 'Steve K.') | ||
@author2 = Author.new(id: 2, name: 'Tenderlove') | ||
@bio1 = Bio.new(id: 1, content: 'AMS Contributor') | ||
@bio2 = Bio.new(id: 2, content: 'Rails Contributor') | ||
@first_post = Post.new(id: 1, title: 'Hello!!', body: 'Hello, world!!') | ||
@second_post = Post.new(id: 2, title: 'New Post', body: 'Body') | ||
@third_post = Post.new(id: 3, title: 'Yet Another Post', body: 'Body') | ||
@first_post.comments = [] | ||
@second_post.comments = [] | ||
@first_post.author = @author | ||
@second_post.author = @author | ||
@author.posts = [@first_post, @second_post] | ||
@author.bio = @bio | ||
@author.roles = [] | ||
@bio.author = @author | ||
@first_post.author = @author1 | ||
@second_post.author = @author2 | ||
@third_post.author = @author1 | ||
@author1.posts = [@first_post, @third_post] | ||
@author1.bio = @bio1 | ||
@author1.roles = [] | ||
@author2.posts = [@second_post] | ||
@author2.bio = @bio2 | ||
@author2.roles = [] | ||
@bio1.author = @author1 | ||
@bio2.author = @author2 | ||
end | ||
|
||
def test_include_multiple_posts_and_linked | ||
@serializer = ArraySerializer.new([@first_post, @second_post]) | ||
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'author,author.bio,comments') | ||
end | ||
|
||
def test_include_multiple_posts_and_linked | ||
@first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT') | ||
@second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT') | ||
@first_post.comments = [@first_comment, @second_comment] | ||
|
@@ -33,7 +41,7 @@ def test_include_multiple_posts_and_linked | |
@second_comment.author = nil | ||
assert_equal([ | ||
{ title: "Hello!!", body: "Hello, world!!", id: "1", links: { comments: ['1', '2'], author: "1" } }, | ||
{ title: "New Post", body: "Body", id: "2", links: { comments: [], :author => "1" } } | ||
{ title: "New Post", body: "Body", id: "2", links: { comments: [], :author => "2" } } | ||
], @adapter.serializable_hash[:posts]) | ||
|
||
|
||
|
@@ -57,17 +65,75 @@ def test_include_multiple_posts_and_linked | |
id: "1", | ||
name: "Steve K.", | ||
links: { | ||
posts: ["1", "2"], | ||
posts: ["1"], | ||
roles: [], | ||
bio: "1" | ||
} | ||
}, { | ||
id: "2", | ||
name: "Tenderlove", | ||
links: { | ||
posts: ["2"], | ||
roles: [], | ||
bio: "2" | ||
} | ||
}], | ||
bios: [{ | ||
id: "1", | ||
content: "AMS Contributor", | ||
links: { | ||
author: "1" | ||
} | ||
}, { | ||
id: "2", | ||
content: "Rails Contributor", | ||
links: { | ||
author: "2" | ||
} | ||
}] | ||
} | ||
assert_equal expected, @adapter.serializable_hash[:linked] | ||
end | ||
|
||
def test_include_multiple_posts_and_linked | ||
@serializer = BioSerializer.new(@bio1) | ||
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'author,author.posts') | ||
|
||
@first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT') | ||
@second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT') | ||
@first_post.comments = [@first_comment, @second_comment] | ||
@third_post.comments = [] | ||
@first_comment.post = @first_post | ||
@first_comment.author = nil | ||
@second_comment.post = @first_post | ||
@second_comment.author = nil | ||
|
||
expected = { | ||
authors: [{ | ||
id: "1", | ||
name: "Steve K.", | ||
links: { | ||
posts: ["1", "3"], | ||
roles: [], | ||
bio: "1" | ||
} | ||
}], | ||
posts: [{ | ||
title: "Hello!!", | ||
body: "Hello, world!!", | ||
id: "1", | ||
links: { | ||
comments: ["1", "2"], | ||
author: "1" | ||
} | ||
}, { | ||
title: "Yet Another Post", | ||
body: "Body", | ||
id: "3", | ||
links: { | ||
comments: [], | ||
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. The bug was here. Before, |
||
author: "1" | ||
} | ||
}] | ||
} | ||
assert_equal expected, @adapter.serializable_hash[:linked] | ||
|
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.
Is 'unless ...' needed here? If serializers is already an Array, Array(serializers) would just retunr serilizers.
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, It can be
Serializer
orArraySerializer
. If it'sSerializer
, I convert to[Serializer]
so I can treat them all equally, without the need for duplicating the logic, eg.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 see, Array(array_serializer) would be wrong.