Skip to content

Commit 777fab0

Browse files
authored
Merge pull request #2337 from InteNs/fix-json-api-belongs-to-fk-on-object
fix incorrect belongs_to serialization when foreign_key on object and belongs_to is blank
2 parents 339f99f + 22849c0 commit 777fab0

File tree

5 files changed

+23
-4
lines changed

5 files changed

+23
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Fixes:
1515
- Fix Rails 6.0 deprication warnings
1616
- update test fixture schema to use `timestamps` instead of `timestamp`
1717
- [#2223](https://github.com/rails-api/active_model_serializers/pull/2223) Support Fieldset in Attributes/JSON adapters documented in [docs/general/fields.md](https://github.com/rails-api/active_model_serializers/blob/0-10-stable/docs/general/fields.md) that worked partially before (@bf4)
18+
- [#2337](https://github.com/rails-api/active_model_serializers/pull/2337) fix incorrect belongs_to serialization when foreign_key on object and belongs_to is blank (@InteNs)
19+
- Fixes incorrect json-api generation when `jsonapi_use_foreign_key_on_belongs_to_relationship` is `true` and the relationship is blank
1820

1921
Misc:
2022

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/action_controller/json_api/linked_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def render_collection_without_include
8282

8383
def render_collection_with_include
8484
setup_post
85-
render json: [@post], adapter: :json_api, include: 'author, comments'
85+
render json: [@post], adapter: :json_api, include: 'author,comments'
8686
end
8787
end
8888

test/adapter/json_api/relationship_test.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,24 @@ 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+
original_config = ActiveModelSerializers.config.jsonapi_use_foreign_key_on_belongs_to_relationship
40+
ActiveModelSerializers.config.jsonapi_use_foreign_key_on_belongs_to_relationship = true
41+
42+
expected = { data: nil }
43+
44+
model_attributes = { blog: nil }
45+
relationship_name = :blog
46+
model = new_model(model_attributes)
47+
actual = build_serializer_and_serialize_relationship(model, relationship_name) do
48+
belongs_to :blog
49+
end
50+
51+
assert_equal(expected, actual)
52+
ensure
53+
ActiveModelSerializers.config.jsonapi_use_foreign_key_on_belongs_to_relationship = original_config
54+
end
55+
3856
def test_relationship_with_data_array
3957
expected = {
4058
data: [

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)