cerebris / jsonapi-resources

A resource-focused Rails library for developing JSON:API compliant servers.
http://jsonapi-resources.com
MIT License
2.32k stars 532 forks source link

Polymorphic relations problem: undefined method for nil:NilClass #1305

Open turtleman opened 4 years ago

turtleman commented 4 years ago

This issue is a (choose one):

Checklist before submitting:

Description

I seem to have bumped into a problem with polymorphic relations I'm unable to solve. Here's a description and a standalone bug report describing it. I'm on JR 0.10.2, Rails 6.0.2.1 and Ruby 2.6.5.

The model in question is composed of three ActiveRecord models: ContactMedium, Individuals and Organizations and the corresponding JR resources. The latter may have many contact media as party and one contact medium can belong to either kind of party.

The problem comes up when trying to get the individual or organisation as party via the contact media like this: GET /contact-media/1/party

The logic works on ActiveRecord level but not in the JR level.

Using the relationship results in the following error in the included example case:

Internal Server Error: undefined method `downcase' for nil:NilClass
.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/bundler/gems/jsonapi-resources-978f590f85fe/lib/jsonapi/relationship.rb:66:in 
`block (3 levels) in polymorphic_types'

The error is similar in the actual app but with a mild difference (that also included in the included pastebin link):

Internal Server Error: undefined method `left' for nil:NilClass
.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/jsonapi-resources-0.10.2/lib/jsonapi/active_relation/join_manager.rb:93:in 
`alias_from_arel_node'`

I'm suspecting this behaviour to be a bug. If I'm doing something wrong instead, please let me know. Workaround suggestions also welcome!

Bug reports:

Partial report in pastebin

https://pastebin.com/V0CfEs3F

Standalone bug report using the template

begin
  require 'bundler/inline'
  require 'bundler'
rescue LoadError => e
  STDERR.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
  raise e
end

gemfile(true, ui: ENV['SILENT'] ? Bundler::UI::Silent.new : Bundler::UI::Shell.new) do
  source 'https://rubygems.org'

  gem 'rails', require: false
  gem 'sqlite3'
  gem 'i18n', '= 1.7.0'

  if ENV['JSONAPI_RESOURCES_PATH']
    gem 'jsonapi-resources', path: ENV['JSONAPI_RESOURCES_PATH'], require: false
  else
    gem 'jsonapi-resources', git: 'https://github.com/cerebris/jsonapi-resources', require: false
  end

end

# prepare active_record database
require 'active_record'

class NullLogger < Logger
  def initialize(*_args)
  end

  def add(*_args, &_block)
  end
end

ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = ENV['SILENT'] ? NullLogger.new : Logger.new(STDOUT)
ActiveRecord::Migration.verbose = !ENV['SILENT']

ActiveRecord::Schema.define do
  # Add your schema here
  create_table :contact_media do |t|
    t.string :name
    t.references :party, polymorphic: true, index: true
  end

  create_table :individuals do |t|
    t.string :name
  end

  create_table :organizations do |t|
    t.string :name
  end
end

# create models
 class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true
end

class ContactMedium < ApplicationRecord
  belongs_to :party, polymorphic: true, inverse_of: :contact_media
end

class Individual < ApplicationRecord
  has_many :contact_media, as: :party
end

class Organization < ApplicationRecord
  has_many :contact_media, as: :party
end

# prepare rails app
require 'action_controller/railtie'
# require 'action_view/railtie'
require 'jsonapi-resources'

class ApplicationController < ActionController::Base
  protect_from_forgery with: :null_session
end

# prepare jsonapi resources and controllers
#

class IndividualsController < ApplicationController
  include JSONAPI::ActsAsResourceController
end

class OrganizationsController < ApplicationController
  include JSONAPI::ActsAsResourceController
end

class ContactMediaController < ApplicationController
  include JSONAPI::ActsAsResourceController
end

class PartiesController < ApplicationController
  include JSONAPI::ActsAsResourceController
end

class ContactMediumResource < JSONAPI::Resource
  attribute :name
  has_one :party, polymorphic: true
end

class IndividualResource < JSONAPI::Resource
  attribute :name
  has_many :contact_media
end

class OrganizationResource < JSONAPI::Resource
  attribute :name
  has_many :contact_media
end

class PartyResource < JSONAPI::Resource
end

class TestApp < Rails::Application
  config.root = File.dirname(__FILE__)
  config.logger = ENV['SILENT'] ? NullLogger.new : Logger.new(STDOUT)
  Rails.logger = config.logger

  secrets.secret_token = 'secret_token'
  secrets.secret_key_base = 'secret_key_base'

  config.eager_load = false
  config.hosts << "example.org"
end

# initialize app
Rails.application.initialize!

JSONAPI.configure do |config|
  config.json_key_format = :underscored_key
  config.route_format = :underscored_key
end

