mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Implement included and id and type as per spec
This commit is contained in:
parent
d82c599c68
commit
33f3a88ba0
@ -164,6 +164,14 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def id
|
||||||
|
object.id if object
|
||||||
|
end
|
||||||
|
|
||||||
|
def type
|
||||||
|
object.class.to_s.demodulize.underscore.pluralize
|
||||||
|
end
|
||||||
|
|
||||||
def attributes(options = {})
|
def attributes(options = {})
|
||||||
attributes =
|
attributes =
|
||||||
if options[:fields]
|
if options[:fields]
|
||||||
@ -172,6 +180,8 @@ module ActiveModel
|
|||||||
self.class._attributes.dup
|
self.class._attributes.dup
|
||||||
end
|
end
|
||||||
|
|
||||||
|
attributes += options[:required_fields] if options[:required_fields]
|
||||||
|
|
||||||
attributes.each_with_object({}) do |name, hash|
|
attributes.each_with_object({}) do |name, hash|
|
||||||
hash[name] = send(name)
|
hash[name] = send(name)
|
||||||
end
|
end
|
||||||
|
|||||||
@ -35,11 +35,9 @@ module ActiveModel
|
|||||||
private
|
private
|
||||||
|
|
||||||
def add_links(resource, name, serializers)
|
def add_links(resource, name, serializers)
|
||||||
type = serialized_object_type(serializers)
|
|
||||||
resource[:links] ||= {}
|
resource[:links] ||= {}
|
||||||
|
|
||||||
resource[:links][name] ||= { linkage: [] }
|
resource[:links][name] ||= { linkage: [] }
|
||||||
resource[:links][name][:linkage] += serializers.map { |serializer| { type: type, id: serializer.id.to_s } }
|
resource[:links][name][:linkage] += serializers.map { |serializer| { type: serializer.type, id: serializer.id.to_s } }
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_link(resource, name, serializer)
|
def add_link(resource, name, serializer)
|
||||||
@ -47,9 +45,7 @@ module ActiveModel
|
|||||||
resource[:links][name] = { linkage: nil }
|
resource[:links][name] = { linkage: nil }
|
||||||
|
|
||||||
if serializer && serializer.object
|
if serializer && serializer.object
|
||||||
type = serialized_object_type(serializer)
|
resource[:links][name][:linkage] = { type: serializer.type, id: serializer.id.to_s }
|
||||||
|
|
||||||
resource[:links][name][:linkage] = { type: type, id: serializer.id.to_s }
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -58,17 +54,15 @@ module ActiveModel
|
|||||||
|
|
||||||
resource_path = [parent, resource_name].compact.join('.')
|
resource_path = [parent, resource_name].compact.join('.')
|
||||||
|
|
||||||
if include_assoc?(resource_path) && resource_type = serialized_object_type(serializers)
|
if include_assoc?(resource_path)
|
||||||
plural_name = resource_type.pluralize.to_sym
|
@top[:included] ||= []
|
||||||
@top[:linked] ||= {}
|
|
||||||
@top[:linked][plural_name] ||= []
|
|
||||||
|
|
||||||
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_links(attrs, serializer, add_included: false)
|
||||||
|
|
||||||
@top[:linked][plural_name].push(attrs) unless @top[:linked][plural_name].include?(attrs)
|
@top[:included].push(attrs) unless @top[:included].include?(attrs)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -85,14 +79,16 @@ module ActiveModel
|
|||||||
result = []
|
result = []
|
||||||
serializer.each do |object|
|
serializer.each do |object|
|
||||||
options[:fields] = @fieldset && @fieldset.fields_for(serializer)
|
options[:fields] = @fieldset && @fieldset.fields_for(serializer)
|
||||||
|
options[:required_fields] = [:id, :type]
|
||||||
attributes = object.attributes(options)
|
attributes = object.attributes(options)
|
||||||
attributes[:id] = attributes[:id].to_s if attributes[:id]
|
attributes[:id] = attributes[:id].to_s
|
||||||
result << attributes
|
result << attributes
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
options[:fields] = @fieldset && @fieldset.fields_for(serializer)
|
options[:fields] = @fieldset && @fieldset.fields_for(serializer)
|
||||||
|
options[:required_fields] = [:id, :type]
|
||||||
result = serializer.attributes(options)
|
result = serializer.attributes(options)
|
||||||
result[:id] = result[:id].to_s if result[:id]
|
result[:id] = result[:id].to_s
|
||||||
end
|
end
|
||||||
|
|
||||||
result
|
result
|
||||||
@ -116,11 +112,6 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def serialized_object_type(serializer)
|
|
||||||
return false unless Array(serializer).first
|
|
||||||
Array(serializer).first.object.class.to_s.demodulize.underscore.pluralize
|
|
||||||
end
|
|
||||||
|
|
||||||
def add_resource_links(attrs, serializer, options = {})
|
def add_resource_links(attrs, serializer, options = {})
|
||||||
options[:add_included] = options.fetch(:add_included, true)
|
options[:add_included] = options.fetch(:add_included, true)
|
||||||
|
|
||||||
|
|||||||
@ -29,7 +29,17 @@ module ActionController
|
|||||||
|
|
||||||
def test_render_using_adapter_override
|
def test_render_using_adapter_override
|
||||||
get :render_using_adapter_override
|
get :render_using_adapter_override
|
||||||
assert_equal '{"data":{"name":"Name 1","description":"Description 1"}}', response.body
|
|
||||||
|
expected = {
|
||||||
|
data: {
|
||||||
|
name: "Name 1",
|
||||||
|
description: "Description 1",
|
||||||
|
id: assigns(:profile).id.to_s,
|
||||||
|
type: "profiles"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assert_equal expected.to_json, response.body
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_render_skipping_adapter
|
def test_render_skipping_adapter
|
||||||
|
|||||||
@ -83,80 +83,87 @@ module ActionController
|
|||||||
def test_render_resource_without_include
|
def test_render_resource_without_include
|
||||||
get :render_resource_without_include
|
get :render_resource_without_include
|
||||||
response = JSON.parse(@response.body)
|
response = JSON.parse(@response.body)
|
||||||
refute response.key? 'linked'
|
refute response.key? 'included'
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_render_resource_with_include
|
def test_render_resource_with_include
|
||||||
get :render_resource_with_include
|
get :render_resource_with_include
|
||||||
response = JSON.parse(@response.body)
|
response = JSON.parse(@response.body)
|
||||||
assert response.key? 'linked'
|
assert response.key? 'included'
|
||||||
assert_equal 1, response['linked']['authors'].size
|
assert_equal 1, response['included'].size
|
||||||
assert_equal 'Steve K.', response['linked']['authors'].first['name']
|
assert_equal 'Steve K.', response['included'].first['name']
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_render_resource_with_nested_has_many_include
|
def test_render_resource_with_nested_has_many_include
|
||||||
get :render_resource_with_nested_has_many_include
|
get :render_resource_with_nested_has_many_include
|
||||||
response = JSON.parse(@response.body)
|
response = JSON.parse(@response.body)
|
||||||
expected_linked = {
|
expected_linked = [
|
||||||
"authors" => [{
|
{
|
||||||
"id" => "1",
|
"id" => "1",
|
||||||
|
"type" => "authors",
|
||||||
"name" => "Steve K.",
|
"name" => "Steve K.",
|
||||||
"links" => {
|
"links" => {
|
||||||
"posts" => { "linkage" => [] },
|
"posts" => { "linkage" => [] },
|
||||||
"roles" => { "linkage" => [{ "type" =>"roles", "id" => "1" }, { "type" =>"roles", "id" => "2" }] },
|
"roles" => { "linkage" => [{ "type" =>"roles", "id" => "1" }, { "type" =>"roles", "id" => "2" }] },
|
||||||
"bio" => { "linkage" => nil }
|
"bio" => { "linkage" => nil }
|
||||||
}
|
}
|
||||||
}],
|
}, {
|
||||||
"roles"=>[{
|
|
||||||
"id" => "1",
|
"id" => "1",
|
||||||
|
"type" => "roles",
|
||||||
"name" => "admin",
|
"name" => "admin",
|
||||||
"links" => {
|
"links" => {
|
||||||
"author" => { "linkage" => { "type" =>"authors", "id" => "1" } }
|
"author" => { "linkage" => { "type" =>"authors", "id" => "1" } }
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
"id" => "2",
|
"id" => "2",
|
||||||
|
"type" => "roles",
|
||||||
"name" => "colab",
|
"name" => "colab",
|
||||||
"links" => {
|
"links" => {
|
||||||
"author" => { "linkage" => { "type" =>"authors", "id" => "1" } }
|
"author" => { "linkage" => { "type" =>"authors", "id" => "1" } }
|
||||||
}
|
}
|
||||||
}]
|
}
|
||||||
}
|
]
|
||||||
assert_equal expected_linked, response['linked']
|
assert_equal expected_linked, response['included']
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_render_resource_with_nested_include
|
def test_render_resource_with_nested_include
|
||||||
get :render_resource_with_nested_include
|
get :render_resource_with_nested_include
|
||||||
response = JSON.parse(@response.body)
|
response = JSON.parse(@response.body)
|
||||||
assert response.key? 'linked'
|
assert response.key? 'included'
|
||||||
assert_equal 1, response['linked']['authors'].size
|
assert_equal 1, response['included'].size
|
||||||
assert_equal 'Anonymous', response['linked']['authors'].first['name']
|
assert_equal 'Anonymous', response['included'].first['name']
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_render_collection_without_include
|
def test_render_collection_without_include
|
||||||
get :render_collection_without_include
|
get :render_collection_without_include
|
||||||
response = JSON.parse(@response.body)
|
response = JSON.parse(@response.body)
|
||||||
refute response.key? 'linked'
|
refute response.key? 'included'
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_render_collection_with_include
|
def test_render_collection_with_include
|
||||||
get :render_collection_with_include
|
get :render_collection_with_include
|
||||||
response = JSON.parse(@response.body)
|
response = JSON.parse(@response.body)
|
||||||
assert response.key? 'linked'
|
assert response.key? 'included'
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_render_resource_with_nested_attributes_even_when_missing_associations
|
def test_render_resource_with_nested_attributes_even_when_missing_associations
|
||||||
get :render_resource_with_missing_nested_has_many_include
|
get :render_resource_with_missing_nested_has_many_include
|
||||||
response = JSON.parse(@response.body)
|
response = JSON.parse(@response.body)
|
||||||
assert response.key? 'linked'
|
assert response.key? 'included'
|
||||||
refute response['linked'].key? 'roles'
|
refute has_type?(response['included'], 'roles')
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_render_collection_with_missing_nested_has_many_include
|
def test_render_collection_with_missing_nested_has_many_include
|
||||||
get :render_collection_with_missing_nested_has_many_include
|
get :render_collection_with_missing_nested_has_many_include
|
||||||
response = JSON.parse(@response.body)
|
response = JSON.parse(@response.body)
|
||||||
assert response.key? 'linked'
|
assert response.key? 'included'
|
||||||
assert response['linked'].key? 'roles'
|
assert has_type?(response['included'], 'roles')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def has_type?(collection, value)
|
||||||
|
collection.detect { |i| i['type'] == value}
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -2,7 +2,7 @@ require 'test_helper'
|
|||||||
require 'pathname'
|
require 'pathname'
|
||||||
|
|
||||||
class DefaultScopeNameTest < ActionController::TestCase
|
class DefaultScopeNameTest < ActionController::TestCase
|
||||||
TestUser = Struct.new(:name, :admin)
|
TestUser = Struct.new(:id, :name, :admin)
|
||||||
|
|
||||||
class UserSerializer < ActiveModel::Serializer
|
class UserSerializer < ActiveModel::Serializer
|
||||||
attributes :admin?
|
attributes :admin?
|
||||||
@ -17,11 +17,11 @@ class DefaultScopeNameTest < ActionController::TestCase
|
|||||||
before_filter { request.format = :json }
|
before_filter { request.format = :json }
|
||||||
|
|
||||||
def current_user
|
def current_user
|
||||||
TestUser.new('Pete', false)
|
TestUser.new(1, 'Pete', false)
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_new_user
|
def render_new_user
|
||||||
render json: TestUser.new('pete', false), serializer: UserSerializer, adapter: :json_api
|
render json: TestUser.new(1, 'pete', false), serializer: UserSerializer, adapter: :json_api
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -29,12 +29,12 @@ 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}}', @response.body
|
assert_equal '{"data":{"admin?":false,"id":"1","type":"test_users"}}', @response.body
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class SerializationScopeNameTest < ActionController::TestCase
|
class SerializationScopeNameTest < ActionController::TestCase
|
||||||
TestUser = Struct.new(:name, :admin)
|
TestUser = Struct.new(:id, :name, :admin)
|
||||||
|
|
||||||
class AdminUserSerializer < ActiveModel::Serializer
|
class AdminUserSerializer < ActiveModel::Serializer
|
||||||
attributes :admin?
|
attributes :admin?
|
||||||
@ -50,11 +50,11 @@ class SerializationScopeNameTest < ActionController::TestCase
|
|||||||
before_filter { request.format = :json }
|
before_filter { request.format = :json }
|
||||||
|
|
||||||
def current_admin
|
def current_admin
|
||||||
TestUser.new('Bob', true)
|
TestUser.new(1, 'Bob', true)
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_new_user
|
def render_new_user
|
||||||
render json: TestUser.new('pete', false), serializer: AdminUserSerializer, adapter: :json_api
|
render json: TestUser.new(1, 'pete', false), serializer: AdminUserSerializer, adapter: :json_api
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -62,6 +62,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}}', @response.body
|
assert_equal '{"data":{"admin?":true,"id":"1","type":"test_users"}}', @response.body
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -15,13 +15,11 @@ module ActionController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def render_using_default_adapter_root
|
def render_using_default_adapter_root
|
||||||
old_adapter = ActiveModel::Serializer.config.adapter
|
with_adapter ActiveModel::Serializer::Adapter::JsonApi do
|
||||||
# JSON-API adapter sets root by default
|
# JSON-API adapter sets root by default
|
||||||
ActiveModel::Serializer.config.adapter = ActiveModel::Serializer::Adapter::JsonApi
|
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
||||||
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
render json: @profile
|
||||||
render json: @profile
|
end
|
||||||
ensure
|
|
||||||
ActiveModel::Serializer.config.adapter = old_adapter
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_using_custom_root_in_adapter_with_a_default
|
def render_using_custom_root_in_adapter_with_a_default
|
||||||
@ -39,15 +37,14 @@ module ActionController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def render_array_using_implicit_serializer_and_meta
|
def render_array_using_implicit_serializer_and_meta
|
||||||
old_adapter = ActiveModel::Serializer.config.adapter
|
with_adapter ActiveModel::Serializer::Adapter::JsonApi do
|
||||||
|
|
||||||
ActiveModel::Serializer.config.adapter = ActiveModel::Serializer::Adapter::JsonApi
|
@profiles = [
|
||||||
array = [
|
Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
||||||
Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
]
|
||||||
]
|
|
||||||
render json: array, meta: { total: 10 }
|
render json: @profiles, meta: { total: 10 }
|
||||||
ensure
|
end
|
||||||
ActiveModel::Serializer.config.adapter = old_adapter
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_object_with_cache_enabled
|
def render_object_with_cache_enabled
|
||||||
@ -88,6 +85,15 @@ module ActionController
|
|||||||
adapter = ActiveModel::Serializer.adapter.new(serializer)
|
adapter = ActiveModel::Serializer.adapter.new(serializer)
|
||||||
adapter.to_json
|
adapter.to_json
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def with_adapter(adapter)
|
||||||
|
old_adapter = ActiveModel::Serializer.config.adapter
|
||||||
|
# JSON-API adapter sets root by default
|
||||||
|
ActiveModel::Serializer.config.adapter = adapter
|
||||||
|
yield
|
||||||
|
ensure
|
||||||
|
ActiveModel::Serializer.config.adapter = old_adapter
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
tests MyController
|
tests MyController
|
||||||
@ -96,8 +102,13 @@ module ActionController
|
|||||||
def test_render_using_implicit_serializer
|
def test_render_using_implicit_serializer
|
||||||
get :render_using_implicit_serializer
|
get :render_using_implicit_serializer
|
||||||
|
|
||||||
|
expected = {
|
||||||
|
name: "Name 1",
|
||||||
|
description: "Description 1"
|
||||||
|
}
|
||||||
|
|
||||||
assert_equal 'application/json', @response.content_type
|
assert_equal 'application/json', @response.content_type
|
||||||
assert_equal '{"name":"Name 1","description":"Description 1"}', @response.body
|
assert_equal expected.to_json, @response.body
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_render_using_custom_root
|
def test_render_using_custom_root
|
||||||
@ -110,15 +121,33 @@ module ActionController
|
|||||||
def test_render_using_default_root
|
def test_render_using_default_root
|
||||||
get :render_using_default_adapter_root
|
get :render_using_default_adapter_root
|
||||||
|
|
||||||
|
expected = {
|
||||||
|
data: {
|
||||||
|
name: "Name 1",
|
||||||
|
description: "Description 1",
|
||||||
|
id: assigns(:profile).id.to_s,
|
||||||
|
type: "profiles"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
assert_equal 'application/json', @response.content_type
|
assert_equal 'application/json', @response.content_type
|
||||||
assert_equal '{"data":{"name":"Name 1","description":"Description 1"}}', @response.body
|
assert_equal expected.to_json, @response.body
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_render_using_custom_root_in_adapter_with_a_default
|
def test_render_using_custom_root_in_adapter_with_a_default
|
||||||
get :render_using_custom_root_in_adapter_with_a_default
|
get :render_using_custom_root_in_adapter_with_a_default
|
||||||
|
|
||||||
|
expected = {
|
||||||
|
data: {
|
||||||
|
name: "Name 1",
|
||||||
|
description: "Description 1",
|
||||||
|
id: assigns(:profile).id.to_s,
|
||||||
|
type: "profiles"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
assert_equal 'application/json', @response.content_type
|
assert_equal 'application/json', @response.content_type
|
||||||
assert_equal '{"data":{"name":"Name 1","description":"Description 1"}}', @response.body
|
assert_equal expected.to_json, @response.body
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_render_array_using_implicit_serializer
|
def test_render_array_using_implicit_serializer
|
||||||
@ -142,8 +171,22 @@ module ActionController
|
|||||||
def test_render_array_using_implicit_serializer_and_meta
|
def test_render_array_using_implicit_serializer_and_meta
|
||||||
get :render_array_using_implicit_serializer_and_meta
|
get :render_array_using_implicit_serializer_and_meta
|
||||||
|
|
||||||
|
expected = {
|
||||||
|
data: [
|
||||||
|
{
|
||||||
|
name: "Name 1",
|
||||||
|
description: "Description 1",
|
||||||
|
id: assigns(:profiles).first.id.to_s,
|
||||||
|
type: "profiles"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
meta: {
|
||||||
|
total: 10
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
assert_equal 'application/json', @response.content_type
|
assert_equal 'application/json', @response.content_type
|
||||||
assert_equal '{"data":[{"name":"Name 1","description":"Description 1"}],"meta":{"total":10}}', @response.body
|
assert_equal expected.to_json, @response.body
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_render_with_cache_enable
|
def test_render_with_cache_enable
|
||||||
|
|||||||
@ -41,6 +41,7 @@ module ActiveModel
|
|||||||
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'post')
|
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'post')
|
||||||
expected = [{
|
expected = [{
|
||||||
id: "42",
|
id: "42",
|
||||||
|
type: "posts",
|
||||||
title: 'New Post',
|
title: 'New Post',
|
||||||
body: 'Body',
|
body: 'Body',
|
||||||
links: {
|
links: {
|
||||||
@ -49,12 +50,14 @@ module ActiveModel
|
|||||||
author: { linkage: { type: "authors", id: "1" } }
|
author: { linkage: { type: "authors", id: "1" } }
|
||||||
}
|
}
|
||||||
}]
|
}]
|
||||||
assert_equal expected, @adapter.serializable_hash[:linked][:posts]
|
assert_equal expected, @adapter.serializable_hash[:included]
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_limiting_linked_post_fields
|
def test_limiting_linked_post_fields
|
||||||
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'post', fields: {post: [:title]})
|
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'post', fields: {post: [:title]})
|
||||||
expected = [{
|
expected = [{
|
||||||
|
id: "42",
|
||||||
|
type: "posts",
|
||||||
title: 'New Post',
|
title: 'New Post',
|
||||||
links: {
|
links: {
|
||||||
comments: { linkage: [ { type: "comments", id: "1" } ] },
|
comments: { linkage: [ { type: "comments", id: "1" } ] },
|
||||||
@ -62,7 +65,7 @@ module ActiveModel
|
|||||||
author: { linkage: { type: "authors", id: "1" } }
|
author: { linkage: { type: "authors", id: "1" } }
|
||||||
}
|
}
|
||||||
}]
|
}]
|
||||||
assert_equal expected, @adapter.serializable_hash[:linked][:posts]
|
assert_equal expected, @adapter.serializable_hash[:included]
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_include_nil_author
|
def test_include_nil_author
|
||||||
@ -102,37 +105,39 @@ module ActiveModel
|
|||||||
def test_include_linked_resources_with_type_name
|
def test_include_linked_resources_with_type_name
|
||||||
serializer = BlogSerializer.new(@blog)
|
serializer = BlogSerializer.new(@blog)
|
||||||
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer, include: ['writer', 'articles'])
|
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer, include: ['writer', 'articles'])
|
||||||
linked = adapter.serializable_hash[:linked]
|
linked = adapter.serializable_hash[:included]
|
||||||
expected = {
|
expected = [
|
||||||
authors: [{
|
{
|
||||||
id: "1",
|
id: "1",
|
||||||
|
type: "authors",
|
||||||
name: "Steve K.",
|
name: "Steve K.",
|
||||||
links: {
|
links: {
|
||||||
posts: { linkage: [] },
|
posts: { linkage: [] },
|
||||||
roles: { linkage: [] },
|
roles: { linkage: [] },
|
||||||
bio: { linkage: nil }
|
bio: { linkage: nil }
|
||||||
}
|
}
|
||||||
}],
|
},{
|
||||||
posts: [{
|
id: "42",
|
||||||
|
type: "posts",
|
||||||
title: "New Post",
|
title: "New Post",
|
||||||
body: "Body",
|
body: "Body",
|
||||||
id: "42",
|
|
||||||
links: {
|
links: {
|
||||||
comments: { linkage: [ { type: "comments", id: "1" } ] },
|
comments: { linkage: [ { type: "comments", id: "1" } ] },
|
||||||
blog: { linkage: { type: "blogs", id: "999" } },
|
blog: { linkage: { type: "blogs", id: "999" } },
|
||||||
author: { linkage: { type: "authors", id: "1" } }
|
author: { linkage: { type: "authors", id: "1" } }
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
|
id: "43",
|
||||||
|
type: "posts",
|
||||||
title: "Hello!!",
|
title: "Hello!!",
|
||||||
body: "Hello, world!!",
|
body: "Hello, world!!",
|
||||||
id: "43",
|
|
||||||
links: {
|
links: {
|
||||||
comments: { linkage: [] },
|
comments: { linkage: [] },
|
||||||
blog: { linkage: { type: "blogs", id: "999" } },
|
blog: { linkage: { type: "blogs", id: "999" } },
|
||||||
author: { linkage: nil }
|
author: { linkage: nil }
|
||||||
}
|
}
|
||||||
}]
|
}
|
||||||
}
|
]
|
||||||
assert_equal expected, linked
|
assert_equal expected, linked
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -27,9 +27,10 @@ module ActiveModel
|
|||||||
def test_include_multiple_posts
|
def test_include_multiple_posts
|
||||||
expected = [
|
expected = [
|
||||||
{
|
{
|
||||||
|
id: "1",
|
||||||
|
type: "posts",
|
||||||
title: "Hello!!",
|
title: "Hello!!",
|
||||||
body: "Hello, world!!",
|
body: "Hello, world!!",
|
||||||
id: "1",
|
|
||||||
links: {
|
links: {
|
||||||
comments: { linkage: [] },
|
comments: { linkage: [] },
|
||||||
blog: { linkage: { type: "blogs", id: "999" } },
|
blog: { linkage: { type: "blogs", id: "999" } },
|
||||||
@ -37,9 +38,10 @@ module ActiveModel
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
id: "2",
|
||||||
|
type: "posts",
|
||||||
title: "New Post",
|
title: "New Post",
|
||||||
body: "Body",
|
body: "Body",
|
||||||
id: "2",
|
|
||||||
links: {
|
links: {
|
||||||
comments: { linkage: [] },
|
comments: { linkage: [] },
|
||||||
blog: { linkage: { type: "blogs", id: "999" } },
|
blog: { linkage: { type: "blogs", id: "999" } },
|
||||||
@ -56,6 +58,8 @@ module ActiveModel
|
|||||||
|
|
||||||
expected = [
|
expected = [
|
||||||
{
|
{
|
||||||
|
id: "1",
|
||||||
|
type: "posts",
|
||||||
title: "Hello!!",
|
title: "Hello!!",
|
||||||
links: {
|
links: {
|
||||||
comments: { linkage: [] },
|
comments: { linkage: [] },
|
||||||
@ -64,6 +68,8 @@ module ActiveModel
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
id: "2",
|
||||||
|
type: "posts",
|
||||||
title: "New Post",
|
title: "New Post",
|
||||||
links: {
|
links: {
|
||||||
comments: { linkage: [] },
|
comments: { linkage: [] },
|
||||||
|
|||||||
@ -39,25 +39,33 @@ module ActiveModel
|
|||||||
assert_equal(expected, @adapter.serializable_hash[:data][:links][:comments])
|
assert_equal(expected, @adapter.serializable_hash[:data][:links][:comments])
|
||||||
end
|
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
|
# If CommentPreviewSerializer is applied correctly the body text will not be present in the output
|
||||||
expected = [
|
expected = [
|
||||||
{
|
{
|
||||||
id: '1',
|
id: '1',
|
||||||
|
type: 'comments',
|
||||||
links: {
|
links: {
|
||||||
post: { linkage: { type: 'posts', id: @post.id.to_s } }
|
post: { linkage: { type: 'posts', id: @post.id.to_s } }
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: '2',
|
id: '2',
|
||||||
|
type: 'comments',
|
||||||
links: {
|
links: {
|
||||||
post: { linkage: { type: 'posts', id: @post.id.to_s } }
|
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,
|
assert_equal(expected, @adapter.serializable_hash[:included])
|
||||||
@adapter.serializable_hash[:linked][:comments])
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_includes_author_id
|
def test_includes_author_id
|
||||||
@ -68,17 +76,6 @@ module ActiveModel
|
|||||||
assert_equal(expected, @adapter.serializable_hash[:data][:links][:author])
|
assert_equal(expected, @adapter.serializable_hash[:data][:links][:author])
|
||||||
end
|
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
|
def test_explicit_serializer_with_null_resource
|
||||||
@post.author = nil
|
@post.author = nil
|
||||||
|
|
||||||
|
|||||||
@ -42,6 +42,7 @@ module ActiveModel
|
|||||||
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'comments')
|
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'comments')
|
||||||
expected = [{
|
expected = [{
|
||||||
id: "1",
|
id: "1",
|
||||||
|
type: "comments",
|
||||||
body: 'ZOMG A COMMENT',
|
body: 'ZOMG A COMMENT',
|
||||||
links: {
|
links: {
|
||||||
post: { linkage: { type: "posts", id: "1" } },
|
post: { linkage: { type: "posts", id: "1" } },
|
||||||
@ -49,31 +50,34 @@ module ActiveModel
|
|||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
id: "2",
|
id: "2",
|
||||||
|
type: "comments",
|
||||||
body: 'ZOMG ANOTHER COMMENT',
|
body: 'ZOMG ANOTHER COMMENT',
|
||||||
links: {
|
links: {
|
||||||
post: { linkage: { type: "posts", id: "1" } },
|
post: { linkage: { type: "posts", id: "1" } },
|
||||||
author: { linkage: nil }
|
author: { linkage: nil }
|
||||||
}
|
}
|
||||||
}]
|
}]
|
||||||
assert_equal expected, @adapter.serializable_hash[:linked][:comments]
|
assert_equal expected, @adapter.serializable_hash[:included]
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_limit_fields_of_linked_comments
|
def test_limit_fields_of_linked_comments
|
||||||
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'comments', fields: {comment: [:id]})
|
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'comments', fields: {comment: [:id]})
|
||||||
expected = [{
|
expected = [{
|
||||||
id: "1",
|
id: "1",
|
||||||
|
type: "comments",
|
||||||
links: {
|
links: {
|
||||||
post: { linkage: { type: "posts", id: "1" } },
|
post: { linkage: { type: "posts", id: "1" } },
|
||||||
author: { linkage: nil }
|
author: { linkage: nil }
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
id: "2",
|
id: "2",
|
||||||
|
type: "comments",
|
||||||
links: {
|
links: {
|
||||||
post: { linkage: { type: "posts", id: "1" } },
|
post: { linkage: { type: "posts", id: "1" } },
|
||||||
author: { linkage: nil }
|
author: { linkage: nil }
|
||||||
}
|
}
|
||||||
}]
|
}]
|
||||||
assert_equal expected, @adapter.serializable_hash[:linked][:comments]
|
assert_equal expected, @adapter.serializable_hash[:included]
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_no_include_linked_if_comments_is_empty
|
def test_no_include_linked_if_comments_is_empty
|
||||||
|
|||||||
@ -41,6 +41,7 @@ module ActiveModel
|
|||||||
expected = [
|
expected = [
|
||||||
{
|
{
|
||||||
id: "43",
|
id: "43",
|
||||||
|
type: "bios",
|
||||||
content:"AMS Contributor",
|
content:"AMS Contributor",
|
||||||
links: {
|
links: {
|
||||||
author: { linkage: { type: "authors", id: "1" } }
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -142,42 +142,41 @@ module ActiveModel
|
|||||||
include: 'author,author.posts'
|
include: 'author,author.posts'
|
||||||
)
|
)
|
||||||
|
|
||||||
expected = {
|
expected = [
|
||||||
authors: [
|
{
|
||||||
{
|
id: "1",
|
||||||
id: "1",
|
type: "authors",
|
||||||
name: "Steve K.",
|
name: "Steve K.",
|
||||||
links: {
|
links: {
|
||||||
posts: { linkage: [ { type: "posts", id: "10"}, { type: "posts", id: "30" }] },
|
posts: { linkage: [ { type: "posts", id: "10"}, { type: "posts", id: "30" }] },
|
||||||
roles: { linkage: [] },
|
roles: { linkage: [] },
|
||||||
bio: { linkage: { type: "bios", id: "1" }}
|
bio: { linkage: { type: "bios", id: "1" }}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
],
|
}, {
|
||||||
posts: [
|
id: "10",
|
||||||
{
|
type: "posts",
|
||||||
id: "10",
|
title: "Hello!!",
|
||||||
title: "Hello!!",
|
body: "Hello, world!!",
|
||||||
body: "Hello, world!!",
|
links: {
|
||||||
links: {
|
comments: { linkage: [ { type: "comments", id: "1"}, { type: "comments", id: "2" }] },
|
||||||
comments: { linkage: [ { type: "comments", id: "1"}, { type: "comments", id: "2" }] },
|
blog: { linkage: { type: "blogs", id: "999" } },
|
||||||
blog: { linkage: { type: "blogs", id: "999" } },
|
author: { linkage: { type: "authors", id: "1" } }
|
||||||
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: "30",
|
||||||
assert_equal expected, adapter.serializable_hash[:linked]
|
type: "posts",
|
||||||
assert_equal expected, alt_adapter.serializable_hash[:linked]
|
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
|
end
|
||||||
|
|
||||||
def test_ignore_model_namespace_for_linked_resource_type
|
def test_ignore_model_namespace_for_linked_resource_type
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user