diff --git a/lib/active_model/serializer/adapter/json_api.rb b/lib/active_model/serializer/adapter/json_api.rb index 461d28f4..0c5aaf97 100644 --- a/lib/active_model/serializer/adapter/json_api.rb +++ b/lib/active_model/serializer/adapter/json_api.rb @@ -38,30 +38,18 @@ module ActiveModel type = serialized_object_type(serializers) resource[:links] ||= {} - if name.to_s == type || !type - resource[:links][name] ||= [] - resource[:links][name] += serializers.map{|serializer| serializer.id.to_s } - else - resource[:links][name] ||= {} - resource[:links][name][:type] = type - resource[:links][name][:ids] ||= [] - resource[:links][name][:ids] += serializers.map{|serializer| serializer.id.to_s } - end + resource[:links][name] ||= { linkage: [] } + resource[:links][name][:linkage] += serializers.map { |serializer| { type: type, id: serializer.id.to_s } } end def add_link(resource, name, serializer) resource[:links] ||= {} - resource[:links][name] = nil + resource[:links][name] = { linkage: nil } if serializer && serializer.object type = serialized_object_type(serializer) - if name.to_s == type || !type - resource[:links][name] = serializer.id.to_s - else - resource[:links][name] ||= {} - resource[:links][name][:type] = type - resource[:links][name][:id] = serializer.id.to_s - end + + resource[:links][name][:linkage] = { type: type, id: serializer.id.to_s } end end diff --git a/test/action_controller/json_api_linked_test.rb b/test/action_controller/json_api_linked_test.rb index 7d4dcbf8..e24f695b 100644 --- a/test/action_controller/json_api_linked_test.rb +++ b/test/action_controller/json_api_linked_test.rb @@ -102,22 +102,22 @@ module ActionController "id" => "1", "name" => "Steve K.", "links" => { - "posts" => [], - "roles" => ["1", "2"], - "bio" => nil + "posts" => { "linkage" => [] }, + "roles" => { "linkage" => [{ "type" =>"roles", "id" => "1" }, { "type" =>"roles", "id" => "2" }] }, + "bio" => { "linkage" => nil } } }], "roles"=>[{ "id" => "1", "name" => "admin", "links" => { - "author" => "1" + "author" => { "linkage" => { "type" =>"author", "id" => "1" } } } }, { "id" => "2", "name" => "colab", "links" => { - "author" => "1" + "author" => { "linkage" => { "type" =>"author", "id" => "1" } } } }] } diff --git a/test/adapter/json_api/belongs_to_test.rb b/test/adapter/json_api/belongs_to_test.rb index e5073d65..a6b1cd55 100644 --- a/test/adapter/json_api/belongs_to_test.rb +++ b/test/adapter/json_api/belongs_to_test.rb @@ -32,7 +32,9 @@ module ActiveModel end def test_includes_post_id - assert_equal("42", @adapter.serializable_hash[:data][:links][:post]) + expected = { linkage: { type: "post", id: "42" } } + + assert_equal(expected, @adapter.serializable_hash[:data][:links][:post]) end def test_includes_linked_post @@ -42,9 +44,9 @@ module ActiveModel title: 'New Post', body: 'Body', links: { - comments: ["1"], - blog: "999", - author: "1" + comments: { linkage: [ { type: "comments", id: "1" } ] }, + blog: { linkage: { type: "blog", id: "999" } }, + author: { linkage: { type: "author", id: "1" } } } }] assert_equal expected, @adapter.serializable_hash[:linked][:posts] @@ -55,9 +57,9 @@ module ActiveModel expected = [{ title: 'New Post', links: { - comments: ["1"], - blog: "999", - author: "1" + comments: { linkage: [ { type: "comments", id: "1" } ] }, + blog: { linkage: { type: "blog", id: "999" } }, + author: { linkage: { type: "author", id: "1" } } } }] assert_equal expected, @adapter.serializable_hash[:linked][:posts] @@ -67,7 +69,7 @@ module ActiveModel serializer = PostSerializer.new(@anonymous_post) adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer) - assert_equal({comments: [], blog: "999", author: nil}, adapter.serializable_hash[:data][:links]) + assert_equal({comments: { linkage: [] }, blog: { linkage: { type: "blog", id: "999" } }, author: { linkage: nil }}, adapter.serializable_hash[:data][:links]) end def test_include_type_for_association_when_different_than_name @@ -76,12 +78,22 @@ module ActiveModel links = adapter.serializable_hash[:data][:links] expected = { writer: { - type: "author", - id: "1" + linkage: { + type: "author", + id: "1" + } }, articles: { - type: "posts", - ids: ["42", "43"] + linkage: [ + { + type: "posts", + id: "42" + }, + { + type: "posts", + id: "43" + } + ] } } assert_equal expected, links @@ -96,9 +108,9 @@ module ActiveModel id: "1", name: "Steve K.", links: { - posts: [], - roles: [], - bio: nil + posts: { linkage: [] }, + roles: { linkage: [] }, + bio: { linkage: nil } } }], posts: [{ @@ -106,18 +118,18 @@ module ActiveModel body: "Body", id: "42", links: { - comments: ["1"], - blog: "999", - author: "1" + comments: { linkage: [ { type: "comments", id: "1" } ] }, + blog: { linkage: { type: "blog", id: "999" } }, + author: { linkage: { type: "author", id: "1" } } } }, { title: "Hello!!", body: "Hello, world!!", id: "43", links: { - comments: [], - blog: "999", - author: nil + comments: { linkage: [] }, + blog: { linkage: { type: "blog", id: "999" } }, + author: { linkage: nil } } }] } diff --git a/test/adapter/json_api/collection_test.rb b/test/adapter/json_api/collection_test.rb index 48b3952d..b7967feb 100644 --- a/test/adapter/json_api/collection_test.rb +++ b/test/adapter/json_api/collection_test.rb @@ -25,18 +25,55 @@ module ActiveModel end def test_include_multiple_posts - assert_equal([ - { 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" } } - ], @adapter.serializable_hash[:data]) + expected = [ + { + title: "Hello!!", + body: "Hello, world!!", + id: "1", + links: { + comments: { linkage: [] }, + blog: { linkage: { type: "blog", id: "999" } }, + author: { linkage: { type: "author", id: "1" } } + } + }, + { + title: "New Post", + body: "Body", + id: "2", + links: { + comments: { linkage: [] }, + blog: { linkage: { type: "blog", id: "999" } }, + author: { linkage: { type: "author", id: "1" } } + } + } + ] + + assert_equal(expected, @adapter.serializable_hash[:data]) end def test_limiting_fields @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, fields: ['title']) - assert_equal([ - { title: "Hello!!", links: { comments: [], blog: "999", author: "1" } }, - { title: "New Post", links: { comments: [], blog: "999", author: "1" } } - ], @adapter.serializable_hash[:data]) + + expected = [ + { + title: "Hello!!", + links: { + comments: { linkage: [] }, + blog: { linkage: { type: "blog", id: "999" } }, + author: { linkage: { type: "author", id: "1" } } + } + }, + { + title: "New Post", + links: { + comments: { linkage: [] }, + blog: { linkage: { type: "blog", id: "999" } }, + author: { linkage: { type: "author", id: "1" } } + } + } + ] + + assert_equal(expected, @adapter.serializable_hash[:data]) end end diff --git a/test/adapter/json_api/has_many_embed_ids_test.rb b/test/adapter/json_api/has_many_embed_ids_test.rb index 1f68006d..2a2cb3ea 100644 --- a/test/adapter/json_api/has_many_embed_ids_test.rb +++ b/test/adapter/json_api/has_many_embed_ids_test.rb @@ -25,7 +25,9 @@ module ActiveModel end def test_includes_comment_ids - assert_equal(["1", "2"], @adapter.serializable_hash[:data][:links][:posts]) + expected = {:linkage=>[{:type=>"posts", :id=>"1"}, {:type=>"posts", :id=>"2"}]} + + assert_equal(expected, @adapter.serializable_hash[:data][:links][:posts]) end def test_no_includes_linked_comments diff --git a/test/adapter/json_api/has_many_explicit_serializer_test.rb b/test/adapter/json_api/has_many_explicit_serializer_test.rb index e1df69f0..01368873 100644 --- a/test/adapter/json_api/has_many_explicit_serializer_test.rb +++ b/test/adapter/json_api/has_many_explicit_serializer_test.rb @@ -29,37 +29,70 @@ module ActiveModel end def test_includes_comment_ids - assert_equal(['1', '2'], - @adapter.serializable_hash[:data][:links][:comments]) + expected = { + linkage: [ + { type: 'comments', id: '1' }, + { type: 'comments', id: '2' } + ] + } + + assert_equal(expected, @adapter.serializable_hash[:data][:links][:comments]) end def test_includes_linked_comments # If CommentPreviewSerializer is applied correctly the body text will not be present in the output - assert_equal([{ id: '1', links: { post: @post.id.to_s}}, - { id: '2', links: { post: @post.id.to_s}}], + expected = [ + { + id: '1', + links: { + post: { linkage: { type: 'post', id: @post.id.to_s } } + } + }, + { + id: '2', + links: { + post: { linkage: { type: 'post', id: @post.id.to_s } } + } + } + ] + + assert_equal(expected, @adapter.serializable_hash[:linked][:comments]) end def test_includes_author_id - assert_equal(@author.id.to_s, - @adapter.serializable_hash[:data][:links][:author]) + expected = { + linkage: { type: "author", id: @author.id.to_s } + } + + assert_equal(expected, @adapter.serializable_hash[:data][:links][:author]) end def test_includes_linked_authors - assert_equal([{ id: @author.id.to_s, links: { posts: [@post.id.to_s] } }], - @adapter.serializable_hash[: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 - assert_equal(nil, - @adapter.serializable_hash[:data][:links][:author]) + + expected = { linkage: nil } + + assert_equal(expected, @adapter.serializable_hash[:data][:links][:author]) end def test_explicit_serializer_with_null_collection @post.comments = [] - assert_equal([], - @adapter.serializable_hash[:data][:links][:comments]) + + expected = { linkage: [] } + + assert_equal(expected, @adapter.serializable_hash[:data][:links][:comments]) end end end diff --git a/test/adapter/json_api/has_many_test.rb b/test/adapter/json_api/has_many_test.rb index a4ec8efd..eae4c620 100644 --- a/test/adapter/json_api/has_many_test.rb +++ b/test/adapter/json_api/has_many_test.rb @@ -33,7 +33,9 @@ module ActiveModel end def test_includes_comment_ids - assert_equal(["1", "2"], @adapter.serializable_hash[:data][:links][:comments]) + expected = { linkage: [ { type: "comments", id: "1" }, { type: "comments", id: "2" } ] } + + assert_equal(expected, @adapter.serializable_hash[:data][:links][:comments]) end def test_includes_linked_comments @@ -42,15 +44,15 @@ module ActiveModel id: "1", body: 'ZOMG A COMMENT', links: { - post: "1", - author: nil + post: { linkage: { type: "post", id: "1" } }, + author: { linkage: nil } } }, { id: "2", body: 'ZOMG ANOTHER COMMENT', links: { - post: "1", - author: nil + post: { linkage: { type: "post", id: "1" } }, + author: { linkage: nil } } }] assert_equal expected, @adapter.serializable_hash[:linked][:comments] @@ -61,14 +63,14 @@ module ActiveModel expected = [{ id: "1", links: { - post: "1", - author: nil + post: { linkage: { type: "post", id: "1" } }, + author: { linkage: nil } } }, { id: "2", links: { - post: "1", - author: nil + post: { linkage: { type: "post", id: "1" } }, + author: { linkage: nil } } }] assert_equal expected, @adapter.serializable_hash[:linked][:comments] @@ -86,8 +88,10 @@ module ActiveModel adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer) actual = adapter.serializable_hash[:data][:links][:articles] expected = { - type: "posts", - ids: ["1"] + linkage: [{ + type: "posts", + id: "1" + }] } assert_equal expected, actual end diff --git a/test/adapter/json_api/has_one_test.rb b/test/adapter/json_api/has_one_test.rb index 42a76156..f8e6dccf 100644 --- a/test/adapter/json_api/has_one_test.rb +++ b/test/adapter/json_api/has_one_test.rb @@ -30,12 +30,25 @@ module ActiveModel end def test_includes_bio_id - assert_equal("43", @adapter.serializable_hash[:data][:links][:bio]) + expected = { linkage: { type: "bio", id: "43" } } + + assert_equal(expected, @adapter.serializable_hash[:data][:links][:bio]) end def test_includes_linked_bio @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'bio') - assert_equal([{id: "43", :content=>"AMS Contributor", :links=>{:author=>"1"}}], @adapter.serializable_hash[:linked][:bios]) + + expected = [ + { + id: "43", + content:"AMS Contributor", + links: { + author: { linkage: { type: "author", id: "1" } } + } + } + ] + + assert_equal(expected, @adapter.serializable_hash[:linked][:bios]) end end end diff --git a/test/adapter/json_api/linked_test.rb b/test/adapter/json_api/linked_test.rb index dd28e74d..2a930238 100644 --- a/test/adapter/json_api/linked_test.rb +++ b/test/adapter/json_api/linked_test.rb @@ -57,15 +57,15 @@ module ActiveModel id: "1", body: "ZOMG A COMMENT", links: { - post: "1", - author: nil + post: { linkage: { type: "post", id: "1" } }, + author: { linkage: nil } } }, { id: "2", body: "ZOMG ANOTHER COMMENT", links: { - post: "1", - author: nil + post: { linkage: { type: "post", id: "1" } }, + author: { linkage: nil } } } ], @@ -74,17 +74,17 @@ module ActiveModel id: "1", name: "Steve K.", links: { - posts: ["1", "3"], - roles: [], - bio: "1" + posts: { linkage: [ { type: "posts", id: "1" }, { type: "posts", id: "3" } ] }, + roles: { linkage: [] }, + bio: { linkage: { type: "bio", id: "1" } } } }, { id: "2", name: "Tenderlove", links: { - posts: ["2"], - roles: [], - bio: "2" + posts: { linkage: [ { type: "posts", id:"2" } ] }, + roles: { linkage: [] }, + bio: { linkage: { type: "bio", id: "2" } } } } ], @@ -93,13 +93,13 @@ module ActiveModel id: "1", content: "AMS Contributor", links: { - author: "1" + author: { linkage: { type: "author", id: "1" } } } }, { id: "2", content: "Rails Contributor", links: { - author: "2" + author: { linkage: { type: "author", id: "2" } } } } ] @@ -110,9 +110,9 @@ module ActiveModel title: "Hello!!", body: "Hello, world!!", links: { - comments: ['1', '2'], - blog: "999", - author: "1" + comments: { linkage: [ { type: "comments", id: '1' }, { type: "comments", id: '2' } ] }, + blog: { linkage: { type: "blog", id: "999" } }, + author: { linkage: { type: "author", id: "1" } } } }, { @@ -120,9 +120,9 @@ module ActiveModel title: "New Post", body: "Body", links: { - comments: [], - blog: "999", - author: "2" + comments: { linkage: [] }, + blog: { linkage: { type: "blog", id: "999" } }, + author: { linkage: { type: "author", id: "2" } } } } ] @@ -148,9 +148,9 @@ module ActiveModel id: "1", name: "Steve K.", links: { - posts: ["10", "30"], - roles: [], - bio: "1" + posts: { linkage: [ { type: "posts", id: "10"}, { type: "posts", id: "30" }] }, + roles: { linkage: [] }, + bio: { linkage: { type: "bio", id: "1" }} } } ], @@ -160,18 +160,18 @@ module ActiveModel title: "Hello!!", body: "Hello, world!!", links: { - comments: ["1", "2"], - blog: "999", - author: "1" + comments: { linkage: [ { type: "comments", id: "1"}, { type: "comments", id: "2" }] }, + blog: { linkage: { type: "blog", id: "999" } }, + author: { linkage: { type: "author", id: "1" } } } }, { id: "30", title: "Yet Another Post", body: "Body", links: { - comments: [], - blog: "999", - author: "1" + comments: { linkage: [] }, + blog: { linkage: { type: "blog", id: "999" } }, + author: { linkage: { type: "author", id: "1" } } } } ] @@ -188,8 +188,10 @@ module ActiveModel links = adapter.serializable_hash[:data][:links] expected = { related: { - type: 'unrelated_links', - ids: ['456'] + linkage: [{ + type: 'unrelated_links', + id: '456' + }] } } assert_equal expected, links diff --git a/test/test_helper.rb b/test/test_helper.rb index f3977b61..b71a4b57 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,5 +1,5 @@ require 'bundler/setup' - +require 'pp' require 'rails' require 'action_controller' require 'action_controller/test_case'