diff --git a/lib/jsonapi/cached_response_fragment.rb b/lib/jsonapi/cached_response_fragment.rb index 4f2abccdb..b1038921f 100644 --- a/lib/jsonapi/cached_response_fragment.rb +++ b/lib/jsonapi/cached_response_fragment.rb @@ -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) diff --git a/test/controllers/controller_test.rb b/test/controllers/controller_test.rb index e815a0e24..3b27f96a5 100644 --- a/test/controllers/controller_test.rb +++ b/test/controllers/controller_test.rb @@ -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'}