diff --git a/lib/jsonapi/rails/railtie.rb b/lib/jsonapi/rails/railtie.rb index 9d083d8..f45b3ff 100644 --- a/lib/jsonapi/rails/railtie.rb +++ b/lib/jsonapi/rails/railtie.rb @@ -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 diff --git a/spec/render_jsonapi_spec.rb b/spec/render_jsonapi_spec.rb index 4cf9341..1c3cde1 100644 --- a/spec/render_jsonapi_spec.rb +++ b/spec/render_jsonapi_spec.rb @@ -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