mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Add Rails >= 5.0.beta3 JSON API params parsing (#1751)
This reverts commit 6288203277.
This commit is contained in:
@@ -22,43 +22,54 @@
|
||||
# render jsonapi: model
|
||||
#
|
||||
# No wrapper format needed as it does not apply (i.e. no `wrap_parameters format: [jsonapi]`)
|
||||
|
||||
module ActiveModelSerializers::Jsonapi
|
||||
MEDIA_TYPE = 'application/vnd.api+json'.freeze
|
||||
HEADERS = {
|
||||
response: { 'CONTENT_TYPE'.freeze => MEDIA_TYPE },
|
||||
request: { 'ACCEPT'.freeze => MEDIA_TYPE }
|
||||
}.freeze
|
||||
|
||||
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
|
||||
|
||||
# Proposal: should actually deserialize the JSON API params
|
||||
# to the hash format expected by `ActiveModel::Serializers::JSON`
|
||||
# actionpack/lib/action_dispatch/http/parameters.rb
|
||||
def self.parser
|
||||
lambda do |body|
|
||||
data = JSON.parse(body)
|
||||
data = { :_json => data } unless data.is_a?(Hash)
|
||||
data.with_indifferent_access
|
||||
end
|
||||
end
|
||||
|
||||
module ControllerSupport
|
||||
def serialize_jsonapi(json, options)
|
||||
options[:adapter] = :json_api
|
||||
options.fetch(:serialization_context) { options[:serialization_context] = ActiveModelSerializers::SerializationContext.new(request) }
|
||||
options.fetch(:serialization_context) do
|
||||
options[:serialization_context] = ActiveModelSerializers::SerializationContext.new(request)
|
||||
end
|
||||
get_serializer(json, options)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# actionpack/lib/action_dispatch/http/mime_types.rb
|
||||
Mime::Type.register ActiveModelSerializers::Jsonapi::MEDIA_TYPE, :jsonapi
|
||||
|
||||
parsers = Rails::VERSION::MAJOR >= 5 ? ActionDispatch::Http::Parameters : ActionDispatch::ParamsParser
|
||||
media_type = Mime::Type.lookup(ActiveModelSerializers::Jsonapi::MEDIA_TYPE)
|
||||
|
||||
# Proposal: should actually deserialize the JSON API params
|
||||
# to the hash format expected by `ActiveModel::Serializers::JSON`
|
||||
# actionpack/lib/action_dispatch/http/parameters.rb
|
||||
parsers::DEFAULT_PARSERS[media_type] = lambda do |body|
|
||||
data = JSON.parse(body)
|
||||
data = { :_json => data } unless data.is_a?(Hash)
|
||||
data.with_indifferent_access
|
||||
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 ||= media_type
|
||||
self.response_body = json
|
||||
end
|
||||
ActiveModelSerializers::Jsonapi.install
|
||||
|
||||
ActiveSupport.on_load(:action_controller) do
|
||||
include ActiveModelSerializers::Jsonapi::ControllerSupport
|
||||
|
||||
Reference in New Issue
Block a user