Non datastore, dynamic related resources broken in version `v0.9.0`
@lgebhardt đang làm issue này rồi.
Từ ngày 10/7/2018.
Đánh giá
Issue này chưa được đánh giá.
Mô tả
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
It seems that dynamic related resources, a feature we use heavily here at Blue Apron, is broken coming from v0.8.0 to v0.9.0. It looks like in the older version, related resources which don't provide a foreign key in the owner model resolved by calling a method on that model with the related resource's name. This same behavior is part of the active record convention:
https://github.com/cerebris/jsonapi-resources/blob/v0.8.0/lib/jsonapi/resource_serializer.rb#L301
However in the new version (v0.9.0), this functionality is no longer supported in the ToOne relationship child class:
https://github.com/cerebris/jsonapi-resources/blob/854ec705183d744565106cf34b12a741139d157e/lib/jsonapi/relationship.rb#L89
Instead, relationships are based on the assumption that related resources are datastore backed, and use foreign keys rather than methods to relate to one another.
The below test passes when specifying the older version of the library, and fails when specifying the newer version. The example is stripped down (and thus a little contrived), but I assure you one can support JSONAPI compliant resources using this method.
Bug reports:
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
# this test works at v0.8.0, but fails at v0.9.0
# gem 'jsonapi-resources', git: 'https://github.com/cerebris/jsonapi-resources', :tag => 'v0.8.0', require: false
gem 'jsonapi-resources', git: 'https://github.com/cerebris/jsonapi-resources', :tag => 'v0.9.0', 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 :your_models, force: true do |t|
t.string :name
end
end
# create models
class YourModel < ActiveRecord::Base
def my_related_model
MyRelatedModel.new
end
end
class MyRelatedModel
include ActiveModel::Model
attr_accessor :id, :data
def initialize
@id = 1
@data = 'snoopy'
end
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 YourModelsController < ApplicationController
include JSONAPI::ActsAsResourceController
end
class YourModelResource < JSONAPI::Resource
attribute :name
filter :name
has_one :my_related_model, eager_load_on_include: false
end
class MyRelatedModelResource < JSONAPI::Resource
attribute :data
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 :your_models, only: [:index, :create]
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_index_your_models
record = YourModel.create! name: 'John Doe'
get '/your_models', {include: :my_related_model}, json_api_headers
assert last_response.ok?
json_response = JSON.parse(last_response.body)
end
private
def app
Rails.application
end
end
- Ngôn ngữ chính
- Ruby
- Star
- 2.3k
- Fork
- 546
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
Chúng tôi chưa kiểm tra các tệp thiết lập môi trường của dự án này. Hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của JSONAPI-Resources/jsonapi-resources
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 85/100
-
ArgumentError with Rails 8.1Đang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 50/100
JSONAPI-Resources/jsonapi-resources#1488 · 1 bình luận ·
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 42/100
JSONAPI-Resources/jsonapi-resources#1479 · 2 bình luận ·
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 30/100
JSONAPI-Resources/jsonapi-resources#1477 · 3 bình luận · 1 reaction ·
-
Fields filtering with includeĐang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 45/100
Tất cả issue của JSONAPI-Resources/jsonapi-resources
Issue tương tự
-
security
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Maintainer thường phản hồi trong vòng 1 ngày
-
OSCON 2016Đang mởcontent
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
rubyevents/rubyevents#2148 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
we-promise/sure#3838 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Mend: dependency security vulnerability
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
ManageIQ/manageiq-ui-classic#10341 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Obsolete link to RubyMine docsĐang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
rubocop/ruby-style-guide#984 ·