Skip to content

Commit 28a172e

Browse files
committed
return nil if id is nil
1 parent 4b0c230 commit 28a172e

File tree

4 files changed

+6
-4
lines changed

4 files changed

+6
-4
lines changed

lib/active_model_serializers/adapter/json_api/relationship.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ def data_for_one(association)
5555
else
5656
association.reflection.type.to_s
5757
end
58-
# TODO: probably return nil if association.object is nil?
5958
ResourceIdentifier.for_type_with_id(type, id, serializable_resource_options)
6059
else
6160
# TODO(BF): Process relationship without evaluating lazy_association

lib/active_model_serializers/adapter/json_api/resource_identifier.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def self.for_type_with_id(type, id, options)
1313
type = inflect_type(type)
1414
type = type_for(:no_class_needed, type, options)
1515
if id.blank?
16-
{ type: type }
16+
nil
1717
else
1818
{ id: id.to_s, type: type }
1919
end

test/adapter/json_api/relationship_test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def test_relationship_with_nil_model
3636
end
3737

3838
def test_relationship_with_nil_model_and_belongs_to_id_on_self
39+
original_config = ActiveModelSerializers.config.jsonapi_use_foreign_key_on_belongs_to_relationship
3940
ActiveModelSerializers.config.jsonapi_use_foreign_key_on_belongs_to_relationship = true
4041

4142
expected = { data: nil }
@@ -46,7 +47,10 @@ def test_relationship_with_nil_model_and_belongs_to_id_on_self
4647
actual = build_serializer_and_serialize_relationship(model, relationship_name) do
4748
belongs_to :blog
4849
end
50+
4951
assert_equal(expected, actual)
52+
ensure
53+
ActiveModelSerializers.config.jsonapi_use_foreign_key_on_belongs_to_relationship = original_config
5054
end
5155

5256
def test_relationship_with_data_array

test/adapter/json_api/type_test.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,7 @@ def test_for_type_with_id
147147
def test_for_type_with_id_given_blank_id
148148
id = ''
149149
actual = ResourceIdentifier.for_type_with_id('admin_user', id, {})
150-
expected = { type: 'admin-users' }
151-
assert_equal actual, expected
150+
assert_nil actual
152151
end
153152

154153
def test_for_type_with_id_inflected

0 commit comments

Comments
 (0)