Polymorphic relations problem: undefined method for nil:NilClass
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 35/100
調査の方向性
ポリモーフィックな ContactMedium、Individual、Organization リソースを使用して、組み込まれた BugTest、特に test_get_party_via_contact_medium を再現する。lib/jsonapi/relationship.rb:66 と lib/jsonapi/active_relation/join_manager.rb:93 から開始する。GET /contact_media/:id/party リクエストがアサーションどおり正常に返されれば完了とする。
索引モデルが issue の本文から書いたものです。
説明
This issue is a (choose one):
- Problem/bug report.
- Feature request.
- Request for support. Note: Please try to avoid submitting issues for support requests. Use Gitter instead.
Checklist before submitting:
- I've searched for an existing issue.
- I've asked my question on Gitter and have not received a satisfactory answer.
- I've included a complete bug report template. This step helps us and allows us to see the bug without trying to reproduce the problem from your description. It helps you because you will frequently detect if it's a problem specific to your project.
- The feature I'm asking for is compliant with the JSON:API spec.
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
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
- 主要言語
- Ruby
- スター
- 2.3k
- フォーク
- 546
- PR マージ指標
- 30日以内にマージされた PR はありません
コントリビューションガイド
このリポジトリのコントリビューションガイドは索引されていません
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
JSONAPI-Resources/jsonapi-resources のほかの issue
-
難易度 1/5 1時間未満 初心者へのやさしさ 85/100
-
難易度 3/5 1〜2日 初心者へのやさしさ 50/100
JSONAPI-Resources/jsonapi-resources#1488 · コメント 1 件 ·
-
難易度 4/5 3〜5日 初心者へのやさしさ 42/100
JSONAPI-Resources/jsonapi-resources#1479 · コメント 2 件 ·
-
難易度 4/5 3〜5日 初心者へのやさしさ 30/100
JSONAPI-Resources/jsonapi-resources#1477 · コメント 3 件 · リアクション 1 件 ·
-
難易度 3/5 1〜2日 初心者へのやさしさ 45/100
JSONAPI-Resources/jsonapi-resources の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
-
bug
難易度 1/5 1時間未満 初心者へのやさしさ 90/100
riscv/riscv-unified-db#2626 ·
-
Component: GLib
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
-
ds-drift
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
we-promise/sure#3693 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
simp/pupmod-simp-simp#395 ·