Skip to content

Commit 87bffef

Browse files
committed
Exclude links without any data
1 parent a182618 commit 87bffef

File tree

3 files changed

+4
-1
lines changed

3 files changed

+4
-1
lines changed

lib/active_model_serializers/adapter/json_api.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,8 @@ def relationships_for(serializer, requested_associations, options)
464464
# }.reject! {|_,v| v.nil? }
465465
def links_for(serializer)
466466
serializer._links.each_with_object({}) do |(name, value), hash|
467-
hash[name] = Link.new(serializer, value).as_json
467+
result = Link.new(serializer, value).as_json
468+
hash[name] = result if result
468469
end
469470
end
470471

lib/active_model_serializers/adapter/json_api/link.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ def as_json
7171
hash[:href] = @href if defined?(@href)
7272
hash[:meta] = @meta if defined?(@meta)
7373

74+
return nil if hash.empty?
7475
hash
7576
end
7677

test/adapter/json_api/links_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class LinkAuthorSerializer < ActiveModel::Serializer
1717
link :yet_another do
1818
"http://example.com/resource/#{object.id}"
1919
end
20+
link(:nil) { nil }
2021
end
2122

2223
def setup

0 commit comments

Comments
 (0)