diff --git a/lib/jsonapi/cached_resource_fragment.rb b/lib/jsonapi/cached_resource_fragment.rb index 4a6d598d4..4431fddf3 100644 --- a/lib/jsonapi/cached_resource_fragment.rb +++ b/lib/jsonapi/cached_resource_fragment.rb @@ -65,7 +65,8 @@ def to_cache_value end def to_real_resource - rs = Resource.resource_klass_for(self.type).find_by_keys([self.id], {context: self.context}) + rs_klass = @resource_klass || Resource.resource_klass_for(self.type) + rs = rs_klass.find_by_keys([self.id], {context: self.context}) return rs.try(:first) end diff --git a/test/controllers/controller_test.rb b/test/controllers/controller_test.rb index 262acc1d3..b1739957f 100644 --- a/test/controllers/controller_test.rb +++ b/test/controllers/controller_test.rb @@ -3520,6 +3520,21 @@ def test_show_relationship end end +class Api::V1::PicturesControllerTest < ActionController::TestCase + def test_pictures_index + assert_cacheable_get :index + assert_response :success + assert_equal 3, json_response['data'].size + end + + def test_pictures_index_with_polymorphic_include_one_level + assert_cacheable_get :index, params: {include: 'imageable'} + assert_response :success + assert_equal 3, json_response['data'].size + assert_equal 2, json_response['included'].size + end +end + class CarsControllerTest < ActionController::TestCase def setup JSONAPI.configuration.json_key_format = :camelized_key diff --git a/test/fixtures/active_record.rb b/test/fixtures/active_record.rb index 39a8b37fe..b5b00196e 100644 --- a/test/fixtures/active_record.rb +++ b/test/fixtures/active_record.rb @@ -833,6 +833,9 @@ def context class LikesController < JSONAPI::ResourceController end + + class PicturesController < JSONAPI::ResourceController + end end module V2 @@ -1492,6 +1495,10 @@ class HairCutResource < HairCutResource; end class VehicleResource < VehicleResource; end class CarResource < CarResource; end class BoatResource < BoatResource; end + class ImageableResource < ImageableResource; end + class PictureResource < PictureResource; end + class ProductResource < ProductResource; end + class DocumentResource < DocumentResource; end end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 01ae9b38c..867e794ff 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -281,6 +281,7 @@ class CatResource < JSONAPI::Resource jsonapi_resources :craters jsonapi_resources :preferences jsonapi_resources :likes + jsonapi_resources :pictures end JSONAPI.configuration.route_format = :underscored_route