Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

bug with inclusion of polymorphic association

未关闭
#1,105 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
25/100
Issue 类型
缺陷
描述清晰度
需要澄清
活跃度
停滞
技术栈
rails, ruby, sqlite
领域
api, backend, database

调研方向

运行嵌入的 Minitest 复现,并比较 test_patch_linkage_options_with_android 与 test_fetch_option_linked_with_android。通过 JSONAPI 控制器和路由跟踪 OptionResource 的多态 optionable 关系。完成的标准是两个请求都成功返回,并且能够按照测试的预期链接和包含 optionable 关联。

由索引模型根据 Issue 内容生成。

描述

Priority: High Target: 0.10 Type: Bug

reproduced with gist

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', platform: :mri

  gem 'activerecord-jdbcsqlite3-adapter',
      git: 'https://github.com/jruby/activerecord-jdbc-adapter',
      branch: 'rails-5',
      platform: :jruby

  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 :options, force: true do |t|
    t.integer :optionable_id
    t.string :optionable_type
    t.boolean :enabled, default: false, null: false
  end

  create_table :androids, force: true do |t|
    t.string :version_name
  end
end

# create models
class Option < ActiveRecord::Base
  belongs_to :optionable, polymorphic: true, required: false
end

class Android < ActiveRecord::Base
  has_one :option, as: :optionable
end

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

class ApplicationController < ActionController::Base
end

# prepare jsonapi resources and controllers
class OptionsController < ApplicationController
  include JSONAPI::ActsAsResourceController
end

class OptionResource < JSONAPI::Resource
  attribute :enabled
  has_one :optionable, polymorphic: true, class_name: 'Android'
end

class AndroidResource < JSONAPI::Resource
  attribute :version_name
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
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 :options, only: [:show, :update]
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 test_patch_linkage_options_with_android
    android = Android.create! version_name: '1.0'
    option = Option.create!
    json_request = {
        data: {
            id: option.id.to_s,
            type: 'options',
            relationships: {
                optionable: {
                    data: {
                        id: android.id.to_s,
                        type: 'androids'
                    }
                }
            }
        }
    }
    patch "/options/#{option.id}?include=optionable", json_request.to_json, json_api_headers
    assert last_response.ok?
    assert android, option.reload.optionable
  end

  def test_fetch_option_linked_with_android
    android = Android.create! version_name: '1.0'
    option = Option.create! optionable: android
    get "/options/#{option.id}?include=optionable", nil, json_api_headers
    assert last_response.ok?
  end

  private

  def app
    Rails.application
  end
end
主要语言
Ruby
星标
2.3k
派生
546
PR 合并指标
30 天内没有已合并 PR

贡献指南

这个仓库没有索引到贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

JSONAPI-Resources/jsonapi-resources 的其他 Issue

查看 JSONAPI-Resources/jsonapi-resources 的全部 Issue

相似的 Issue

更多 Ruby Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。