return nil if id is nil

This commit is contained in:
Mark Havekes 2019-06-07 12:17:18 +02:00
parent 4b0c230d76
commit 28a172e66a
4 changed files with 6 additions and 4 deletions

View File

@ -55,7 +55,6 @@ module ActiveModelSerializers
else
association.reflection.type.to_s
end
# TODO: probably return nil if association.object is nil?
ResourceIdentifier.for_type_with_id(type, id, serializable_resource_options)
else
# TODO(BF): Process relationship without evaluating lazy_association

View File

@ -13,7 +13,7 @@ module ActiveModelSerializers
type = inflect_type(type)
type = type_for(:no_class_needed, type, options)
if id.blank?
{ type: type }
nil
else
{ id: id.to_s, type: type }
end

View File

@ -36,6 +36,7 @@ module ActiveModelSerializers
end
def test_relationship_with_nil_model_and_belongs_to_id_on_self
original_config = ActiveModelSerializers.config.jsonapi_use_foreign_key_on_belongs_to_relationship
ActiveModelSerializers.config.jsonapi_use_foreign_key_on_belongs_to_relationship = true
expected = { data: nil }
@ -46,7 +47,10 @@ module ActiveModelSerializers
actual = build_serializer_and_serialize_relationship(model, relationship_name) do
belongs_to :blog
end
assert_equal(expected, actual)
ensure
ActiveModelSerializers.config.jsonapi_use_foreign_key_on_belongs_to_relationship = original_config
end
def test_relationship_with_data_array

View File

@ -147,8 +147,7 @@ module ActiveModelSerializers
def test_for_type_with_id_given_blank_id
id = ''
actual = ResourceIdentifier.for_type_with_id('admin_user', id, {})
expected = { type: 'admin-users' }
assert_equal actual, expected
assert_nil actual
end
def test_for_type_with_id_inflected