Skip to content

Commit 0f55f21

Browse files
committed
Update format of links
1 parent 83c2854 commit 0f55f21

File tree

10 files changed

+198
-107
lines changed

10 files changed

+198
-107
lines changed

lib/active_model/serializer/adapter/json_api.rb

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,30 +38,18 @@ def add_links(resource, name, serializers)
3838
type = serialized_object_type(serializers)
3939
resource[:links] ||= {}
4040

41-
if name.to_s == type || !type
42-
resource[:links][name] ||= []
43-
resource[:links][name] += serializers.map{|serializer| serializer.id.to_s }
44-
else
45-
resource[:links][name] ||= {}
46-
resource[:links][name][:type] = type
47-
resource[:links][name][:ids] ||= []
48-
resource[:links][name][:ids] += serializers.map{|serializer| serializer.id.to_s }
49-
end
41+
resource[:links][name] ||= { linkage: [] }
42+
resource[:links][name][:linkage] += serializers.map { |serializer| { type: type, id: serializer.id.to_s } }
5043
end
5144

5245
def add_link(resource, name, serializer)
5346
resource[:links] ||= {}
54-
resource[:links][name] = nil
47+
resource[:links][name] = { linkage: nil }
5548

5649
if serializer && serializer.object
5750
type = serialized_object_type(serializer)
58-
if name.to_s == type || !type
59-
resource[:links][name] = serializer.id.to_s
60-
else
61-
resource[:links][name] ||= {}
62-
resource[:links][name][:type] = type
63-
resource[:links][name][:id] = serializer.id.to_s
64-
end
51+
52+
resource[:links][name][:linkage] = { type: type, id: serializer.id.to_s }
6553
end
6654
end
6755

test/action_controller/json_api_linked_test.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,22 +102,22 @@ def test_render_resource_with_nested_has_many_include
102102
"id" => "1",
103103
"name" => "Steve K.",
104104
"links" => {
105-
"posts" => [],
106-
"roles" => ["1", "2"],
107-
"bio" => nil
105+
"posts" => { "linkage" => [] },
106+
"roles" => { "linkage" => [{ "type" =>"roles", "id" => "1" }, { "type" =>"roles", "id" => "2" }] },
107+
"bio" => { "linkage" => nil }
108108
}
109109
}],
110110
"roles"=>[{
111111
"id" => "1",
112112
"name" => "admin",
113113
"links" => {
114-
"author" => "1"
114+
"author" => { "linkage" => { "type" =>"author", "id" => "1" } }
115115
}
116116
}, {
117117
"id" => "2",
118118
"name" => "colab",
119119
"links" => {
120-
"author" => "1"
120+
"author" => { "linkage" => { "type" =>"author", "id" => "1" } }
121121
}
122122
}]
123123
}

test/adapter/json_api/belongs_to_test.rb

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ def setup
3232
end
3333

3434
def test_includes_post_id
35-
assert_equal("42", @adapter.serializable_hash[:data][:links][:post])
35+
expected = { linkage: { type: "post", id: "42" } }
36+
37+
assert_equal(expected, @adapter.serializable_hash[:data][:links][:post])
3638
end
3739

3840
def test_includes_linked_post
@@ -42,9 +44,9 @@ def test_includes_linked_post
4244
title: 'New Post',
4345
body: 'Body',
4446
links: {
45-
comments: ["1"],
46-
blog: "999",
47-
author: "1"
47+
comments: { linkage: [ { type: "comments", id: "1" } ] },
48+
blog: { linkage: { type: "blog", id: "999" } },
49+
author: { linkage: { type: "author", id: "1" } }
4850
}
4951
}]
5052
assert_equal expected, @adapter.serializable_hash[:linked][:posts]
@@ -55,9 +57,9 @@ def test_limiting_linked_post_fields
5557
expected = [{
5658
title: 'New Post',
5759
links: {
58-
comments: ["1"],
59-
blog: "999",
60-
author: "1"
60+
comments: { linkage: [ { type: "comments", id: "1" } ] },
61+
blog: { linkage: { type: "blog", id: "999" } },
62+
author: { linkage: { type: "author", id: "1" } }
6163
}
6264
}]
6365
assert_equal expected, @adapter.serializable_hash[:linked][:posts]
@@ -67,7 +69,7 @@ def test_include_nil_author
6769
serializer = PostSerializer.new(@anonymous_post)
6870
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
6971

70-
assert_equal({comments: [], blog: "999", author: nil}, adapter.serializable_hash[:data][:links])
72+
assert_equal({comments: { linkage: [] }, blog: { linkage: { type: "blog", id: "999" } }, author: { linkage: nil }}, adapter.serializable_hash[:data][:links])
7173
end
7274

