Skip to content

Commit 003be87

Browse files
committed
Support serializers with no attributes.
1 parent 9695ef1 commit 003be87

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

lib/jsonapi-serializers/serializer.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ def relationships
139139
end
140140

141141
def attributes
142+
return {} if self.class.attributes_map.nil?
142143
attributes = {}
143144
self.class.attributes_map.each do |attribute_name, attr_data|
144145
next if !should_include_attr?(attr_data[:options][:if], attr_data[:options][:unless])

spec/serializer_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,18 @@ def serialize_primary(object, options = {})
275275
})
276276
end
277277
end
278+
it 'can serialize primary data for an empty serializer with no attributes' do
279+
post = create(:post)
280+
primary_data = serialize_primary(post, {serializer: MyApp::EmptySerializer})
281+
expect(primary_data).to eq({
282+
'id' => '1',
283+
'type' => 'posts',
284+
'attributes' => {},
285+
'links' => {
286+
'self' => '/posts/1',
287+
},
288+
})
289+
end
278290
end
279291

280292
describe 'JSONAPI::Serializer.serialize' do

spec/support/serializers.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,8 @@ def hide_body?
9595
context.fetch(:hide_body, false)
9696
end
9797
end
98+
99+
class EmptySerializer
100+
include JSONAPI::Serializer
101+
end
98102
end

0 commit comments

Comments
 (0)