module ActiveModelSerializers::Jsonapi

Constants

HEADERS
MEDIA_TYPE

Public Class Methods

install() click to toggle source
# File lib/active_model_serializers/register_jsonapi_renderer.rb, line 35
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, options|
    json = serialize_jsonapi(json, options).to_json(options) unless json.is_a?(String)
    self.content_type ||= Mime[:jsonapi]
    self.response_body = json
  end
end
parser() click to toggle source

Proposal: should actually deserialize the JSON API params to the hash format expected by `ActiveModel::Serializers::JSON` actionpack/lib/action_dispatch/http/parameters.rb

# File lib/active_model_serializers/register_jsonapi_renderer.rb, line 56
def self.parser
  lambda do |body|
    data = JSON.parse(body)
    data = { _json: data } unless data.is_a?(Hash)
    data.with_indifferent_access
  end
end