Skip to content

Commit a6c6979

Browse files
committed
Merge pull request #1516 from groyoh/fix_relationship_href
[FIX] bug displaying nil for relationship link href
2 parents 2b67363 + 3cbc054 commit a6c6979

File tree

3 files changed

+45
-23
lines changed

3 files changed

+45
-23
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Features:
1414
- [#1340](https://github.com/rails-api/active_model_serializers/pull/1340) Add support for resource-level meta. (@beauby)
1515

1616
Fixes:
17+
- [#1516](https://github.com/rails-api/active_model_serializers/pull/1501) No longer return a nil href when only
18+
adding meta to a relationship link. (@groyoh)
1719
- [#1458](https://github.com/rails-api/active_model_serializers/pull/1458) Preserve the serializer
1820
type when fragment caching. (@bdmac)
1921
- [#1477](https://github.com/rails-api/active_model_serializers/pull/1477) Fix `fragment_cached?`

lib/active_model/serializer/adapter/json_api/link.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ def meta(value)
2828
def as_json
2929
return @value if @value
3030

31-
hash = { href: @href }
32-
hash.merge!(meta: @meta) if @meta
31+
hash = {}
32+
hash[:href] = @href if @href
33+
hash[:meta] = @meta if @meta
3334

3435
hash
3536
end

test/adapter/json_api/relationship_test.rb

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,25 @@ class RelationshipAuthorSerializer < ActiveModel::Serializer
1919

2020
has_many :locations do
2121
link :related do
22-
ids = object.locations.map!(&:id).join(',')
22+
ids = object.locations.map(&:id).join(',')
2323
href "//example.com/locations/#{ids}"
2424
end
2525
end
2626

2727
has_many :posts do
2828
link :related do
29-
ids = object.posts.map!(&:id).join(',')
29+
ids = object.posts.map(&:id).join(',')
3030
href "//example.com/posts/#{ids}"
3131
meta ids: ids
3232
end
3333
end
3434

35+
has_many :comments do
36+
link :self do
37+
meta ids: [1]
38+
end
39+
end
40+
3541
has_many :roles do
3642
meta count: object.posts.count
3743
end
@@ -48,7 +54,7 @@ class RelationshipAuthorSerializer < ActiveModel::Serializer
4854

4955
has_many :likes do
5056
link :related do
51-
ids = object.likes.map!(&:id).join(',')
57+
ids = object.likes.map(&:id).join(',')
5258
href "//example.com/likes/#{ids}"
5359
meta ids: ids
5460
end
@@ -65,6 +71,7 @@ def setup
6571
@profile = Profile.new(id: 1337)
6672
@location = Location.new(id: 1337)
6773
@reviewer = Author.new(id: 1337)
74+
@comment = Comment.new(id: 1337)
6875
@author = RelationshipAuthor.new(
6976
id: 1337,
7077
posts: [@post],
@@ -74,12 +81,12 @@ def setup
7481
likes: [@like],
7582
roles: [@role],
7683
locations: [@location],
77-
profile: @profile
84+
profile: @profile,
85+
comments: [@comment]
7886
)
7987
end
8088

8189
def test_relationship_simple_link
82-
hash = serializable(@author, adapter: :json_api).serializable_hash
8390
expected = {
8491
data: {
8592
id: '1337',
@@ -89,31 +96,28 @@ def test_relationship_simple_link
8996
self: '//example.com/link_author/relationships/bio'
9097
}
9198
}
92-
assert_equal(expected, hash[:data][:relationships][:bio])
99+
assert_relationship(:bio, expected)
93100
end
94101

95102
def test_relationship_block_link
96-
hash = serializable(@author, adapter: :json_api).serializable_hash
97103
expected = {
98104
data: { id: '1337', type: 'profiles' },
99105
links: { related: '//example.com/profiles/1337' }
100106
}
101-
assert_equal(expected, hash[:data][:relationships][:profile])
107+
assert_relationship(:profile, expected)
102108
end
103109

104110
def test_relationship_block_link_href
105-
hash = serializable(@author, adapter: :json_api).serializable_hash
106111
expected = {
107112
data: [{ id: '1337', type: 'locations' }],
108113
links: {
109114
related: { href: '//example.com/locations/1337' }
110115
}
111116
}
112-
assert_equal(expected, hash[:data][:relationships][:locations])
117+
assert_relationship(:locations, expected)
113118
end
114119

115-
def test_relationship_block_link_meta
116-
hash = serializable(@author, adapter: :json_api).serializable_hash
120+
def test_relationship_block_link_href_and_meta
117121
expected = {
118122
data: [{ id: '1337', type: 'posts' }],
119123
links: {
@@ -123,37 +127,45 @@ def test_relationship_block_link_meta
123127
}
124128
}
125129
}
126-
assert_equal(expected, hash[:data][:relationships][:posts])
130+
assert_relationship(:posts, expected)
131+
end
132+
133+
def test_relationship_block_link_meta
134+
expected = {
135+
data: [{ id: '1337', type: 'comments' }],
136+
links: {
137+
self: {
138+
meta: { ids: [1] }
139+
}
140+
}
141+
}
142+
assert_relationship(:comments, expected)
127143
end
128144

129145
def test_relationship_meta
130-
hash = serializable(@author, adapter: :json_api).serializable_hash
131146
expected = {
132147
data: [{ id: '1337', type: 'roles' }],
133148
meta: { count: 1 }
134149
}
135-
assert_equal(expected, hash[:data][:relationships][:roles])
150+
assert_relationship(:roles, expected)
136151
end
137152

138153
def test_relationship_not_including_data
139-
hash = serializable(@author, adapter: :json_api).serializable_hash
140154
expected = {
141155
links: { self: '//example.com/link_author/relationships/blog' }
142156
}
143-
assert_equal(expected, hash[:data][:relationships][:blog])
157+
assert_relationship(:blog, expected)
144158
end
145159

146160
def test_relationship_including_data_explicit
147-
hash = serializable(@author, adapter: :json_api).serializable_hash
148161
expected = {
149162
data: { id: '1337', type: 'authors' },
150163
meta: { name: 'Dan Brown' }
151164
}
152-
assert_equal(expected, hash[:data][:relationships][:reviewer])
165+
assert_relationship(:reviewer, expected)
153166
end
154167

155168
def test_relationship_with_everything
156-
hash = serializable(@author, adapter: :json_api).serializable_hash
157169
expected = {
158170
data: [{ id: '1337', type: 'likes' }],
159171
links: {
@@ -164,7 +176,14 @@ def test_relationship_with_everything
164176
},
165177
meta: { liked: true }
166178
}
167-
assert_equal(expected, hash[:data][:relationships][:likes])
179+
assert_relationship(:likes, expected)
180+
end
181+
182+
private
183+
184+
def assert_relationship(relationship_name, expected)
185+
hash = serializable(@author, adapter: :json_api).serializable_hash
186+
assert_equal(expected, hash[:data][:relationships][relationship_name])
168187
end
169188
end
170189
end

0 commit comments

Comments
 (0)