Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix incorrect belongs_to serialization when foreign_key on object and belongs_to is blank #2337

Merged

Conversation

InteNs
Copy link

@InteNs InteNs commented Jun 4, 2019

Changes

ResourceIdentifier will return nil instead of a hash when the association foreign_key is blank
and jsonapi_use_foreign_key_on_belongs_to_relationship = true

Caveats

- locks jsonapi-renderer to < 0.2.1 untill jsonapi-rb/jsonapi-renderer#38 gets solved

Related GitHub issues

related #2335

Additional helpful information

@InteNs InteNs changed the base branch from master to 0-10-stable June 4, 2019 08:28
@InteNs InteNs changed the title #### Purpose #### fix incorrect belongs_to serialization when foreign_key on object and belongs_to is blank Jun 4, 2019
@InteNs InteNs changed the title #### fix incorrect belongs_to serialization when foreign_key on object and belongs_to is blank fix incorrect belongs_to serialization when foreign_key on object and belongs_to is blank Jun 4, 2019
@InteNs
Copy link
Author

InteNs commented Jun 4, 2019

Had a quick try but can't get rake test to run:

LoadError: cannot load such file -- yard
/Users/intens/Projects/Fabriquartz/active_model_serializers/Rakefile:16:in `require'

@InteNs
Copy link
Author

InteNs commented Jun 7, 2019

is this up for discussion? (it's blocking the PR)

lib/active_model_serializers/adapter/json_api/relationship.rb:47:9: C: Metrics/PerceivedComplexity: Perceived complexity for data_for_one is too high. [10/9]
        def data_for_one(association)
        ^^^

@wasifhossain
Copy link
Member

wasifhossain commented Jun 9, 2019

I think it should be enough to make this test pass according to json_api specification.

def test_for_type_with_id_given_blank_id
  id = ''
  actual = ResourceIdentifier.for_type_with_id('admin_user', id, {})
  assert_nil actual
end

In order to do that, we simply need to return nil instead of { type: type } when id is blank in ResourceIdentifier.for_type_with_id

I tested it locally and all tests ran successfully along with the rubocop linter.

@InteNs
Copy link
Author

InteNs commented Jun 11, 2019

@wasifhossain looks good, are you going to make a PR?

@wasifhossain
Copy link
Member

wasifhossain commented Jun 11, 2019

@InteNs I think you are better equipped to test my proposed changes in your codebase and if you find it working, then take your time to update this PR. After that, we will take another look and merge if none of us find any issues with it

@InteNs InteNs force-pushed the fix-json-api-belongs-to-fk-on-object branch from 1528241 to 41f699f Compare June 11, 2019 11:26
@InteNs
Copy link
Author

InteNs commented Jun 11, 2019

aye, will test my fork in our staging environment

@InteNs InteNs force-pushed the fix-json-api-belongs-to-fk-on-object branch from 41f699f to 28a172e Compare June 11, 2019 12:42
@InteNs
Copy link
Author

InteNs commented Jun 11, 2019

tested this on staging over here and it's working fine.

@wasifhossain
Copy link
Member

wasifhossain commented Jun 11, 2019

great to know 🙌 thanks for your help!

unfortunately we are having this issue with jsonapi-renderer at the moment. Lets see if there is any quick fix and decide later how should we move on afterwards.

@InteNs
Copy link
Author

InteNs commented Jun 11, 2019

we are experiencing the same issue and downgraded the gem to 0.2.0

@wasifhossain
Copy link
Member

wasifhossain commented Jun 11, 2019

I also think we can restrict the jsonapi-renderer version to 0.2.0 until this PR jsonapi-rb/jsonapi-renderer#38 gets merged, which is supposed to fix the current failure.

@InteNs can you please make a small tweak in active_model_serializers.gemspec#L45 to update the upper bound to < 0.2.1

spec.add_runtime_dependency 'jsonapi-renderer', ['>= 0.1.1.beta1', '< 0.2.1']

@wasifhossain
Copy link
Member

@bf4 do you have any other suggestions? thanks

@InteNs
Copy link
Author

InteNs commented Jun 12, 2019

the failing travis run seems to be a problem outside of this PR?

Also, since nothing has been released since february, Can I expect there to be a release soon after this merge?

@wasifhossain
Copy link
Member

i agree with you @InteNs on travis failed case (looks to be rvm issue). btw @bf4 might have plans to release a new version some time soon

@wasifhossain
Copy link
Member

@InteNs can you put a changelog entry to summarize the change this PR would bring? thanks!

@dawidof
Copy link

dawidof commented Jun 13, 2019

I also think we can restrict the jsonapi-renderer version to 0.2.0 until this PR jsonapi-rb/jsonapi-renderer#38 gets merged, which is supposed to fix the current failure.

@InteNs can you please make a small tweak in active_model_serializers.gemspec#L45 to update the upper bound to < 0.2.1

spec.add_runtime_dependency 'jsonapi-renderer', ['>= 0.1.1.beta1', '< 0.2.1']

jsonapi-rb/jsonapi-renderer#38 has been merged and jsonapi-renderer updated to 0.2.2. Try new version.

@wasifhossain
Copy link
Member

wasifhossain commented Jun 13, 2019

looks like jsonapi-renderer is back to normal state 👏 , with a tiny thing still need to be fixed on our side to make the test suite pass.

let's remove that single space from 'author, comments' in the include option in test/action_controller/json_api/linked_test.rb

render json: [@post], adapter: :json_api, include: 'author,comments'

Ref: Include Doc

@@ -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'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Member

@wasifhossain wasifhossain Jun 14, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note for future reference:

this change belongs to jsonapi-rb/jsonapi-renderer#38 only, irrelevant of this PR. we need to make this tweak according to the include doc in order to make the test suite pass.

@wasifhossain
Copy link
Member

all tests are green again 🙌 I think we can merge it now

@wasifhossain wasifhossain merged commit 777fab0 into rails-api:0-10-stable Jun 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants