mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
9b51ee9f92
When a `get` method called with `as: :json` and `params: nil` or `params: false` (explicitly or implicitly) `RequestEncoder#encode_params` converts it into a `null` or `false` value which includes a unexpected `null=` or `false` query string into request URL. From now on `RequestEncoder#encode_params` checks whether `params` is nil or not otherwise returns. Move down `nil` conversion guard Update CHANGELOG.md
55 lines
1.4 KiB
Ruby
55 lines
1.4 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module ActionDispatch
|
|
class RequestEncoder # :nodoc:
|
|
class IdentityEncoder
|
|
def content_type; end
|
|
def accept_header; end
|
|
def encode_params(params); params; end
|
|
def response_parser; -> body { body }; end
|
|
end
|
|
|
|
@encoders = { identity: IdentityEncoder.new }
|
|
|
|
attr_reader :response_parser
|
|
|
|
def initialize(mime_name, param_encoder, response_parser)
|
|
@mime = Mime[mime_name]
|
|
|
|
unless @mime
|
|
raise ArgumentError, "Can't register a request encoder for " \
|
|
"unregistered MIME Type: #{mime_name}. See `Mime::Type.register`."
|
|
end
|
|
|
|
@response_parser = response_parser || -> body { body }
|
|
@param_encoder = param_encoder || :"to_#{@mime.symbol}".to_proc
|
|
end
|
|
|
|
def content_type
|
|
@mime.to_s
|
|
end
|
|
|
|
def accept_header
|
|
@mime.to_s
|
|
end
|
|
|
|
def encode_params(params)
|
|
@param_encoder.call(params) if params
|
|
end
|
|
|
|
def self.parser(content_type)
|
|
mime = Mime::Type.lookup(content_type)
|
|
encoder(mime ? mime.ref : nil).response_parser
|
|
end
|
|
|
|
def self.encoder(name)
|
|
@encoders[name] || @encoders[:identity]
|
|
end
|
|
|
|
def self.register_encoder(mime_name, param_encoder: nil, response_parser: nil)
|
|
@encoders[mime_name] = new(mime_name, param_encoder, response_parser)
|
|
end
|
|
|
|
register_encoder :json, response_parser: -> body { JSON.parse(body) }
|
|
end
|
|
end
|