Skip to content

Commit d8078b3

Browse files
authored
Merge pull request jsonapi-rb#70 from jsonapi-rb/json-caching
Fix JSON fragments caching.
2 parents 11b6fbe + 0603acf commit d8078b3

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

lib/jsonapi/rails/railtie.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def register_renderers
5757
ActiveSupport::Notifications.instrument('render.jsonapi-rails',
5858
resources: resources,
5959
options: options) do
60-
renderer.render(resources, options, self).to_json
60+
JSON.generate(renderer.render(resources, options, self))
6161
end
6262
end
6363
end

spec/render_jsonapi_spec.rb

+34
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,38 @@ def index
1717
expect(subject.key?('data')).to be true
1818
end
1919
end
20+
21+
context 'when using a cache' do
22+
controller do
23+
def serializer
24+
Class.new(JSONAPI::Serializable::Resource) do
25+
type 'users'
26+
attribute :name
27+
28+
def jsonapi_cache_key(*)
29+
'foo'
30+
end
31+
end
32+
end
33+
34+
def index
35+
user = OpenStruct.new(id: 1, name: 'Lucas')
36+
37+
render jsonapi: user,
38+
class: { OpenStruct: serializer },
39+
cache: Rails.cache
40+
end
41+
end
42+
43+
subject { JSON.parse(response.body) }
44+
45+
it 'renders a JSON API success document' do
46+
get :index
47+
expect(Rails.cache.exist?('foo')).to be true
48+
get :index
49+
50+
expect(response.content_type).to eq('application/vnd.api+json')
51+
expect(subject.key?('data')).to be true
52+
end
53+
end
2054
end

0 commit comments

Comments
 (0)