mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 22:36:50 +00:00
Merge pull request #925 from benedikt/json-api
Updates JSON API Adapter to generate RC4 schema
This commit is contained in:
commit
16f75126a8
@ -8,7 +8,7 @@ AMS does this through two components: **serializers** and **adapters**.
|
|||||||
Serializers describe _which_ attributes and relationships should be serialized.
|
Serializers describe _which_ attributes and relationships should be serialized.
|
||||||
Adapters describe _how_ attributes and relationships should be serialized.
|
Adapters describe _how_ attributes and relationships should be serialized.
|
||||||
|
|
||||||
By default AMS will use the JsonApi Adapter that follows RC3 of the format specified in [jsonapi.org/format](http://jsonapi.org/format).
|
By default AMS will use the JsonApi Adapter that follows RC4 of the format specified in [jsonapi.org/format](http://jsonapi.org/format).
|
||||||
Check how to change the adapter in the sections bellow.
|
Check how to change the adapter in the sections bellow.
|
||||||
|
|
||||||
# RELEASE CANDIDATE, PLEASE READ
|
# RELEASE CANDIDATE, PLEASE READ
|
||||||
@ -178,7 +178,7 @@ end
|
|||||||
|
|
||||||
#### JSONAPI
|
#### JSONAPI
|
||||||
|
|
||||||
This adapter follows RC3 of the format specified in
|
This adapter follows RC4 of the format specified in
|
||||||
[jsonapi.org/format](http://jsonapi.org/format). It will include the associated
|
[jsonapi.org/format](http://jsonapi.org/format). It will include the associated
|
||||||
resources in the `"included"` member when the resource names are included in the
|
resources in the `"included"` member when the resource names are included in the
|
||||||
`include` option.
|
`include` option.
|
||||||
|
|||||||
@ -29,7 +29,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
else
|
else
|
||||||
@hash[:data] = attributes_for_serializer(serializer, @options)
|
@hash[:data] = attributes_for_serializer(serializer, @options)
|
||||||
add_resource_links(@hash[:data], serializer)
|
add_resource_relationships(@hash[:data], serializer)
|
||||||
end
|
end
|
||||||
@hash
|
@hash
|
||||||
end
|
end
|
||||||
@ -41,18 +41,18 @@ module ActiveModel
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def add_links(resource, name, serializers)
|
def add_relationships(resource, name, serializers)
|
||||||
resource[:links] ||= {}
|
resource[:relationships] ||= {}
|
||||||
resource[:links][name] ||= { linkage: [] }
|
resource[:relationships][name] ||= { data: [] }
|
||||||
resource[:links][name][:linkage] += serializers.map { |serializer| { type: serializer.type, id: serializer.id.to_s } }
|
resource[:relationships][name][:data] += serializers.map { |serializer| { type: serializer.type, id: serializer.id.to_s } }
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_link(resource, name, serializer, val=nil)
|
def add_relationship(resource, name, serializer, val=nil)
|
||||||
resource[:links] ||= {}
|
resource[:relationships] ||= {}
|
||||||
resource[:links][name] = { linkage: nil }
|
resource[:relationships][name] = { data: nil }
|
||||||
|
|
||||||
if serializer && serializer.object
|
if serializer && serializer.object
|
||||||
resource[:links][name][:linkage] = { type: serializer.type, id: serializer.id.to_s }
|
resource[:relationships][name][:data] = { type: serializer.type, id: serializer.id.to_s }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -68,7 +68,7 @@ module ActiveModel
|
|||||||
serializers.each do |serializer|
|
serializers.each do |serializer|
|
||||||
attrs = attributes_for_serializer(serializer, @options)
|
attrs = attributes_for_serializer(serializer, @options)
|
||||||
|
|
||||||
add_resource_links(attrs, serializer, add_included: false)
|
add_resource_relationships(attrs, serializer, add_included: false)
|
||||||
|
|
||||||
@hash[:included].push(attrs) unless @hash[:included].include?(attrs)
|
@hash[:included].push(attrs) unless @hash[:included].include?(attrs)
|
||||||
end
|
end
|
||||||
@ -85,26 +85,31 @@ module ActiveModel
|
|||||||
if serializer.respond_to?(:each)
|
if serializer.respond_to?(:each)
|
||||||
result = []
|
result = []
|
||||||
serializer.each do |object|
|
serializer.each do |object|
|
||||||
options[:fields] = @fieldset && @fieldset.fields_for(serializer)
|
result << resource_object_for(object, options)
|
||||||
result << cache_check(object) do
|
|
||||||
options[:required_fields] = [:id, :type]
|
|
||||||
attributes = object.attributes(options)
|
|
||||||
attributes[:id] = attributes[:id].to_s
|
|
||||||
result << attributes
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
options[:fields] = @fieldset && @fieldset.fields_for(serializer)
|
result = resource_object_for(serializer, options)
|
||||||
options[:required_fields] = [:id, :type]
|
|
||||||
result = cache_check(serializer) do
|
|
||||||
result = serializer.attributes(options)
|
|
||||||
result[:id] = result[:id].to_s
|
|
||||||
result
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
result
|
result
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def resource_object_for(serializer, options)
|
||||||
|
options[:fields] = @fieldset && @fieldset.fields_for(serializer)
|
||||||
|
options[:required_fields] = [:id, :type]
|
||||||
|
|
||||||
|
cache_check(serializer) do
|
||||||
|
attributes = serializer.attributes(options)
|
||||||
|
|
||||||
|
result = {
|
||||||
|
id: attributes.delete(:id).to_s,
|
||||||
|
type: attributes.delete(:type)
|
||||||
|
}
|
||||||
|
|
||||||
|
result[:attributes] = attributes if attributes.any?
|
||||||
|
result
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def include_assoc?(assoc)
|
def include_assoc?(assoc)
|
||||||
return false unless @options[:include]
|
return false unless @options[:include]
|
||||||
check_assoc("#{assoc}$")
|
check_assoc("#{assoc}$")
|
||||||
@ -123,19 +128,19 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_resource_links(attrs, serializer, options = {})
|
def add_resource_relationships(attrs, serializer, options = {})
|
||||||
options[:add_included] = options.fetch(:add_included, true)
|
options[:add_included] = options.fetch(:add_included, true)
|
||||||
|
|
||||||
serializer.each_association do |name, association, opts|
|
serializer.each_association do |name, association, opts|
|
||||||
attrs[:links] ||= {}
|
attrs[:relationships] ||= {}
|
||||||
|
|
||||||
if association.respond_to?(:each)
|
if association.respond_to?(:each)
|
||||||
add_links(attrs, name, association)
|
add_relationships(attrs, name, association)
|
||||||
else
|
else
|
||||||
if opts[:virtual_value]
|
if opts[:virtual_value]
|
||||||
add_link(attrs, name, nil, opts[:virtual_value])
|
add_relationship(attrs, name, nil, opts[:virtual_value])
|
||||||
else
|
else
|
||||||
add_link(attrs, name, association)
|
add_relationship(attrs, name, association)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -10,9 +10,10 @@ module ActiveModel
|
|||||||
core_non_cached = non_cached_hash.first
|
core_non_cached = non_cached_hash.first
|
||||||
no_root_cache = cached_hash.delete_if {|key, value| key == core_cached[0] }
|
no_root_cache = cached_hash.delete_if {|key, value| key == core_cached[0] }
|
||||||
no_root_non_cache = non_cached_hash.delete_if {|key, value| key == core_non_cached[0] }
|
no_root_non_cache = non_cached_hash.delete_if {|key, value| key == core_non_cached[0] }
|
||||||
cached_resource = (core_cached[1]) ? core_cached[1].merge(core_non_cached[1]) : core_non_cached[1]
|
cached_resource = (core_cached[1]) ? core_cached[1].deep_merge(core_non_cached[1]) : core_non_cached[1]
|
||||||
hash = (root) ? { root => cached_resource } : cached_resource
|
hash = (root) ? { root => cached_resource } : cached_resource
|
||||||
hash.merge no_root_non_cache.merge no_root_cache
|
|
||||||
|
hash.deep_merge no_root_non_cache.deep_merge no_root_cache
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
@ -32,10 +32,12 @@ module ActionController
|
|||||||
|
|
||||||
expected = {
|
expected = {
|
||||||
data: {
|
data: {
|
||||||
name: "Name 1",
|
|
||||||
description: "Description 1",
|
|
||||||
id: assigns(:profile).id.to_s,
|
id: assigns(:profile).id.to_s,
|
||||||
type: "profiles"
|
type: "profiles",
|
||||||
|
attributes: {
|
||||||
|
name: "Name 1",
|
||||||
|
description: "Description 1",
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -91,7 +91,7 @@ module ActionController
|
|||||||
response = JSON.parse(@response.body)
|
response = JSON.parse(@response.body)
|
||||||
assert response.key? 'included'
|
assert response.key? 'included'
|
||||||
assert_equal 1, response['included'].size
|
assert_equal 1, response['included'].size
|
||||||
assert_equal 'Steve K.', response['included'].first['name']
|
assert_equal 'Steve K.', response['included'].first['attributes']['name']
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_render_resource_with_nested_has_many_include
|
def test_render_resource_with_nested_has_many_include
|
||||||
@ -101,29 +101,35 @@ module ActionController
|
|||||||
{
|
{
|
||||||
"id" => "1",
|
"id" => "1",
|
||||||
"type" => "authors",
|
"type" => "authors",
|
||||||
"name" => "Steve K.",
|
"attributes" => {
|
||||||
"links" => {
|
"name" => "Steve K."
|
||||||
"posts" => { "linkage" => [] },
|
},
|
||||||
"roles" => { "linkage" => [{ "type" =>"roles", "id" => "1" }, { "type" =>"roles", "id" => "2" }] },
|
"relationships" => {
|
||||||
"bio" => { "linkage" => nil }
|
"posts" => { "data" => [] },
|
||||||
|
"roles" => { "data" => [{ "type" =>"roles", "id" => "1" }, { "type" =>"roles", "id" => "2" }] },
|
||||||
|
"bio" => { "data" => nil }
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
"id" => "1",
|
"id" => "1",
|
||||||
"type" => "roles",
|
"type" => "roles",
|
||||||
"name" => "admin",
|
"attributes" => {
|
||||||
"description" => nil,
|
"name" => "admin",
|
||||||
"slug" => "admin-1",
|
"description" => nil,
|
||||||
"links" => {
|
"slug" => "admin-1"
|
||||||
"author" => { "linkage" => { "type" =>"authors", "id" => "1" } }
|
},
|
||||||
|
"relationships" => {
|
||||||
|
"author" => { "data" => { "type" =>"authors", "id" => "1" } }
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
"id" => "2",
|
"id" => "2",
|
||||||
"type" => "roles",
|
"type" => "roles",
|
||||||
"name" => "colab",
|
"attributes" => {
|
||||||
"description" => nil,
|
"name" => "colab",
|
||||||
"slug" => "colab-2",
|
"description" => nil,
|
||||||
"links" => {
|
"slug" => "colab-2"
|
||||||
"author" => { "linkage" => { "type" =>"authors", "id" => "1" } }
|
},
|
||||||
|
"relationships" => {
|
||||||
|
"author" => { "data" => { "type" =>"authors", "id" => "1" } }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@ -135,7 +141,7 @@ module ActionController
|
|||||||
response = JSON.parse(@response.body)
|
response = JSON.parse(@response.body)
|
||||||
assert response.key? 'included'
|
assert response.key? 'included'
|
||||||
assert_equal 1, response['included'].size
|
assert_equal 1, response['included'].size
|
||||||
assert_equal 'Anonymous', response['included'].first['name']
|
assert_equal 'Anonymous', response['included'].first['attributes']['name']
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_render_collection_without_include
|
def test_render_collection_without_include
|
||||||
|
|||||||
@ -27,7 +27,7 @@ class DefaultScopeNameTest < ActionController::TestCase
|
|||||||
|
|
||||||
def test_default_scope_name
|
def test_default_scope_name
|
||||||
get :render_new_user
|
get :render_new_user
|
||||||
assert_equal '{"data":{"admin?":false,"id":"1","type":"users"}}', @response.body
|
assert_equal '{"data":{"id":"1","type":"users","attributes":{"admin?":false}}}', @response.body
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -58,6 +58,6 @@ class SerializationScopeNameTest < ActionController::TestCase
|
|||||||
|
|
||||||
def test_override_scope_name_with_controller
|
def test_override_scope_name_with_controller
|
||||||
get :render_new_user
|
get :render_new_user
|
||||||
assert_equal '{"data":{"admin?":true,"id":"1","type":"users"}}', @response.body
|
assert_equal '{"data":{"id":"1","type":"users","attributes":{"admin?":true}}}', @response.body
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -166,10 +166,12 @@ module ActionController
|
|||||||
|
|
||||||
expected = {
|
expected = {
|
||||||
data: {
|
data: {
|
||||||
name: "Name 1",
|
|
||||||
description: "Description 1",
|
|
||||||
id: assigns(:profile).id.to_s,
|
id: assigns(:profile).id.to_s,
|
||||||
type: "profiles"
|
type: "profiles",
|
||||||
|
attributes: {
|
||||||
|
name: "Name 1",
|
||||||
|
description: "Description 1"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -182,10 +184,12 @@ module ActionController
|
|||||||
|
|
||||||
expected = {
|
expected = {
|
||||||
data: {
|
data: {
|
||||||
name: "Name 1",
|
|
||||||
description: "Description 1",
|
|
||||||
id: assigns(:profile).id.to_s,
|
id: assigns(:profile).id.to_s,
|
||||||
type: "profiles"
|
type: "profiles",
|
||||||
|
attributes: {
|
||||||
|
name: "Name 1",
|
||||||
|
description: "Description 1"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -217,10 +221,12 @@ module ActionController
|
|||||||
expected = {
|
expected = {
|
||||||
data: [
|
data: [
|
||||||
{
|
{
|
||||||
name: "Name 1",
|
|
||||||
description: "Description 1",
|
|
||||||
id: assigns(:profiles).first.id.to_s,
|
id: assigns(:profiles).first.id.to_s,
|
||||||
type: "profiles"
|
type: "profiles",
|
||||||
|
attributes: {
|
||||||
|
name: "Name 1",
|
||||||
|
description: "Description 1"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
meta: {
|
meta: {
|
||||||
|
|||||||
@ -32,9 +32,9 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_includes_post_id
|
def test_includes_post_id
|
||||||
expected = { linkage: { type: "posts", id: "42" } }
|
expected = { data: { type: "posts", id: "42" } }
|
||||||
|
|
||||||
assert_equal(expected, @adapter.serializable_hash[:data][:links][:post])
|
assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:post])
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_includes_linked_post
|
def test_includes_linked_post
|
||||||
@ -42,12 +42,14 @@ module ActiveModel
|
|||||||
expected = [{
|
expected = [{
|
||||||
id: "42",
|
id: "42",
|
||||||
type: "posts",
|
type: "posts",
|
||||||
title: 'New Post',
|
attributes: {
|
||||||
body: 'Body',
|
title: 'New Post',
|
||||||
links: {
|
body: 'Body',
|
||||||
comments: { linkage: [ { type: "comments", id: "1" } ] },
|
},
|
||||||
blog: { linkage: { type: "blogs", id: "999" } },
|
relationships: {
|
||||||
author: { linkage: { type: "authors", id: "1" } }
|
comments: { data: [ { type: "comments", id: "1" } ] },
|
||||||
|
blog: { data: { type: "blogs", id: "999" } },
|
||||||
|
author: { data: { type: "authors", id: "1" } }
|
||||||
}
|
}
|
||||||
}]
|
}]
|
||||||
assert_equal expected, @adapter.serializable_hash[:included]
|
assert_equal expected, @adapter.serializable_hash[:included]
|
||||||
@ -58,11 +60,13 @@ module ActiveModel
|
|||||||
expected = [{
|
expected = [{
|
||||||
id: "42",
|
id: "42",
|
||||||
type: "posts",
|
type: "posts",
|
||||||
title: 'New Post',
|
attributes: {
|
||||||
links: {
|
title: 'New Post'
|
||||||
comments: { linkage: [ { type: "comments", id: "1" } ] },
|
},
|
||||||
blog: { linkage: { type: "blogs", id: "999" } },
|
relationships: {
|
||||||
author: { linkage: { type: "authors", id: "1" } }
|
comments: { data: [ { type: "comments", id: "1" } ] },
|
||||||
|
blog: { data: { type: "blogs", id: "999" } },
|
||||||
|
author: { data: { type: "authors", id: "1" } }
|
||||||
}
|
}
|
||||||
}]
|
}]
|
||||||
assert_equal expected, @adapter.serializable_hash[:included]
|
assert_equal expected, @adapter.serializable_hash[:included]
|
||||||
@ -72,22 +76,22 @@ module ActiveModel
|
|||||||
serializer = PostSerializer.new(@anonymous_post)
|
serializer = PostSerializer.new(@anonymous_post)
|
||||||
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
||||||
|
|
||||||
assert_equal({comments: { linkage: [] }, blog: { linkage: { type: "blogs", id: "999" } }, author: { linkage: nil }}, adapter.serializable_hash[:data][:links])
|
assert_equal({comments: { data: [] }, blog: { data: { type: "blogs", id: "999" } }, author: { data: nil }}, adapter.serializable_hash[:data][:relationships])
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_include_type_for_association_when_different_than_name
|
def test_include_type_for_association_when_different_than_name
|
||||||
serializer = BlogSerializer.new(@blog)
|
serializer = BlogSerializer.new(@blog)
|
||||||
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
||||||
links = adapter.serializable_hash[:data][:links]
|
relationships = adapter.serializable_hash[:data][:relationships]
|
||||||
expected = {
|
expected = {
|
||||||
writer: {
|
writer: {
|
||||||
linkage: {
|
data: {
|
||||||
type: "authors",
|
type: "authors",
|
||||||
id: "1"
|
id: "1"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
articles: {
|
articles: {
|
||||||
linkage: [
|
data: [
|
||||||
{
|
{
|
||||||
type: "posts",
|
type: "posts",
|
||||||
id: "42"
|
id: "42"
|
||||||
@ -99,7 +103,7 @@ module ActiveModel
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
assert_equal expected, links
|
assert_equal expected, relationships
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_include_linked_resources_with_type_name
|
def test_include_linked_resources_with_type_name
|
||||||
@ -110,31 +114,37 @@ module ActiveModel
|
|||||||
{
|
{
|
||||||
id: "1",
|
id: "1",
|
||||||
type: "authors",
|
type: "authors",
|
||||||
name: "Steve K.",
|
attributes: {
|
||||||
links: {
|
name: "Steve K."
|
||||||
posts: { linkage: [] },
|
},
|
||||||
roles: { linkage: [] },
|
relationships: {
|
||||||
bio: { linkage: nil }
|
posts: { data: [] },
|
||||||
|
roles: { data: [] },
|
||||||
|
bio: { data: nil }
|
||||||
}
|
}
|
||||||
},{
|
},{
|
||||||
id: "42",
|
id: "42",
|
||||||
type: "posts",
|
type: "posts",
|
||||||
title: "New Post",
|
attributes: {
|
||||||
body: "Body",
|
title: "New Post",
|
||||||
links: {
|
body: "Body"
|
||||||
comments: { linkage: [ { type: "comments", id: "1" } ] },
|
},
|
||||||
blog: { linkage: { type: "blogs", id: "999" } },
|
relationships: {
|
||||||
author: { linkage: { type: "authors", id: "1" } }
|
comments: { data: [ { type: "comments", id: "1" } ] },
|
||||||
|
blog: { data: { type: "blogs", id: "999" } },
|
||||||
|
author: { data: { type: "authors", id: "1" } }
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
id: "43",
|
id: "43",
|
||||||
type: "posts",
|
type: "posts",
|
||||||
title: "Hello!!",
|
attributes: {
|
||||||
body: "Hello, world!!",
|
title: "Hello!!",
|
||||||
links: {
|
body: "Hello, world!!"
|
||||||
comments: { linkage: [] },
|
},
|
||||||
blog: { linkage: { type: "blogs", id: "999" } },
|
relationships: {
|
||||||
author: { linkage: nil }
|
comments: { data: [] },
|
||||||
|
blog: { data: { type: "blogs", id: "999" } },
|
||||||
|
author: { data: nil }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|||||||
@ -29,23 +29,27 @@ module ActiveModel
|
|||||||
{
|
{
|
||||||
id: "1",
|
id: "1",
|
||||||
type: "posts",
|
type: "posts",
|
||||||
title: "Hello!!",
|
attributes: {
|
||||||
body: "Hello, world!!",
|
title: "Hello!!",
|
||||||
links: {
|
body: "Hello, world!!"
|
||||||
comments: { linkage: [] },
|
},
|
||||||
blog: { linkage: { type: "blogs", id: "999" } },
|
relationships: {
|
||||||
author: { linkage: { type: "authors", id: "1" } }
|
comments: { data: [] },
|
||||||
|
blog: { data: { type: "blogs", id: "999" } },
|
||||||
|
author: { data: { type: "authors", id: "1" } }
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: "2",
|
id: "2",
|
||||||
type: "posts",
|
type: "posts",
|
||||||
title: "New Post",
|
attributes: {
|
||||||
body: "Body",
|
title: "New Post",
|
||||||
links: {
|
body: "Body"
|
||||||
comments: { linkage: [] },
|
},
|
||||||
blog: { linkage: { type: "blogs", id: "999" } },
|
relationships: {
|
||||||
author: { linkage: { type: "authors", id: "1" } }
|
comments: { data: [] },
|
||||||
|
blog: { data: { type: "blogs", id: "999" } },
|
||||||
|
author: { data: { type: "authors", id: "1" } }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@ -60,21 +64,25 @@ module ActiveModel
|
|||||||
{
|
{
|
||||||
id: "1",
|
id: "1",
|
||||||
type: "posts",
|
type: "posts",
|
||||||
title: "Hello!!",
|
attributes: {
|
||||||
links: {
|
title: "Hello!!"
|
||||||
comments: { linkage: [] },
|
},
|
||||||
blog: { linkage: { type: "blogs", id: "999" } },
|
relationships: {
|
||||||
author: { linkage: { type: "authors", id: "1" } }
|
comments: { data: [] },
|
||||||
|
blog: { data: { type: "blogs", id: "999" } },
|
||||||
|
author: { data: { type: "authors", id: "1" } }
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: "2",
|
id: "2",
|
||||||
type: "posts",
|
type: "posts",
|
||||||
title: "New Post",
|
attributes: {
|
||||||
links: {
|
title: "New Post"
|
||||||
comments: { linkage: [] },
|
},
|
||||||
blog: { linkage: { type: "blogs", id: "999" } },
|
relationships: {
|
||||||
author: { linkage: { type: "authors", id: "1" } }
|
comments: { data: [] },
|
||||||
|
blog: { data: { type: "blogs", id: "999" } },
|
||||||
|
author: { data: { type: "authors", id: "1" } }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|||||||
@ -26,13 +26,13 @@ module ActiveModel
|
|||||||
|
|
||||||
def test_includes_comment_ids
|
def test_includes_comment_ids
|
||||||
expected = {
|
expected = {
|
||||||
linkage: [
|
data: [
|
||||||
{ type: "posts", id: "1"},
|
{ type: "posts", id: "1"},
|
||||||
{ type: "posts", id: "2"}
|
{ type: "posts", id: "2"}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_equal(expected, @adapter.serializable_hash[:data][:links][:posts])
|
assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:posts])
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_no_includes_linked_comments
|
def test_no_includes_linked_comments
|
||||||
|
|||||||
@ -30,13 +30,13 @@ module ActiveModel
|
|||||||
|
|
||||||
def test_includes_comment_ids
|
def test_includes_comment_ids
|
||||||
expected = {
|
expected = {
|
||||||
linkage: [
|
data: [
|
||||||
{ type: 'comments', id: '1' },
|
{ type: 'comments', id: '1' },
|
||||||
{ type: 'comments', id: '2' }
|
{ type: 'comments', id: '2' }
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_equal(expected, @adapter.serializable_hash[:data][:links][:comments])
|
assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:comments])
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_includes_linked_data
|
def test_includes_linked_data
|
||||||
@ -45,22 +45,22 @@ module ActiveModel
|
|||||||
{
|
{
|
||||||
id: '1',
|
id: '1',
|
||||||
type: 'comments',
|
type: 'comments',
|
||||||
links: {
|
relationships: {
|
||||||
post: { linkage: { type: 'posts', id: @post.id.to_s } }
|
post: { data: { type: 'posts', id: @post.id.to_s } }
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: '2',
|
id: '2',
|
||||||
type: 'comments',
|
type: 'comments',
|
||||||
links: {
|
relationships: {
|
||||||
post: { linkage: { type: 'posts', id: @post.id.to_s } }
|
post: { data: { type: 'posts', id: @post.id.to_s } }
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: @author.id.to_s,
|
id: @author.id.to_s,
|
||||||
type: "authors",
|
type: "authors",
|
||||||
links: {
|
relationships: {
|
||||||
posts: { linkage: [ {type: "posts", id: @post.id.to_s } ] }
|
posts: { data: [ {type: "posts", id: @post.id.to_s } ] }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@ -70,26 +70,26 @@ module ActiveModel
|
|||||||
|
|
||||||
def test_includes_author_id
|
def test_includes_author_id
|
||||||
expected = {
|
expected = {
|
||||||
linkage: { type: "authors", id: @author.id.to_s }
|
data: { type: "authors", id: @author.id.to_s }
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_equal(expected, @adapter.serializable_hash[:data][:links][:author])
|
assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:author])
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_explicit_serializer_with_null_resource
|
def test_explicit_serializer_with_null_resource
|
||||||
@post.author = nil
|
@post.author = nil
|
||||||
|
|
||||||
expected = { linkage: nil }
|
expected = { data: nil }
|
||||||
|
|
||||||
assert_equal(expected, @adapter.serializable_hash[:data][:links][:author])
|
assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:author])
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_explicit_serializer_with_null_collection
|
def test_explicit_serializer_with_null_collection
|
||||||
@post.comments = []
|
@post.comments = []
|
||||||
|
|
||||||
expected = { linkage: [] }
|
expected = { data: [] }
|
||||||
|
|
||||||
assert_equal(expected, @adapter.serializable_hash[:data][:links][:comments])
|
assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:comments])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -33,9 +33,9 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_includes_comment_ids
|
def test_includes_comment_ids
|
||||||
expected = { linkage: [ { type: "comments", id: "1" }, { type: "comments", id: "2" } ] }
|
expected = { data: [ { type: "comments", id: "1" }, { type: "comments", id: "2" } ] }
|
||||||
|
|
||||||
assert_equal(expected, @adapter.serializable_hash[:data][:links][:comments])
|
assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:comments])
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_includes_linked_comments
|
def test_includes_linked_comments
|
||||||
@ -43,18 +43,22 @@ module ActiveModel
|
|||||||
expected = [{
|
expected = [{
|
||||||
id: "1",
|
id: "1",
|
||||||
type: "comments",
|
type: "comments",
|
||||||
body: 'ZOMG A COMMENT',
|
attributes: {
|
||||||
links: {
|
body: 'ZOMG A COMMENT'
|
||||||
post: { linkage: { type: "posts", id: "1" } },
|
},
|
||||||
author: { linkage: nil }
|
relationships: {
|
||||||
|
post: { data: { type: "posts", id: "1" } },
|
||||||
|
author: { data: nil }
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
id: "2",
|
id: "2",
|
||||||
type: "comments",
|
type: "comments",
|
||||||
body: 'ZOMG ANOTHER COMMENT',
|
attributes: {
|
||||||
links: {
|
body: 'ZOMG ANOTHER COMMENT'
|
||||||
post: { linkage: { type: "posts", id: "1" } },
|
},
|
||||||
author: { linkage: nil }
|
relationships: {
|
||||||
|
post: { data: { type: "posts", id: "1" } },
|
||||||
|
author: { data: nil }
|
||||||
}
|
}
|
||||||
}]
|
}]
|
||||||
assert_equal expected, @adapter.serializable_hash[:included]
|
assert_equal expected, @adapter.serializable_hash[:included]
|
||||||
@ -65,16 +69,16 @@ module ActiveModel
|
|||||||
expected = [{
|
expected = [{
|
||||||
id: "1",
|
id: "1",
|
||||||
type: "comments",
|
type: "comments",
|
||||||
links: {
|
relationships: {
|
||||||
post: { linkage: { type: "posts", id: "1" } },
|
post: { data: { type: "posts", id: "1" } },
|
||||||
author: { linkage: nil }
|
author: { data: nil }
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
id: "2",
|
id: "2",
|
||||||
type: "comments",
|
type: "comments",
|
||||||
links: {
|
relationships: {
|
||||||
post: { linkage: { type: "posts", id: "1" } },
|
post: { data: { type: "posts", id: "1" } },
|
||||||
author: { linkage: nil }
|
author: { data: nil }
|
||||||
}
|
}
|
||||||
}]
|
}]
|
||||||
assert_equal expected, @adapter.serializable_hash[:included]
|
assert_equal expected, @adapter.serializable_hash[:included]
|
||||||
@ -90,9 +94,9 @@ module ActiveModel
|
|||||||
def test_include_type_for_association_when_different_than_name
|
def test_include_type_for_association_when_different_than_name
|
||||||
serializer = BlogSerializer.new(@blog)
|
serializer = BlogSerializer.new(@blog)
|
||||||
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
||||||
actual = adapter.serializable_hash[:data][:links][:articles]
|
actual = adapter.serializable_hash[:data][:relationships][:articles]
|
||||||
expected = {
|
expected = {
|
||||||
linkage: [{
|
data: [{
|
||||||
type: "posts",
|
type: "posts",
|
||||||
id: "1"
|
id: "1"
|
||||||
}]
|
}]
|
||||||
|
|||||||
@ -30,9 +30,9 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_includes_bio_id
|
def test_includes_bio_id
|
||||||
expected = { linkage: { type: "bios", id: "43" } }
|
expected = { data: { type: "bios", id: "43" } }
|
||||||
|
|
||||||
assert_equal(expected, @adapter.serializable_hash[:data][:links][:bio])
|
assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:bio])
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_includes_linked_bio
|
def test_includes_linked_bio
|
||||||
@ -41,11 +41,13 @@ module ActiveModel
|
|||||||
expected = [
|
expected = [
|
||||||
{
|
{
|
||||||
id: "43",
|
id: "43",
|
||||||
rating: nil,
|
|
||||||
type: "bios",
|
type: "bios",
|
||||||
content:"AMS Contributor",
|
attributes: {
|
||||||
links: {
|
content:"AMS Contributor",
|
||||||
author: { linkage: { type: "authors", id: "1" } }
|
rating: nil
|
||||||
|
},
|
||||||
|
relationships: {
|
||||||
|
author: { data: { type: "authors", id: "1" } }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|||||||
@ -54,77 +54,93 @@ module ActiveModel
|
|||||||
data: [
|
data: [
|
||||||
{
|
{
|
||||||
id: "10",
|
id: "10",
|
||||||
title: "Hello!!",
|
|
||||||
body: "Hello, world!!",
|
|
||||||
type: "posts",
|
type: "posts",
|
||||||
links: {
|
attributes: {
|
||||||
comments: { linkage: [ { type: "comments", id: '1' }, { type: "comments", id: '2' } ] },
|
title: "Hello!!",
|
||||||
blog: { linkage: { type: "blogs", id: "999" } },
|
body: "Hello, world!!"
|
||||||
author: { linkage: { type: "authors", id: "1" } }
|
},
|
||||||
|
relationships: {
|
||||||
|
comments: { data: [ { type: "comments", id: '1' }, { type: "comments", id: '2' } ] },
|
||||||
|
blog: { data: { type: "blogs", id: "999" } },
|
||||||
|
author: { data: { type: "authors", id: "1" } }
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: "20",
|
id: "20",
|
||||||
title: "New Post",
|
|
||||||
body: "Body",
|
|
||||||
type: "posts",
|
type: "posts",
|
||||||
links: {
|
attributes: {
|
||||||
comments: { linkage: [] },
|
title: "New Post",
|
||||||
blog: { linkage: { type: "blogs", id: "999" } },
|
body: "Body"
|
||||||
author: { linkage: { type: "authors", id: "2" } }
|
},
|
||||||
|
relationships: {
|
||||||
|
comments: { data: [] },
|
||||||
|
blog: { data: { type: "blogs", id: "999" } },
|
||||||
|
author: { data: { type: "authors", id: "2" } }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
included: [
|
included: [
|
||||||
{
|
{
|
||||||
id: "1",
|
id: "1",
|
||||||
body: "ZOMG A COMMENT",
|
|
||||||
type: "comments",
|
type: "comments",
|
||||||
links: {
|
attributes: {
|
||||||
post: { linkage: { type: "posts", id: "10" } },
|
body: "ZOMG A COMMENT"
|
||||||
author: { linkage: nil }
|
},
|
||||||
|
relationships: {
|
||||||
|
post: { data: { type: "posts", id: "10" } },
|
||||||
|
author: { data: nil }
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
id: "2",
|
id: "2",
|
||||||
body: "ZOMG ANOTHER COMMENT",
|
|
||||||
type: "comments",
|
type: "comments",
|
||||||
links: {
|
attributes: {
|
||||||
post: { linkage: { type: "posts", id: "10" } },
|
body: "ZOMG ANOTHER COMMENT",
|
||||||
author: { linkage: nil }
|
},
|
||||||
|
relationships: {
|
||||||
|
post: { data: { type: "posts", id: "10" } },
|
||||||
|
author: { data: nil }
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
id: "1",
|
id: "1",
|
||||||
name: "Steve K.",
|
|
||||||
type: "authors",
|
type: "authors",
|
||||||
links: {
|
attributes: {
|
||||||
posts: { linkage: [ { type: "posts", id: "10" }, { type: "posts", id: "30" } ] },
|
name: "Steve K."
|
||||||
roles: { linkage: [] },
|
},
|
||||||
bio: { linkage: { type: "bios", id: "1" } }
|
relationships: {
|
||||||
|
posts: { data: [ { type: "posts", id: "10" }, { type: "posts", id: "30" } ] },
|
||||||
|
roles: { data: [] },
|
||||||
|
bio: { data: { type: "bios", id: "1" } }
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
id: "1",
|
id: "1",
|
||||||
rating: nil,
|
|
||||||
type: "bios",
|
type: "bios",
|
||||||
content: "AMS Contributor",
|
attributes: {
|
||||||
links: {
|
content: "AMS Contributor",
|
||||||
author: { linkage: { type: "authors", id: "1" } }
|
rating: nil
|
||||||
|
},
|
||||||
|
relationships: {
|
||||||
|
author: { data: { type: "authors", id: "1" } }
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
id: "2",
|
id: "2",
|
||||||
name: "Tenderlove",
|
|
||||||
type: "authors",
|
type: "authors",
|
||||||
links: {
|
attributes: {
|
||||||
posts: { linkage: [ { type: "posts", id:"20" } ] },
|
name: "Tenderlove"
|
||||||
roles: { linkage: [] },
|
},
|
||||||
bio: { linkage: { type: "bios", id: "2" } }
|
relationships: {
|
||||||
|
posts: { data: [ { type: "posts", id:"20" } ] },
|
||||||
|
roles: { data: [] },
|
||||||
|
bio: { data: { type: "bios", id: "2" } }
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
id: "2",
|
id: "2",
|
||||||
rating: nil,
|
|
||||||
type: "bios",
|
type: "bios",
|
||||||
content: "Rails Contributor",
|
attributes: {
|
||||||
links: {
|
rating: nil,
|
||||||
author: { linkage: { type: "authors", id: "2" } }
|
content: "Rails Contributor",
|
||||||
|
},
|
||||||
|
relationships: {
|
||||||
|
author: { data: { type: "authors", id: "2" } }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@ -148,31 +164,37 @@ module ActiveModel
|
|||||||
{
|
{
|
||||||
id: "1",
|
id: "1",
|
||||||
type: "authors",
|
type: "authors",
|
||||||
name: "Steve K.",
|
attributes: {
|
||||||
links: {
|
name: "Steve K."
|
||||||
posts: { linkage: [ { type: "posts", id: "10"}, { type: "posts", id: "30" }] },
|
},
|
||||||
roles: { linkage: [] },
|
relationships: {
|
||||||
bio: { linkage: { type: "bios", id: "1" }}
|
posts: { data: [ { type: "posts", id: "10"}, { type: "posts", id: "30" }] },
|
||||||
|
roles: { data: [] },
|
||||||
|
bio: { data: { type: "bios", id: "1" }}
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
id: "10",
|
id: "10",
|
||||||
type: "posts",
|
type: "posts",
|
||||||
title: "Hello!!",
|
attributes: {
|
||||||
body: "Hello, world!!",
|
title: "Hello!!",
|
||||||
links: {
|
body: "Hello, world!!"
|
||||||
comments: { linkage: [ { type: "comments", id: "1"}, { type: "comments", id: "2" }] },
|
},
|
||||||
blog: { linkage: { type: "blogs", id: "999" } },
|
relationships: {
|
||||||
author: { linkage: { type: "authors", id: "1" } }
|
comments: { data: [ { type: "comments", id: "1"}, { type: "comments", id: "2" }] },
|
||||||
|
blog: { data: { type: "blogs", id: "999" } },
|
||||||
|
author: { data: { type: "authors", id: "1" } }
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
id: "30",
|
id: "30",
|
||||||
type: "posts",
|
type: "posts",
|
||||||
title: "Yet Another Post",
|
attributes: {
|
||||||
body: "Body",
|
title: "Yet Another Post",
|
||||||
links: {
|
body: "Body"
|
||||||
comments: { linkage: [] },
|
},
|
||||||
blog: { linkage: { type: "blogs", id: "999" } },
|
relationships: {
|
||||||
author: { linkage: { type: "authors", id: "1" } }
|
comments: { data: [] },
|
||||||
|
blog: { data: { type: "blogs", id: "999" } },
|
||||||
|
author: { data: { type: "authors", id: "1" } }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@ -186,16 +208,16 @@ module ActiveModel
|
|||||||
spammy_post.related = [Spam::UnrelatedLink.new(id: 456)]
|
spammy_post.related = [Spam::UnrelatedLink.new(id: 456)]
|
||||||
serializer = SpammyPostSerializer.new(spammy_post)
|
serializer = SpammyPostSerializer.new(spammy_post)
|
||||||
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
||||||
links = adapter.serializable_hash[:data][:links]
|
relationships = adapter.serializable_hash[:data][:relationships]
|
||||||
expected = {
|
expected = {
|
||||||
related: {
|
related: {
|
||||||
linkage: [{
|
data: [{
|
||||||
type: 'unrelated_links',
|
type: 'unrelated_links',
|
||||||
id: '456'
|
id: '456'
|
||||||
}]
|
}]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
assert_equal expected, links
|
assert_equal expected, relationships
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_multiple_references_to_same_resource
|
def test_multiple_references_to_same_resource
|
||||||
@ -208,18 +230,20 @@ module ActiveModel
|
|||||||
expected = [
|
expected = [
|
||||||
{
|
{
|
||||||
id: "10",
|
id: "10",
|
||||||
title: "Hello!!",
|
|
||||||
body: "Hello, world!!",
|
|
||||||
type: "posts",
|
type: "posts",
|
||||||
links: {
|
attributes: {
|
||||||
|
title: "Hello!!",
|
||||||
|
body: "Hello, world!!"
|
||||||
|
},
|
||||||
|
relationships: {
|
||||||
comments: {
|
comments: {
|
||||||
linkage: [{type: "comments", id: "1"}, {type: "comments", id: "2"}]
|
data: [{type: "comments", id: "1"}, {type: "comments", id: "2"}]
|
||||||
},
|
},
|
||||||
blog: {
|
blog: {
|
||||||
linkage: {type: "blogs", id: "999"}
|
data: {type: "blogs", id: "999"}
|
||||||
},
|
},
|
||||||
author: {
|
author: {
|
||||||
linkage: {type: "authors", id: "1"}
|
data: {type: "authors", id: "1"}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -239,12 +263,14 @@ module ActiveModel
|
|||||||
expected = {
|
expected = {
|
||||||
data: {
|
data: {
|
||||||
id: "10",
|
id: "10",
|
||||||
title: "Hello!!",
|
|
||||||
body: "Hello, world!!",
|
|
||||||
type: "posts",
|
type: "posts",
|
||||||
links: {
|
attributes: {
|
||||||
comments: { linkage: [ { type: "comments", id: '1' }, { type: "comments", id: '2' } ] },
|
title: "Hello!!",
|
||||||
author: { linkage: nil }
|
body: "Hello, world!!"
|
||||||
|
},
|
||||||
|
relationships: {
|
||||||
|
comments: { data: [ { type: "comments", id: '1' }, { type: "comments", id: '2' } ] },
|
||||||
|
author: { data: nil }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user