Skip to content

Commit 3fda2cf

Browse files
authored
Remove data from cached resource fragments (#1380)
* Remove `data` from cached resource fragments * Create a new hash using `transform_values`
1 parent 5bb4605 commit 3fda2cf

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

lib/jsonapi/cached_response_fragment.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ def initialize(resource_klass, id, type, context, fetchable_fields, relationship
5151
@fetchable_fields = Set.new(fetchable_fields)
5252

5353
# Relationships left uncompiled because we'll often want to insert included ids on retrieval
54-
@relationships = relationships
55-
54+
# Remove the data since that should not be cached
55+
@relationships = relationships&.transform_values {|v| v.delete_if {|k, _v| k == 'data'} }
5656
@links_json = CompiledJson.of(links_json)
5757
@attributes_json = CompiledJson.of(attributes_json)
5858
@meta_json = CompiledJson.of(meta_json)

test/controllers/controller_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,22 @@ def test_index_filter_not_allowed
304304
JSONAPI.configuration.allow_filter = true
305305
end
306306

307+
def test_cached_result_does_not_include_relationship_data
308+
JSONAPI.configuration.resource_cache = ActiveSupport::Cache::MemoryStore.new
309+
JSONAPI.configuration.default_caching = true
310+
311+
get :show, params: {id: '1', include: 'author'}
312+
assert_response :success
313+
assert json_response['data']['relationships']['author']['data']
314+
315+
get :show, params: {id: '1'}
316+
assert_response :success
317+
refute json_response['data']['relationships']['author']['data']
318+
ensure
319+
JSONAPI.configuration.resource_cache = nil
320+
JSONAPI.configuration.default_caching = false
321+
end
322+
307323
def test_index_include_one_level_query_count
308324
assert_query_count(4) do
309325
assert_cacheable_get :index, params: {include: 'author'}

0 commit comments

Comments
 (0)