# draw routes
Rails.application.routes.draw do
  jsonapi_resources :contact_media do
    jsonapi_relationships
  end
  jsonapi_resources :individuals do
    jsonapi_relationships
  end
  jsonapi_resources :organizations do
    jsonapi_relationships
  end
end

# prepare tests
require 'minitest/autorun'
require 'rack/test'

# Replace this with the code necessary to make your test fail.
class BugTest < Minitest::Test
  include Rack::Test::Methods

  def json_api_headers
    {'Accept' => JSONAPI::MEDIA_TYPE, 'CONTENT_TYPE' => JSONAPI::MEDIA_TYPE}
  end

  def teardown
    Individual.delete_all
    ContactMedium.delete_all
  end

  def test_find_party_via_contact_medium
    individual = Individual.create(name: 'test')
    contact_medium = ContactMedium.create(party: individual, name: 'test contact medium')
    fetched_party = contact_medium.party
    assert_same individual, fetched_party, "Expect an individual to have been found via contact medium model's relationship 'party'"
  end

  def test_get_individual
    individual = Individual.create(name: 'test')
    ContactMedium.create(party: individual, name: 'test contact medium')
    get "/individuals/#{individual.id}"
    assert last_response.ok?
  end

  def test_get_party_via_contact_medium
    individual = Individual.create(name: 'test')
    contact_medium = ContactMedium.create(party: individual, name: 'test contact medium')
    get "/contact_media/#{contact_medium.id}/party"
    assert last_response.ok?, "Expect an individual to have been found via contact medium resource's relationship 'party'"
  end

  private

  def app
    Rails.application
  end
end
bf4 commented 3 years ago

Oh wow, this is great. I just commented on where this behavior was imho introduced, in https://github.com/cerebris/jsonapi-resources/pull/1045#discussion_r542982208 but you actually wrote a test case!

I think is similar to

historically related

rafahuaman commented 3 years ago

Are there any updates on this issue?

lgebhardt commented 3 years ago

@rafahuaman For now I've taken @bf4's #1349 pr and made a few changes in a test branch, https://github.com/cerebris/jsonapi-resources/compare/bf4_fix_polymorphic_relations_lookup?expand=1, including a workaround that requires additional relationships on the model to help resolve each supported polymorphic type. Like

class ContactMedium < TestApplicationRecord
  belongs_to :party, polymorphic: true, inverse_of: :contact_media
  belongs_to :individual, -> { where( contact_media: { party_type: 'Individual' } ).eager_load( :contact_media ) }, foreign_key: 'party_id'
  belongs_to :organization, -> { where( contact_media: { party_type: 'Organization' } ).eager_load( :contact_media ) }, foreign_key: 'party_id'
end

I'm going to work on finding a solution that does not need this type of hack.

I believe the hack could work with the latest releases if you also specify the polymorphic_types on the resource relationship:

class ContactMediumResource < JSONAPI::Resource
  attribute :name
  has_one :party, polymorphic: true, polymorphic_types: ['individual', 'organization']
end
bf4 commented 3 years ago

Oh, that's good news. I never got to te heart of it

On Wed, Mar 10, 2021, 5:09 PM Larry Gebhardt @.***> wrote:

@rafahuaman https://github.com/rafahuaman For now I've taken @bf4 https://github.com/bf4's #1349 https://github.com/cerebris/jsonapi-resources/pull/1349 pr and made a few changes in a test branch, https://github.com/cerebris/jsonapi-resources/tree/bf4_fix_polymorphic_relations_lookup, including a work around that requires additional relationships on the model to help resolve each supported polymorphic type. Like

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/cerebris/jsonapi-resources/issues/1305#issuecomment-796270954, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABC4QQXR2HD2D3CWJM362DTC7UYVANCNFSM4KMBNQKQ .

rafahuaman commented 3 years ago

@lgebhardt Thank you for the reply I am going to try out the workaround and report back.

dcantu96 commented 3 years ago

Hello, what is the status on this issue?

dcantu96 commented 3 years ago

i needed a fix asap and nothing worked except for this

def _replace_polymorphic_to_one_link(relationship_type, key_value, key_type, _options)
    relationship = self.class._relationships[relationship_type.to_sym]

    send("#{relationship.foreign_key}=", type: key_type.to_s.capitalize, id: key_value) # <--- i just set type capitalized and voila
    @save_needed = true

    :completed
  end
Alexander-Senko commented 2 years ago
Internal Server Error: undefined method `downcase' for nil:NilClass
.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/bundler/gems/jsonapi-resources-978f590f85fe/lib/jsonapi/relationship.rb:66:in 
`block (3 levels) in polymorphic_types'

I get this because there could be singleton classes in the ObjectSpace that lack a name. Just need to filter them out:

ObjectSpace.each_object
    .select { Class === _1 }
    .select { _1 < ActiveRecord::Base }
    .select(&:name)
valscion commented 2 years ago

Looks like jsonapi-authorization's support for polymorphic association will be blocked on this issue, as I'm hitting this same case when I try to update the gem to support JR 0.10.x: