mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 15:23:06 +00:00
Implement included and id and type as per spec
This commit is contained in:
@@ -41,6 +41,7 @@ module ActiveModel
|
||||
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'post')
|
||||
expected = [{
|
||||
id: "42",
|
||||
type: "posts",
|
||||
title: 'New Post',
|
||||
body: 'Body',
|
||||
links: {
|
||||
@@ -49,12 +50,14 @@ module ActiveModel
|
||||
author: { linkage: { type: "authors", id: "1" } }
|
||||
}
|
||||
}]
|
||||
assert_equal expected, @adapter.serializable_hash[:linked][:posts]
|
||||
assert_equal expected, @adapter.serializable_hash[:included]
|
||||
end
|
||||
|
||||
def test_limiting_linked_post_fields
|
||||
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'post', fields: {post: [:title]})
|
||||
expected = [{
|
||||
id: "42",
|
||||
type: "posts",
|
||||
title: 'New Post',
|
||||
links: {
|
||||
comments: { linkage: [ { type: "comments", id: "1" } ] },
|
||||
@@ -62,7 +65,7 @@ module ActiveModel
|
||||
author: { linkage: { type: "authors", id: "1" } }
|
||||
}
|
||||
}]
|
||||
assert_equal expected, @adapter.serializable_hash[:linked][:posts]
|
||||
assert_equal expected, @adapter.serializable_hash[:included]
|
||||
end
|
||||
|
||||
def test_include_nil_author
|
||||
@@ -102,37 +105,39 @@ module ActiveModel
|
||||
def test_include_linked_resources_with_type_name
|
||||
serializer = BlogSerializer.new(@blog)
|
||||
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer, include: ['writer', 'articles'])
|
||||
linked = adapter.serializable_hash[:linked]
|
||||
expected = {
|
||||
authors: [{
|
||||
linked = adapter.serializable_hash[:included]
|
||||
expected = [
|
||||
{
|
||||
id: "1",
|
||||
type: "authors",
|
||||
name: "Steve K.",
|
||||
links: {
|
||||
posts: { linkage: [] },
|
||||
roles: { linkage: [] },
|
||||
bio: { linkage: nil }
|
||||
}
|
||||
}],
|
||||
posts: [{
|
||||
},{
|
||||
id: "42",
|
||||
type: "posts",
|
||||
title: "New Post",
|
||||
body: "Body",
|
||||
id: "42",
|
||||
links: {
|
||||
comments: { linkage: [ { type: "comments", id: "1" } ] },
|
||||
blog: { linkage: { type: "blogs", id: "999" } },
|
||||
author: { linkage: { type: "authors", id: "1" } }
|
||||
}
|
||||
}, {
|
||||
id: "43",
|
||||
type: "posts",
|
||||
title: "Hello!!",
|
||||
body: "Hello, world!!",
|
||||
id: "43",
|
||||
links: {
|
||||
comments: { linkage: [] },
|
||||
blog: { linkage: { type: "blogs", id: "999" } },
|
||||
author: { linkage: nil }
|
||||
}
|
||||
}]
|
||||
}
|
||||
}
|
||||
]
|
||||
assert_equal expected, linked
|
||||
end
|
||||
end
|
||||
|
||||
@@ -27,9 +27,10 @@ module ActiveModel
|
||||
def test_include_multiple_posts
|
||||
expected = [
|
||||
{
|
||||
id: "1",
|
||||
type: "posts",
|
||||
title: "Hello!!",
|
||||
body: "Hello, world!!",
|
||||
id: "1",
|
||||
links: {
|
||||
comments: { linkage: [] },
|
||||
blog: { linkage: { type: "blogs", id: "999" } },
|
||||
@@ -37,9 +38,10 @@ module ActiveModel
|
||||
}
|
||||
},
|
||||
{
|
||||
id: "2",
|
||||
type: "posts",
|
||||
title: "New Post",
|
||||
body: "Body",
|
||||
id: "2",
|
||||
links: {
|
||||
comments: { linkage: [] },
|
||||
blog: { linkage: { type: "blogs", id: "999" } },
|
||||
@@ -56,6 +58,8 @@ module ActiveModel
|
||||
|
||||
expected = [
|
||||
{
|
||||
id: "1",
|
||||
type: "posts",
|
||||
title: "Hello!!",
|
||||
links: {
|
||||
comments: { linkage: [] },
|
||||
@@ -64,6 +68,8 @@ module ActiveModel
|
||||
}
|
||||
},
|
||||
{
|
||||
id: "2",
|
||||
type: "posts",
|
||||
title: "New Post",
|
||||
links: {
|
||||
comments: { linkage: [] },
|
||||
|
||||
@@ -39,25 +39,33 @@ module ActiveModel
|
||||
assert_equal(expected, @adapter.serializable_hash[:data][:links][:comments])
|
||||
end
|
||||
|
||||
def test_includes_linked_comments
|
||||
def test_includes_linked_data
|
||||
# If CommentPreviewSerializer is applied correctly the body text will not be present in the output
|
||||
expected = [
|
||||
{
|
||||
id: '1',
|
||||
type: 'comments',
|
||||
links: {
|
||||
post: { linkage: { type: 'posts', id: @post.id.to_s } }
|
||||
}
|
||||
},
|
||||
{
|
||||
id: '2',
|
||||
type: 'comments',
|
||||
links: {
|
||||
post: { linkage: { type: 'posts', id: @post.id.to_s } }
|
||||
}
|
||||
},
|
||||
{
|
||||
id: @author.id.to_s,
|
||||
type: "authors",
|
||||
links: {
|
||||
posts: { linkage: [ {type: "posts", id: @post.id.to_s } ] }
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
assert_equal(expected,
|
||||
@adapter.serializable_hash[:linked][:comments])
|
||||
assert_equal(expected, @adapter.serializable_hash[:included])
|
||||
end
|
||||
|
||||
def test_includes_author_id
|
||||
@@ -68,17 +76,6 @@ module ActiveModel
|
||||
assert_equal(expected, @adapter.serializable_hash[:data][:links][:author])
|
||||
end
|
||||
|
||||
def test_includes_linked_authors
|
||||
expected = [{
|
||||
id: @author.id.to_s,
|
||||
links: {
|
||||
posts: { linkage: [ { type: "posts", id: @post.id.to_s } ] }
|
||||
}
|
||||
}]
|
||||
|
||||
assert_equal(expected, @adapter.serializable_hash[:linked][:authors])
|
||||
end
|
||||
|
||||
def test_explicit_serializer_with_null_resource
|
||||
@post.author = nil
|
||||
|
||||
|
||||
@@ -42,6 +42,7 @@ module ActiveModel
|
||||
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'comments')
|
||||
expected = [{
|
||||
id: "1",
|
||||
type: "comments",
|
||||
body: 'ZOMG A COMMENT',
|
||||
links: {
|
||||
post: { linkage: { type: "posts", id: "1" } },
|
||||
@@ -49,31 +50,34 @@ module ActiveModel
|
||||
}
|
||||
}, {
|
||||
id: "2",
|
||||
type: "comments",
|
||||
body: 'ZOMG ANOTHER COMMENT',
|
||||
links: {
|
||||
post: { linkage: { type: "posts", id: "1" } },
|
||||
author: { linkage: nil }
|
||||
}
|
||||
}]
|
||||
assert_equal expected, @adapter.serializable_hash[:linked][:comments]
|
||||
assert_equal expected, @adapter.serializable_hash[:included]
|
||||
end
|
||||
|
||||
def test_limit_fields_of_linked_comments
|
||||
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'comments', fields: {comment: [:id]})
|
||||
expected = [{
|
||||
id: "1",
|
||||
type: "comments",
|
||||
links: {
|
||||
post: { linkage: { type: "posts", id: "1" } },
|
||||
author: { linkage: nil }
|
||||
}
|
||||
}, {
|
||||
id: "2",
|
||||
type: "comments",
|
||||
links: {
|
||||
post: { linkage: { type: "posts", id: "1" } },
|
||||
author: { linkage: nil }
|
||||
}
|
||||
}]
|
||||
assert_equal expected, @adapter.serializable_hash[:linked][:comments]
|
||||
assert_equal expected, @adapter.serializable_hash[:included]
|
||||
end
|
||||
|
||||
def test_no_include_linked_if_comments_is_empty
|
||||
|
||||
@@ -41,6 +41,7 @@ module ActiveModel
|
||||
expected = [
|
||||
{
|
||||
id: "43",
|
||||
type: "bios",
|
||||
content:"AMS Contributor",
|
||||
links: {
|
||||
author: { linkage: { type: "authors", id: "1" } }
|
||||
@@ -48,7 +49,7 @@ module ActiveModel
|
||||
}
|
||||
]
|
||||
|
||||
assert_equal(expected, @adapter.serializable_hash[:linked][:bios])
|
||||
assert_equal(expected, @adapter.serializable_hash[:included])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -142,42 +142,41 @@ module ActiveModel
|
||||
include: 'author,author.posts'
|
||||
)
|
||||
|
||||
expected = {
|
||||
authors: [
|
||||
{
|
||||
id: "1",
|
||||
name: "Steve K.",
|
||||
links: {
|
||||
posts: { linkage: [ { type: "posts", id: "10"}, { type: "posts", id: "30" }] },
|
||||
roles: { linkage: [] },
|
||||
bio: { linkage: { type: "bios", id: "1" }}
|
||||
}
|
||||
expected = [
|
||||
{
|
||||
id: "1",
|
||||
type: "authors",
|
||||
name: "Steve K.",
|
||||
links: {
|
||||
posts: { linkage: [ { type: "posts", id: "10"}, { type: "posts", id: "30" }] },
|
||||
roles: { linkage: [] },
|
||||
bio: { linkage: { type: "bios", id: "1" }}
|
||||
}
|
||||
],
|
||||
posts: [
|
||||
{
|
||||
id: "10",
|
||||
title: "Hello!!",
|
||||
body: "Hello, world!!",
|
||||
links: {
|
||||
comments: { linkage: [ { type: "comments", id: "1"}, { type: "comments", id: "2" }] },
|
||||
blog: { linkage: { type: "blogs", id: "999" } },
|
||||
author: { linkage: { type: "authors", id: "1" } }
|
||||
}
|
||||
}, {
|
||||
id: "30",
|
||||
title: "Yet Another Post",
|
||||
body: "Body",
|
||||
links: {
|
||||
comments: { linkage: [] },
|
||||
blog: { linkage: { type: "blogs", id: "999" } },
|
||||
author: { linkage: { type: "authors", id: "1" } }
|
||||
}
|
||||
}, {
|
||||
id: "10",
|
||||
type: "posts",
|
||||
title: "Hello!!",
|
||||
body: "Hello, world!!",
|
||||
links: {
|
||||
comments: { linkage: [ { type: "comments", id: "1"}, { type: "comments", id: "2" }] },
|
||||
blog: { linkage: { type: "blogs", id: "999" } },
|
||||
author: { linkage: { type: "authors", id: "1" } }
|
||||
}
|
||||
]
|
||||
}
|
||||
assert_equal expected, adapter.serializable_hash[:linked]
|
||||
assert_equal expected, alt_adapter.serializable_hash[:linked]
|
||||
}, {
|
||||
id: "30",
|
||||
type: "posts",
|
||||
title: "Yet Another Post",
|
||||
body: "Body",
|
||||
links: {
|
||||
comments: { linkage: [] },
|
||||
blog: { linkage: { type: "blogs", id: "999" } },
|
||||
author: { linkage: { type: "authors", id: "1" } }
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
assert_equal expected, adapter.serializable_hash[:included]
|
||||
assert_equal expected, alt_adapter.serializable_hash[:included]
|
||||
end
|
||||
|
||||
def test_ignore_model_namespace_for_linked_resource_type
|
||||
|
||||
Reference in New Issue
Block a user