Skip to content

Commit

Permalink
Merge pull request #2337 from InteNs/fix-json-api-belongs-to-fk-on-ob…
Browse files Browse the repository at this point in the history
…ject

fix incorrect belongs_to serialization when foreign_key on object and belongs_to is blank
  • Loading branch information
wasifhossain committed Jun 15, 2019
2 parents 339f99f + 22849c0 commit 777fab0
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/action_controller/json_api/linked_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 18 additions & 0 deletions test/adapter/json_api/relationship_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down
3 changes: 1 addition & 2 deletions test/adapter/json_api/type_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 777fab0

Please sign in to comment.