Skip to content

Commit 4b0c230

Browse files
committed
add failing test and suggestion
1 parent 339f99f commit 4b0c230

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

lib/active_model_serializers/adapter/json_api/relationship.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ 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?
5859
ResourceIdentifier.for_type_with_id(type, id, serializable_resource_options)
5960
else
6061
# TODO(BF): Process relationship without evaluating lazy_association

test/adapter/json_api/relationship_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,20 @@ def test_relationship_with_nil_model
3535
assert_equal(expected, actual)
3636
end
3737

38+
def test_relationship_with_nil_model_and_belongs_to_id_on_self
39+
ActiveModelSerializers.config.jsonapi_use_foreign_key_on_belongs_to_relationship = true
40+
41+
expected = { data: nil }
42+
43+
model_attributes = { blog: nil }
44+
relationship_name = :blog
45+
model = new_model(model_attributes)
46+
actual = build_serializer_and_serialize_relationship(model, relationship_name) do
47+
belongs_to :blog
48+
end
49+
assert_equal(expected, actual)
50+
end
51+
3852
def test_relationship_with_data_array
3953
expected = {
4054
data: [

0 commit comments

Comments
 (0)