Skip to content

Commit

Permalink
Remove data from cached resource fragments (#1380)
Browse files Browse the repository at this point in the history
* Remove `data` from cached resource fragments

* Create a new hash using `transform_values`
  • Loading branch information
lgebhardt authored Feb 4, 2022
1 parent 5bb4605 commit 3fda2cf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/jsonapi/cached_response_fragment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ def initialize(resource_klass, id, type, context, fetchable_fields, relationship
@fetchable_fields = Set.new(fetchable_fields)

# Relationships left uncompiled because we'll often want to insert included ids on retrieval
@relationships = relationships

# Remove the data since that should not be cached
@relationships = relationships&.transform_values {|v| v.delete_if {|k, _v| k == 'data'} }
@links_json = CompiledJson.of(links_json)
@attributes_json = CompiledJson.of(attributes_json)
@meta_json = CompiledJson.of(meta_json)
Expand Down
16 changes: 16 additions & 0 deletions test/controllers/controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,22 @@ def test_index_filter_not_allowed
JSONAPI.configuration.allow_filter = true
end

def test_cached_result_does_not_include_relationship_data
JSONAPI.configuration.resource_cache = ActiveSupport::Cache::MemoryStore.new
JSONAPI.configuration.default_caching = true

get :show, params: {id: '1', include: 'author'}
assert_response :success
assert json_response['data']['relationships']['author']['data']

get :show, params: {id: '1'}
assert_response :success
refute json_response['data']['relationships']['author']['data']
ensure
JSONAPI.configuration.resource_cache = nil
JSONAPI.configuration.default_caching = false
end

def test_index_include_one_level_query_count
assert_query_count(4) do
assert_cacheable_get :index, params: {include: 'author'}
Expand Down

0 comments on commit 3fda2cf

Please sign in to comment.