JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

Clearing polymorphic relationship by sending null data with PATCH is broken again

Open
#1,081 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
2.3k
Forks
546
PR merge metrics
No merged PRs in 30d

Description

The original issue #656 seems to have regressed, as the same error is now raised for a polymorphic association removal.

Started PATCH "/tags/830926030/relationships/taggable" for 127.0.0.1 at 2017-07-05 11:33:31 +0300
Processing by TagsController#update_relationship as HTML
  Parameters: {"data"=>nil, "relationship"=>"taggable", "tag_id"=>"830926030"}
Internal Server Error: undefined method `[]' for nil:NilClass /Users/vesa/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/jsonapi-resources-0.9.0/lib/jsonapi/request_parser.rb:594:in `parse_update_relationship_operation'
/Users/vesa/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/jsonapi-resources-0.9.0/lib/jsonapi/request_parser.rb:133:in `parse_modify_relationship_action'
/Users/vesa/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/jsonapi-resources-0.9.0/lib/jsonapi/request_parser.rb:98:in `setup_update_relationship_action'
/Users/vesa/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/jsonapi-resources-0.9.0/lib/jsonapi/request_parser.rb:38:in `setup_action'
/Users/vesa/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/jsonapi-resources-0.9.0/lib/jsonapi/request_parser.rb:27:in `initialize'
/Users/vesa/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/jsonapi-resources-0.9.0/lib/jsonapi/acts_as_resource_controller.rb:66:in `new'
/Users/vesa/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/jsonapi-resources-0.9.0/lib/jsonapi/acts_as_resource_controller.rb:66:in `process_request'
/Users/vesa/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/jsonapi-resources-0.9.0/lib/jsonapi/acts_as_resource_controller.rb:39:in `update_relationship'

The regression seems to stem from this commit a232243f5e1e4a24cd9bb87406c5a229326e7ea2 where operations were reworked to use an operation dispatcher and operation processors.

       if relationship.is_a?(JSONAPI::Relationship::ToOne)
         if relationship.polymorphic?
-          operation_args[1].merge!(
-            key_value: verified_params[:to_one].values[0] && verified_params[:to_one].values[0][:id],
-            key_type: verified_params[:to_one].values[0] && verified_params[:to_one].values[0][:type]
-          )
+          options[:key_value] = verified_params[:to_one].values[0][:id]
+          options[:key_type] = verified_params[:to_one].values[0][:type]

https://github.com/cerebris/jsonapi-resources/commit/a232243f5e1e4a24cd9bb87406c5a229326e7ea2#diff-a32d87f955d51d1e7270b1a974529f7eR585

I am not sure how to add tests to ensure this issue does not regress again. Any pointers on how you'd like to have such tests would be nice.

I discovered this issue as I'm adding special handling to jsonapi-authorization gem: https://github.com/venuu/jsonapi-authorization/pull/75

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in lib/jsonapi/request_parser.rb at parse_update_relationship_operation and compare the regression with issue #656 and commit a232243f5e1e4a24cd9bb87406c5a229326e7ea2. Reproduce the PATCH relationship request with a polymorphic association and null data; done means the removal no longer raises an undefined-method error and a regression test covers it.

Written by the indexing model from the issue text.

Assessment

Tech stack
rails, ruby
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.