Skip to content

Commit 2480985

Browse files
authored
Merge pull request #789 from togglepro/no-partial-linkage-data
avoid situation where the linkage data could be partially calculated
2 parents a799ee0 + ea8bb3c commit 2480985

1 file changed

Lines changed: 9 additions & 9 deletions

File tree

lib/jsonapi/resource_serializer.rb

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

355355
def to_one_linkage(source, relationship)
356-
linkage = {}
357356
linkage_id = foreign_key_value(source, relationship)
357+
linkage_type = format_key(relationship.type_for_source(source))
358+
return unless linkage_id.present? && linkage_type.present?
358359

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
360+
{
361+
type: linkage_type,
362+
id: linkage_id,
363+
}
366364
end
367365

368366
def to_many_linkage(source, relationship)
@@ -390,7 +388,9 @@ def to_many_linkage(source, relationship)
390388
end
391389

392390
linkage_types_and_values.each do |type, value|
393-
linkage.append({type: format_key(type), id: @id_formatter.format(value)})
391+
if type && value
392+
linkage.append({type: format_key(type), id: @id_formatter.format(value)})
393+
end
394394
end
395395
linkage
396396
end

0 commit comments

Comments
 (0)