Fix: resource object identifier with nil id excludes id

This commit is contained in:
Benjamin Fleischer 2017-10-31 14:34:30 -05:00
parent 92dde58f5f
commit 5916014b48
2 changed files with 12 additions and 2 deletions

View File

@ -38,8 +38,11 @@ module ActiveModelSerializers
end end
def as_json def as_json
return nil if id.blank? if id.blank?
{ id: id, type: type } { type: type }
else
{ id: id.to_s, type: type }
end
end end
protected protected

View File

@ -128,6 +128,13 @@ module ActiveModelSerializers
assert_equal actual, expected assert_equal actual, expected
end end
def test_blank_id
@model.id = nil
actual = actual_resource_identifier_object(AuthorSerializer)
expected = { type: expected_model_type }
assert_equal actual, expected
end
def test_id_defined_on_serializer def test_id_defined_on_serializer
actual = actual_resource_identifier_object(WithDefinedIdSerializer) actual = actual_resource_identifier_object(WithDefinedIdSerializer)
expected = { id: 'special_id', type: expected_model_type } expected = { id: 'special_id', type: expected_model_type }