mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 23:06:50 +00:00
Use symbol for root in jsonapi, fix tests
This commit is contained in:
parent
3ba4386bda
commit
da86747a3e
@ -16,7 +16,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def serializable_hash(options = {})
|
def serializable_hash(options = {})
|
||||||
@root = 'data'
|
@root = :data
|
||||||
|
|
||||||
if serializer.respond_to?(:each)
|
if serializer.respond_to?(:each)
|
||||||
@hash[@root] = serializer.map do |s|
|
@hash[@root] = serializer.map do |s|
|
||||||
|
|||||||
@ -29,7 +29,7 @@ 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 '{"profiles":{"name":"Name 1","description":"Description 1"}}', response.body
|
assert_equal '{"data":{"name":"Name 1","description":"Description 1"}}', response.body
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_render_skipping_adapter
|
def test_render_skipping_adapter
|
||||||
|
|||||||
@ -29,7 +29,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 '{"users":{"admin?":false}}', @response.body
|
assert_equal '{"data":{"admin?":false}}', @response.body
|
||||||
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 '{"admin_users":{"admin?":true}}', @response.body
|
assert_equal '{"data":{"admin?":true}}', @response.body
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -111,14 +111,14 @@ module ActionController
|
|||||||
get :render_using_default_adapter_root
|
get :render_using_default_adapter_root
|
||||||
|
|
||||||
assert_equal 'application/json', @response.content_type
|
assert_equal 'application/json', @response.content_type
|
||||||
assert_equal '{"profiles":{"name":"Name 1","description":"Description 1"}}', @response.body
|
assert_equal '{"data":{"name":"Name 1","description":"Description 1"}}', @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
|
||||||
|
|
||||||
assert_equal 'application/json', @response.content_type
|
assert_equal 'application/json', @response.content_type
|
||||||
assert_equal '{"profile":{"name":"Name 1","description":"Description 1"}}', @response.body
|
assert_equal '{"data":{"name":"Name 1","description":"Description 1"}}', @response.body
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_render_array_using_implicit_serializer
|
def test_render_array_using_implicit_serializer
|
||||||
@ -143,7 +143,7 @@ module ActionController
|
|||||||
get :render_array_using_implicit_serializer_and_meta
|
get :render_array_using_implicit_serializer_and_meta
|
||||||
|
|
||||||
assert_equal 'application/json', @response.content_type
|
assert_equal 'application/json', @response.content_type
|
||||||
assert_equal '{"profiles":[{"name":"Name 1","description":"Description 1"}],"meta":{"total":10}}', @response.body
|
assert_equal '{"data":[{"name":"Name 1","description":"Description 1"}],"meta":{"total":10}}', @response.body
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_render_with_cache_enable
|
def test_render_with_cache_enable
|
||||||
|
|||||||
@ -32,7 +32,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_includes_post_id
|
def test_includes_post_id
|
||||||
assert_equal("42", @adapter.serializable_hash[:comments][:links][:post])
|
assert_equal("42", @adapter.serializable_hash[:data][:links][:post])
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_includes_linked_post
|
def test_includes_linked_post
|
||||||
@ -67,13 +67,13 @@ 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: [], blog: "999", author: nil}, adapter.serializable_hash[:posts][:links])
|
assert_equal({comments: [], blog: "999", author: nil}, adapter.serializable_hash[:data][:links])
|
||||||
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[:blogs][:links]
|
links = adapter.serializable_hash[:data][:links]
|
||||||
expected = {
|
expected = {
|
||||||
writer: {
|
writer: {
|
||||||
type: "author",
|
type: "author",
|
||||||
|
|||||||
@ -28,7 +28,7 @@ module ActiveModel
|
|||||||
assert_equal([
|
assert_equal([
|
||||||
{ title: "Hello!!", body: "Hello, world!!", id: "1", links: { comments: [], blog: "999", author: "1" } },
|
{ title: "Hello!!", body: "Hello, world!!", id: "1", links: { comments: [], blog: "999", author: "1" } },
|
||||||
{ title: "New Post", body: "Body", id: "2", links: { comments: [], blog: "999", author: "1" } }
|
{ title: "New Post", body: "Body", id: "2", links: { comments: [], blog: "999", author: "1" } }
|
||||||
], @adapter.serializable_hash[:posts])
|
], @adapter.serializable_hash[:data])
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_limiting_fields
|
def test_limiting_fields
|
||||||
@ -36,7 +36,7 @@ module ActiveModel
|
|||||||
assert_equal([
|
assert_equal([
|
||||||
{ title: "Hello!!", links: { comments: [], blog: "999", author: "1" } },
|
{ title: "Hello!!", links: { comments: [], blog: "999", author: "1" } },
|
||||||
{ title: "New Post", links: { comments: [], blog: "999", author: "1" } }
|
{ title: "New Post", links: { comments: [], blog: "999", author: "1" } }
|
||||||
], @adapter.serializable_hash[:posts])
|
], @adapter.serializable_hash[:data])
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
@ -25,7 +25,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_includes_comment_ids
|
def test_includes_comment_ids
|
||||||
assert_equal(["1", "2"], @adapter.serializable_hash[:authors][:links][:posts])
|
assert_equal(["1", "2"], @adapter.serializable_hash[:data][:links][:posts])
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_no_includes_linked_comments
|
def test_no_includes_linked_comments
|
||||||
|
|||||||
@ -30,7 +30,7 @@ module ActiveModel
|
|||||||
|
|
||||||
def test_includes_comment_ids
|
def test_includes_comment_ids
|
||||||
assert_equal(['1', '2'],
|
assert_equal(['1', '2'],
|
||||||
@adapter.serializable_hash[:posts][:links][:comments])
|
@adapter.serializable_hash[:data][:links][:comments])
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_includes_linked_comments
|
def test_includes_linked_comments
|
||||||
@ -42,7 +42,7 @@ module ActiveModel
|
|||||||
|
|
||||||
def test_includes_author_id
|
def test_includes_author_id
|
||||||
assert_equal(@author.id.to_s,
|
assert_equal(@author.id.to_s,
|
||||||
@adapter.serializable_hash[:posts][:links][:author])
|
@adapter.serializable_hash[:data][:links][:author])
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_includes_linked_authors
|
def test_includes_linked_authors
|
||||||
@ -53,13 +53,13 @@ module ActiveModel
|
|||||||
def test_explicit_serializer_with_null_resource
|
def test_explicit_serializer_with_null_resource
|
||||||
@post.author = nil
|
@post.author = nil
|
||||||
assert_equal(nil,
|
assert_equal(nil,
|
||||||
@adapter.serializable_hash[:posts][:links][:author])
|
@adapter.serializable_hash[:data][:links][:author])
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_explicit_serializer_with_null_collection
|
def test_explicit_serializer_with_null_collection
|
||||||
@post.comments = []
|
@post.comments = []
|
||||||
assert_equal([],
|
assert_equal([],
|
||||||
@adapter.serializable_hash[:posts][:links][:comments])
|
@adapter.serializable_hash[:data][:links][:comments])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -33,7 +33,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_includes_comment_ids
|
def test_includes_comment_ids
|
||||||
assert_equal(["1", "2"], @adapter.serializable_hash[:posts][:links][:comments])
|
assert_equal(["1", "2"], @adapter.serializable_hash[:data][:links][:comments])
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_includes_linked_comments
|
def test_includes_linked_comments
|
||||||
@ -84,7 +84,7 @@ 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[:blogs][:links][:articles]
|
actual = adapter.serializable_hash[:data][:links][:articles]
|
||||||
expected = {
|
expected = {
|
||||||
type: "posts",
|
type: "posts",
|
||||||
ids: ["1"]
|
ids: ["1"]
|
||||||
|
|||||||
@ -30,7 +30,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_includes_bio_id
|
def test_includes_bio_id
|
||||||
assert_equal("43", @adapter.serializable_hash[:authors][:links][:bio])
|
assert_equal("43", @adapter.serializable_hash[:data][:links][:bio])
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_includes_linked_bio
|
def test_includes_linked_bio
|
||||||
|
|||||||
@ -185,7 +185,7 @@ 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[:posts][:links]
|
links = adapter.serializable_hash[:data][:links]
|
||||||
expected = {
|
expected = {
|
||||||
related: {
|
related: {
|
||||||
type: 'unrelated_links',
|
type: 'unrelated_links',
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user