diff --git a/CHANGELOG.md b/CHANGELOG.md index ff86d3458..5fa42f9a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,8 @@ Fixes: - Fix Rails 6.0 deprication warnings - update test fixture schema to use `timestamps` instead of `timestamp` - [#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) +- [#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) + - Fixes incorrect json-api generation when `jsonapi_use_foreign_key_on_belongs_to_relationship` is `true` and the relationship is blank Misc: diff --git a/lib/active_model_serializers/adapter/json_api/resource_identifier.rb b/lib/active_model_serializers/adapter/json_api/resource_identifier.rb index 8aee0a7d2..e321914ea 100644 --- a/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +++ b/lib/active_model_serializers/adapter/json_api/resource_identifier.rb @@ -13,7 +13,7 @@ def self.for_type_with_id(type, id, options) 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 diff --git a/test/action_controller/json_api/linked_test.rb b/test/action_controller/json_api/linked_test.rb index 3e38e1129..69d8974d7 100644 --- a/test/action_controller/json_api/linked_test.rb +++ b/test/action_controller/json_api/linked_test.rb @@ -82,7 +82,7 @@ def render_collection_without_include def render_collection_with_include setup_post - render json: [@post], adapter: :json_api, include: 'author, comments' + render json: [@post], adapter: :json_api, include: 'author,comments' end end diff --git a/test/adapter/json_api/relationship_test.rb b/test/adapter/json_api/relationship_test.rb index 35069537d..38a67fe22 100644 --- a/test/adapter/json_api/relationship_test.rb +++ b/test/adapter/json_api/relationship_test.rb @@ -35,6 +35,24 @@ def test_relationship_with_nil_model assert_equal(expected, actual) 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 } + + model_attributes = { blog: nil } + relationship_name = :blog + model = new_model(model_attributes) + 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 expected = { data: [ diff --git a/test/adapter/json_api/type_test.rb b/test/adapter/json_api/type_test.rb index 8a7aaa280..56f7efcbc 100644 --- a/test/adapter/json_api/type_test.rb +++ b/test/adapter/json_api/type_test.rb @@ -147,8 +147,7 @@ def test_for_type_with_id 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