Skip to content

Fix JSON fragments caching. #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/jsonapi/rails/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def register_renderers
ActiveSupport::Notifications.instrument('render.jsonapi-rails',
resources: resources,
options: options) do
renderer.render(resources, options, self).to_json
JSON.generate(renderer.render(resources, options, self))
end
end
end
Expand Down
34 changes: 34 additions & 0 deletions spec/render_jsonapi_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,38 @@ def index
expect(subject.key?('data')).to be true
end
end

context 'when using a cache' do
controller do
def serializer
Class.new(JSONAPI::Serializable::Resource) do
type 'users'
attribute :name

def jsonapi_cache_key(*)
'foo'
end
end
end

def index
user = OpenStruct.new(id: 1, name: 'Lucas')

render jsonapi: user,
class: { OpenStruct: serializer },
cache: Rails.cache
end
end

subject { JSON.parse(response.body) }

it 'renders a JSON API success document' do
get :index
expect(Rails.cache.exist?('foo')).to be true
get :index

expect(response.content_type).to eq('application/vnd.api+json')
expect(subject.key?('data')).to be true
end
end
end