diff --git a/lib/jsonapi/resource.rb b/lib/jsonapi/resource.rb index 14c34d417..af4ded28e 100644 --- a/lib/jsonapi/resource.rb +++ b/lib/jsonapi/resource.rb @@ -1040,7 +1040,7 @@ def cached_resources_for(records, serializer, options) cache_ids = pluck_arel_attributes(records, t[_primary_key], t[_cache_field]) resources = CachedResourceFragment.fetch_fragments(self, serializer, options[:context], cache_ids) else - resources = resources_for(records, options).map{|r| [r.id, r] }.to_h + resources = resources_for(records, options[:context]).map{|r| [r.id, r] }.to_h end preload_included_fragments(resources, records, serializer, options) diff --git a/test/controllers/controller_test.rb b/test/controllers/controller_test.rb index 83c0bdfac..38baf07c6 100644 --- a/test/controllers/controller_test.rb +++ b/test/controllers/controller_test.rb @@ -2451,6 +2451,12 @@ def test_destroy_relationship_has_and_belongs_to_many_refect ensure JSONAPI.configuration.use_relationship_reflection = false end + + def test_index_with_caching_enabled_uses_context + assert_cacheable_get :index + assert_response :success + assert json_response['data'][0]['attributes']['title'] = 'Title' + end end class Api::V5::AuthorsControllerTest < ActionController::TestCase diff --git a/test/fixtures/active_record.rb b/test/fixtures/active_record.rb index 778c72538..469e81f34 100644 --- a/test/fixtures/active_record.rb +++ b/test/fixtures/active_record.rb @@ -717,6 +717,9 @@ class BoatsController < JSONAPI::ResourceController end class BooksController < JSONAPI::ResourceController + def context + { title: 'Title' } + end end ### CONTROLLERS @@ -1246,7 +1249,13 @@ class AuthorResource < JSONAPI::Resource end class BookResource < JSONAPI::Resource + attribute :title + has_many :authors, class_name: 'Author', inverse_relationship: :books + + def title + context[:title] + end end class AuthorDetailResource < JSONAPI::Resource