Skip to content

Commit 5916014

Browse files
committed
Fix: resource object identifier with nil id excludes id
1 parent 92dde58 commit 5916014

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

lib/active_model_serializers/adapter/json_api/resource_identifier.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,11 @@ def initialize(serializer, options)
3838
end
3939

4040
def as_json
41-
return nil if id.blank?
42-
{ id: id, type: type }
41+
if id.blank?
42+
{ type: type }
43+
else
44+
{ id: id.to_s, type: type }
45+
end
4346
end
4447

4548
protected

test/adapter/json_api/type_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,13 @@ def test_id_defined_on_object
128128
assert_equal actual, expected
129129
end
130130

131+
def test_blank_id
132+
@model.id = nil
133+
actual = actual_resource_identifier_object(AuthorSerializer)
134+
expected = { type: expected_model_type }
135+
assert_equal actual, expected
136+
end
137+
131138
def test_id_defined_on_serializer
132139
actual = actual_resource_identifier_object(WithDefinedIdSerializer)
133140
expected = { id: 'special_id', type: expected_model_type }

0 commit comments

Comments
 (0)