Skip to content

Commit 0603acf

Browse files
beaubydawidof
authored andcommitted
Fix JSON fragments caching.
1 parent c99bdec commit 0603acf

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

Diff for: lib/jsonapi/rails/railtie.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def register_renderers
5454
ActiveSupport::Notifications.instrument('render.jsonapi-rails',
5555
resources: resources,
5656
options: options) do
57-
renderer.render(resources, options, self).to_json
57+
JSON.generate(renderer.render(resources, options, self))
5858
end
5959
end
6060
end

Diff for: 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)