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
This commit is contained in:
Wasif Hossain 2019-06-15 14:50:12 +06:00 committed by GitHub
commit 777fab04ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 23 additions and 4 deletions

View File

@ -15,6 +15,8 @@ Fixes:
- Fix Rails 6.0 deprication warnings - Fix Rails 6.0 deprication warnings
- update test fixture schema to use `timestamps` instead of `timestamp` - 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) - [#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: Misc:

View File

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

View File

@ -82,7 +82,7 @@ module ActionController
def render_collection_with_include def render_collection_with_include
setup_post setup_post
render json: [@post], adapter: :json_api, include: 'author, comments' render json: [@post], adapter: :json_api, include: 'author,comments'
end end
end end

View File

@ -35,6 +35,24 @@ module ActiveModelSerializers
assert_equal(expected, actual) assert_equal(expected, actual)
end 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 def test_relationship_with_data_array
expected = { expected = {
data: [ data: [

View File

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