Module: ActiveModelSerializers::Jsonapi
- Defined in:
- lib/active_model_serializers/register_jsonapi_renderer.rb
Overview
Based on discussion in github.com/rails/rails/pull/23712#issuecomment-184977238, the JSON API media type will have its own format/renderer.
> We recommend the media type be registered on its own as jsonapi when a jsonapi Renderer and deserializer (Http::Parameters::DEFAULT_PARSERS) are added.
Usage:
ActiveSupport.on_load(:action_controller) do
require 'active_model_serializers/register_jsonapi_renderer'
end
And then in controllers, use `render jsonapi: model` rather than `render json: model, adapter: :json_api`.
For example, in a controller action, we can: respond_to do |format|
format.jsonapi { render jsonapi: model }
end
or
render jsonapi: model
No wrapper format needed as it does not apply (i.e. no `wrap_parameters format: [jsonapi]`)
Defined Under Namespace
Modules: ControllerSupport
Constant Summary
- MEDIA_TYPE =
'application/vnd.api+json'.freeze
- HEADERS =
{ response: { 'CONTENT_TYPE'.freeze => MEDIA_TYPE }, request: { 'ACCEPT'.freeze => MEDIA_TYPE } }.freeze
Class Method Summary (collapse)
- + (Object) install
-
+ (Object) parser
Proposal: should actually deserialize the JSON API params to the hash format expected by `ActiveModel::Serializers::JSON` actionpack/lib/action_dispatch/http/parameters.rb.
Class Method Details
+ (Object) install
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/active_model_serializers/register_jsonapi_renderer.rb', line 32 def self.install # actionpack/lib/action_dispatch/http/mime_types.rb Mime::Type.register MEDIA_TYPE, :jsonapi if Rails::VERSION::MAJOR >= 5 ActionDispatch::Request.parameter_parsers[:jsonapi] = parser else ActionDispatch::ParamsParser::DEFAULT_PARSERS[Mime[:jsonapi]] = parser end # ref https://github.com/rails/rails/pull/21496 ActionController::Renderers.add :jsonapi do |json, | json = serialize_jsonapi(json, ).to_json() unless json.is_a?(String) self.content_type ||= Mime[:jsonapi] self.response_body = json end end |
+ (Object) parser
Proposal: should actually deserialize the JSON API params to the hash format expected by `ActiveModel::Serializers::JSON` actionpack/lib/action_dispatch/http/parameters.rb
53 54 55 56 57 58 59 |
# File 'lib/active_model_serializers/register_jsonapi_renderer.rb', line 53 def self.parser lambda do |body| data = JSON.parse(body) data = { :_json => data } unless data.is_a?(Hash) data.with_indifferent_access end end |