Skip to content

Commit 49c8bc6

Browse files
committed
avoid situation where the linkage data could be partially calculated
1 parent 95027f4 commit 49c8bc6

1 file changed

Lines changed: 9 additions & 11 deletions

File tree

lib/jsonapi/resource_serializer.rb

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -353,16 +353,12 @@ def related_link(source, relationship)
353353
end
354354

355355
def to_one_linkage(source, relationship)
356-
linkage = {}
357-
linkage_id = foreign_key_value(source, relationship)
358-
359-
if linkage_id
360-
linkage[:type] = format_key(relationship.type_for_source(source))
361-
linkage[:id] = linkage_id
362-
else
363-
linkage = nil
364-
end
365-
linkage
356+
return unless linkage_id = foreign_key_value(source, relationship)
357+
return unless linkage_type = format_key(relationship.type_for_source(source))
358+
{
359+
type: linkage_type,
360+
id: linkage_id,
361+
}
366362
end
367363

368364
def to_many_linkage(source, relationship)
@@ -390,7 +386,9 @@ def to_many_linkage(source, relationship)
390386
end
391387

392388
linkage_types_and_values.each do |type, value|
393-
linkage.append({type: format_key(type), id: @id_formatter.format(value)})
389+
if type && value
390+
linkage.append({type: format_key(type), id: @id_formatter.format(value)})
391+
end
394392
end
395393
linkage
396394
end

0 commit comments

Comments
 (0)