mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
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:
commit
777fab04ae
@ -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:
|
||||
|
||||
|
||||
@ -13,7 +13,7 @@ module ActiveModelSerializers
|
||||
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
|
||||
|
||||
@ -82,7 +82,7 @@ module ActionController
|
||||
|
||||
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
|
||||
|
||||
|
||||
@ -35,6 +35,24 @@ module ActiveModelSerializers
|
||||
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: [
|
||||
|
||||
@ -147,8 +147,7 @@ module ActiveModelSerializers
|
||||
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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user