Skip to content

Commit 004e0dc

Browse files
authored
Merge pull request #2119 from bf4/exclude_empty_relationships
Return null resource object identifier for blank id
2 parents afe0183 + 73eae19 commit 004e0dc

File tree

3 files changed

+32
-18
lines changed

3 files changed

+32
-18
lines changed

lib/active_model_serializers/adapter/json_api.rb

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -295,20 +295,8 @@ def attributes_for(serializer, fields)
295295

296296
# {http://jsonapi.org/format/#document-resource-objects Document Resource Objects}
297297
def resource_object_for(serializer, include_slice = {})
298-
resource_object = serializer.fetch(self) do
299-
resource_object = ResourceIdentifier.new(serializer, instance_options).as_json
300-
301-
requested_fields = fieldset && fieldset.fields_for(resource_object[:type])
302-
attributes = attributes_for(serializer, requested_fields)
303-
resource_object[:attributes] = attributes if attributes.any?
304-
resource_object
305-
end
306-
307-
requested_associations = fieldset.fields_for(resource_object[:type]) || '*'
308-
relationships = relationships_for(serializer, requested_associations, include_slice)
309-
resource_object[:relationships] = relationships if relationships.any?
298+
resource_object = data_for(serializer, include_slice)
310299

311-
links = links_for(serializer)
312300
# toplevel_links
313301
# definition:
314302
# allOf
@@ -322,7 +310,10 @@ def resource_object_for(serializer, include_slice = {})
322310
# prs:
323311
# https://github.com/rails-api/active_model_serializers/pull/1247
324312
# https://github.com/rails-api/active_model_serializers/pull/1018
325-
resource_object[:links] = links if links.any?
313+
if (links = links_for(serializer)).any?
314+
resource_object ||= {}
315+
resource_object[:links] = links
316+
end
326317

327318
# toplevel_meta
328319
# alias meta
@@ -332,12 +323,33 @@ def resource_object_for(serializer, include_slice = {})
332323
# {
333324
# :'git-ref' => 'abc123'
334325
# }
335-
meta = meta_for(serializer)
336-
resource_object[:meta] = meta unless meta.blank?
326+
if (meta = meta_for(serializer)).present?
327+
resource_object ||= {}
328+
resource_object[:meta] = meta
329+
end
337330

338331
resource_object
339332
end
340333

334+
def data_for(serializer, include_slice)
335+
data = serializer.fetch(self) do
336+
resource_object = ResourceIdentifier.new(serializer, instance_options).as_json
337+
break nil if resource_object.nil?
338+
339+
requested_fields = fieldset && fieldset.fields_for(resource_object[:type])
340+
attributes = attributes_for(serializer, requested_fields)
341+
resource_object[:attributes] = attributes if attributes.any?
342+
resource_object
343+
end
344+
data.tap do |resource_object|
345+
next if resource_object.nil?
346+
# NOTE(BF): the attributes are cached above, separately from the relationships, below.
347+
requested_associations = fieldset.fields_for(resource_object[:type]) || '*'
348+
relationships = relationships_for(serializer, requested_associations, include_slice)
349+
resource_object[:relationships] = relationships if relationships.any?
350+
end
351+
end
352+
341353
# {http://jsonapi.org/format/#document-resource-object-relationships Document Resource Object Relationship}
342354
# relationships
343355
# definition:

lib/active_model_serializers/adapter/json_api/resource_identifier.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def self.type_for(class_name, serializer_type = nil, transform_options = {})
2323
end
2424

2525
def self.for_type_with_id(type, id, options)
26+
return nil if id.blank?
2627
{
2728
id: id.to_s,
2829
type: type_for(:no_class_needed, type, options)
@@ -36,6 +37,7 @@ def initialize(serializer, options)
3637
end
3738

3839
def as_json
40+
return nil if id.blank?
3941
{ id: id, type: type }
4042
end
4143

test/action_controller/adapter_selector_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def render_using_default_adapter
1919
end
2020

2121
def render_using_adapter_override
22-
@profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
22+
@profile = Profile.new(id: 'render_using_adapter_override', name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
2323
render json: @profile, adapter: :json_api
2424
end
2525

@@ -41,7 +41,7 @@ def test_render_using_adapter_override
4141

4242
expected = {
4343
data: {
44-
id: @controller.instance_variable_get(:@profile).id.to_s,
44+
id: 'render_using_adapter_override',
4545
type: 'profiles',
4646
attributes: {
4747
name: 'Name 1',

0 commit comments

Comments
 (0)