lib/grape/dsl/inside_route.rb in grape-1.5.3 vs lib/grape/dsl/inside_route.rb in grape-1.6.0
- old
+ new
@@ -46,34 +46,39 @@
declared(passed_param || {}, options, declared_params, params_nested_path)
end
end
def declared_hash(passed_params, options, declared_params, params_nested_path)
+ renamed_params = route_setting(:renamed_params) || {}
+
declared_params.each_with_object(passed_params.class.new) do |declared_param, memo|
if declared_param.is_a?(Hash)
declared_param.each_pair do |declared_parent_param, declared_children_params|
params_nested_path_dup = params_nested_path.dup
params_nested_path_dup << declared_parent_param.to_s
next unless options[:include_missing] || passed_params.key?(declared_parent_param)
+ rename_path = params_nested_path + [declared_parent_param.to_s]
+ renamed_param_name = renamed_params[rename_path]
+
+ memo_key = optioned_param_key(renamed_param_name || declared_parent_param, options)
passed_children_params = passed_params[declared_parent_param] || passed_params.class.new
- memo_key = optioned_param_key(declared_parent_param, options)
memo[memo_key] = handle_passed_param(params_nested_path_dup, passed_children_params.any?) do
declared(passed_children_params, options, declared_children_params, params_nested_path_dup)
end
end
else
# If it is not a Hash then it does not have children.
# Find its value or set it to nil.
- has_renaming = route_setting(:renamed_params) && route_setting(:renamed_params).find { |current| current[declared_param] }
- param_renaming = has_renaming[declared_param] if has_renaming
+ next unless options[:include_missing] || passed_params.key?(declared_param)
- next unless options[:include_missing] || passed_params.key?(declared_param) || (param_renaming && passed_params.key?(param_renaming))
+ rename_path = params_nested_path + [declared_param.to_s]
+ renamed_param_name = renamed_params[rename_path]
- memo_key = optioned_param_key(param_renaming || declared_param, options)
- passed_param = passed_params[param_renaming || declared_param]
+ memo_key = optioned_param_key(renamed_param_name || declared_param, options)
+ passed_param = passed_params[declared_param]
params_nested_path_dup = params_nested_path.dup
params_nested_path_dup << declared_param.to_s
memo[memo_key] = passed_param || handle_passed_param(params_nested_path_dup) do
passed_param
@@ -84,19 +89,19 @@
def handle_passed_param(params_nested_path, has_passed_children = false, &_block)
return yield if has_passed_children
key = params_nested_path[0]
- key += '[' + params_nested_path[1..-1].join('][') + ']' if params_nested_path.size > 1
+ key += "[#{params_nested_path[1..-1].join('][')}]" if params_nested_path.size > 1
route_options_params = options[:route_options][:params] || {}
type = route_options_params.dig(key, :type)
has_children = route_options_params.keys.any? { |k| k != key && k.start_with?(key) }
if type == 'Hash' && !has_children
{}
- elsif type == 'Array' || type&.start_with?('[') && !type&.include?(',')
+ elsif type == 'Array' || (type&.start_with?('[') && !type&.include?(','))
[]
elsif type == 'Set' || type&.start_with?('#<Set')
Set.new
else
yield
@@ -115,10 +120,11 @@
# Declared params at current namespace
namespace_stackable(:declared_params).last || []
end
raise ArgumentError, 'Tried to filter for declared parameters but none exist.' unless declared_params
+
declared_params
end
end
# A filtering method that will return a hash
@@ -185,15 +191,17 @@
# @param status [Integer] The HTTP Status Code to return for this request.
def status(status = nil)
case status
when Symbol
raise ArgumentError, "Status code :#{status} is invalid." unless Rack::Utils::SYMBOL_TO_STATUS_CODE.key?(status)
+
@status = Rack::Utils.status_code(status)
when Integer
@status = status
when nil
return @status if instance_variable_defined?(:@status) && @status
+
case request.request_method.to_s.upcase
when Grape::Http::Headers::POST
201
when Grape::Http::Headers::DELETE
if instance_variable_defined?(:@body) && @body.present?
@@ -367,9 +375,10 @@
if key
representation = (body || {}).merge(key => representation)
elsif entity_class.present? && body
raise ArgumentError, "Representation of type #{representation.class} cannot be merged." unless representation.respond_to?(:merge)
+
representation = body.merge(representation)
end
body representation
end