7375
def test_include_type_for_association_when_different_than_name
@@ -76,12 +78,22 @@ def test_include_type_for_association_when_different_than_name
7678
links = adapter.serializable_hash[:data][:links]
7779
expected = {
7880
writer: {
79-
type: "author",
80-
id: "1"
81+
linkage: {
82+
type: "author",
83+
id: "1"
84+
}
8185
},
8286
articles: {
83-
type: "posts",
84-
ids: ["42", "43"]
87+
linkage: [
88+
{
89+
type: "posts",
90+
id: "42"
91+
},
92+
{
93+
type: "posts",
94+
id: "43"
95+
}
96+
]
8597
}
8698
}
8799
assert_equal expected, links
@@ -96,28 +108,28 @@ def test_include_linked_resources_with_type_name
96108
id: "1",
97109
name: "Steve K.",
98110
links: {
99-
posts: [],
100-
roles: [],
101-
bio: nil
111+
posts: { linkage: [] },
112+
roles: { linkage: [] },
113+
bio: { linkage: nil }
102114
}
103115
}],
104116
posts: [{
105117
title: "New Post",
106118
body: "Body",
107119
id: "42",
108120
links: {
109-
comments: ["1"],
110-
blog: "999",
111-
author: "1"
121+
comments: { linkage: [ { type: "comments", id: "1" } ] },
122+
blog: { linkage: { type: "blog", id: "999" } },
123+
author: { linkage: { type: "author", id: "1" } }
112124
}
113125
}, {
114126
title: "Hello!!",
115127
body: "Hello, world!!",
116128
id: "43",
117129
links: {
118-
comments: [],
119-
blog: "999",
120-
author: nil
130+
comments: { linkage: [] },
131+
blog: { linkage: { type: "blog", id: "999" } },
132+
author: { linkage: nil }
121133
}
122134
}]
123135
}

test/adapter/json_api/collection_test.rb

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,55 @@ def setup
2525
end
2626

2727
def test_include_multiple_posts
28-
assert_equal([
29-
{ title: "Hello!!", body: "Hello, world!!", id: "1", links: { comments: [], blog: "999", author: "1" } },
30-
{ title: "New Post", body: "Body", id: "2", links: { comments: [], blog: "999", author: "1" } }
31-
], @adapter.serializable_hash[:data])
28+
expected = [
29+
{
30+
title: "Hello!!",
31+
body: "Hello, world!!",
32+
id: "1",
33+
links: {
34+
comments: { linkage: [] },
35+
blog: { linkage: { type: "blog", id: "999" } },
36+
author: { linkage: { type: "author", id: "1" } }
37+
}
38+
},
39+
{
40+
title: "New Post",
41+
body: "Body",
42+
id: "2",
43+
links: {
44+
comments: { linkage: [] },
45+
blog: { linkage: { type: "blog", id: "999" } },
46+
author: { linkage: { type: "author", id: "1" } }
47+
}
48+
}
49+
]
50+
51+
assert_equal(expected, @adapter.serializable_hash[:data])
3252
end
3353

3454
def test_limiting_fields
3555
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, fields: ['title'])
36-
assert_equal([
37-
{ title: "Hello!!", links: { comments: [], blog: "999", author: "1" } },
38-
{ title: "New Post", links: { comments: [], blog: "999", author: "1" } }
39-
], @adapter.serializable_hash[:data])
56+
57+
expected = [
58+
{
59+
title: "Hello!!",
60+
links: {
61+
comments: { linkage: [] },
62+
blog: { linkage: { type: "blog", id: "999" } },
63+
author: { linkage: { type: "author", id: "1" } }
64+
}
65+
},
66+
{
67+
title: "New Post",
68+
links: {
69+
comments: { linkage: [] },
70+
blog: { linkage: { type: "blog", id: "999" } },
71+
author: { linkage: { type: "author", id: "1" } }
72+
}
73+
}
74+
]
75+
76+
assert_equal(expected, @adapter.serializable_hash[:data])
4077
end
4178

4279
end

test/adapter/json_api/has_many_embed_ids_test.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ def setup
2525
end
2626

2727
def test_includes_comment_ids
28-
assert_equal(["1", "2"], @adapter.serializable_hash[:data][:links][:posts])
28+
expected = {:linkage=>[{:type=>"posts", :id=>"1"}, {:type=>"posts", :id=>"2"}]}
29+
30+
assert_equal(expected, @adapter.serializable_hash[:data][:links][:posts])
2931
end
3032

3133
def test_no_includes_linked_comments

test/adapter/json_api/has_many_explicit_serializer_test.rb

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,37 +29,70 @@ def setup
2929
end
3030

3131
def test_includes_comment_ids
32-
assert_equal(['1', '2'],
33-
@adapter.serializable_hash[:data][:links][:comments])
32+
expected = {
33+
linkage: [
34+
{ type: 'comments', id: '1' },
35+
{ type: 'comments', id: '2' }
36+
]
37+
}
38+
39+
assert_equal(expected, @adapter.serializable_hash[:data][:links][:comments])
3440
end
3541

3642
def test_includes_linked_comments
3743
# If CommentPreviewSerializer is applied correctly the body text will not be present in the output
38-
assert_equal([{ id: '1', links: { post: @post.id.to_s}},
39-
{ id: '2', links: { post: @post.id.to_s}}],
44+
expected = [
45+
{
46+
id: '1',
47+
links: {
48+
post: { linkage: { type: 'post', id: @post.id.to_s } }
49+
}
50+
},
51+
{
52+
id: '2',
53+
links: {
54+
post: { linkage: { type: 'post', id: @post.id.to_s } }
55+
}
56+
}
57+
]
58+
59+
assert_equal(expected,
4060
@adapter.serializable_hash[:linked][:comments])
4161
end
4262

4363
def test_includes_author_id
44-
assert_equal(@author.id.to_s,
45-
@adapter.serializable_hash[:data][:links][:author])
64+
expected = {
65+
linkage: { type: "author", id: @author.id.to_s }
66+
}
67+
68+
assert_equal(expected, @adapter.serializable_hash[:data][:links][:author])
4669
end
4770

4871
def test_includes_linked_authors
49-
assert_equal([{ id: @author.id.to_s, links: { posts: [@post.id.to_s] } }],
50-
@adapter.serializable_hash[:linked][:authors])
72+
expected = [{
73+
id: @author.id.to_s,
74+
links: {
75+
posts: { linkage: [ { type: "posts", id: @post.id.to_s } ] }
76+
}
77+
}]
78+
79+
assert_equal(expected, @adapter.serializable_hash[:linked][:authors])
5180
end
5281

5382
def test_explicit_serializer_with_null_resource
5483
@post.author = nil
55-
assert_equal(nil,
56-
@adapter.serializable_hash[:data][:links][:author])
84+
85+
expected = { linkage: nil }
86+
87+
assert_equal(expected, @adapter.serializable_hash[:data][:links][:author])
5788
end
5889

5990
def test_explicit_serializer_with_null_collection
6091
@post.comments = []
61-
assert_equal([],
62-
@adapter.serializable_hash[:data][:links][:comments])
92+
93+
expected = { linkage: [] }
94+
95+
assert_equal(expected, @adapter.serializable_hash[:data][:links][:comments])
6396
end
6497
end
6598
end

test/adapter/json_api/has_many_test.rb

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ def setup
3333
end
3434

3535
def test_includes_comment_ids
36-
assert_equal(["1", "2"], @adapter.serializable_hash[:data][:links][:comments])
36+
expected = { linkage: [ { type: "comments", id: "1" }, { type: "comments", id: "2" } ] }
37+
38+
assert_equal(expected, @adapter.serializable_hash[:data][:links][:comments])
3739
end
3840

3941
def test_includes_linked_comments
@@ -42,15 +44,15 @@ def test_includes_linked_comments
4244
id: "1",
4345
body: 'ZOMG A COMMENT',
4446
links: {
45-
post: "1",
46-
author: nil
47+
post: { linkage: { type: "post", id: "1" } },
48+
author: { linkage: nil }
4749
}
4850
}, {
4951
id: "2",
5052
body: 'ZOMG ANOTHER COMMENT',
5153
links: {
52-
post: "1",
53-
author: nil
54+
post: { linkage: { type: "post", id: "1" } },
55+
author: { linkage: nil }
5456
}
5557
}]
5658
assert_equal expected, @adapter.serializable_hash[:linked][:comments]
@@ -61,14 +63,14 @@ def test_limit_fields_of_linked_comments
6163
expected = [{
6264
id: "1",
6365
links: {
64-
post: "1",
65-
author: nil
66+
post: { linkage: { type: "post", id: "1" } },
67+
author: { linkage: nil }
6668
}
6769
}, {
6870
id: "2",
6971
links: {
70-
post: "1",
71-
author: nil
72+
post: { linkage: { type: "post", id: "1" } },
73+
author: { linkage: nil }
7274
}
7375
}]
7476
assert_equal expected, @adapter.serializable_hash[:linked][:comments]
@@ -86,8 +88,10 @@ def test_include_type_for_association_when_different_than_name
8688
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
8789
actual = adapter.serializable_hash[:data][:links][:articles]
8890
expected = {
89-
type: "posts",
90-
ids: ["1"]
91+
linkage: [{
92+
type: "posts",
93+
id: "1"
94+
}]
9195
}
9296
assert_equal expected, actual
9397
end

0 commit comments

Comments
 (0)