lib/grape/dsl/inside_route.rb in grape-1.6.2 vs lib/grape/dsl/inside_route.rb in grape-1.7.0

- old
+ new

@@ -1,8 +1,7 @@ # frozen_string_literal: true -require 'active_support/concern' require 'grape/dsl/headers' module Grape module DSL module InsideRoute @@ -27,11 +26,11 @@ # Methods which should not be available in filters until the before filter # has completed module PostBeforeFilter def declared(passed_params, options = {}, declared_params = nil, params_nested_path = []) - options = options.reverse_merge(include_missing: true, include_parent_namespaces: true) + options = options.reverse_merge(include_missing: true, include_parent_namespaces: true, evaluate_given: false) declared_params ||= optioned_declared_params(**options) if passed_params.is_a?(Array) declared_array(passed_params, options, declared_params, params_nested_path) else @@ -46,44 +45,49 @@ 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_attr, memo| + next if options[:evaluate_given] && !declared_param_attr.scope.attr_meets_dependency?(passed_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) + declared_hash_attr(passed_params, options, declared_param_attr.key, params_nested_path, memo) + end + end - rename_path = params_nested_path + [declared_parent_param.to_s] - renamed_param_name = renamed_params[rename_path] + def declared_hash_attr(passed_params, options, declared_param, params_nested_path, memo) + renamed_params = route_setting(:renamed_params) || {} + 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) - memo_key = optioned_param_key(renamed_param_name || declared_parent_param, options) - passed_children_params = passed_params[declared_parent_param] || passed_params.class.new + rename_path = params_nested_path + [declared_parent_param.to_s] + renamed_param_name = renamed_params[rename_path] - 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 + 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[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 - else - # If it is not a Hash then it does not have children. - # Find its value or set it to nil. - next unless options[:include_missing] || passed_params.key?(declared_param) + end + else + # If it is not a Hash then it does not have children. + # Find its value or set it to nil. + return unless options[:include_missing] || passed_params.key?(declared_param) - rename_path = params_nested_path + [declared_param.to_s] - renamed_param_name = renamed_params[rename_path] + rename_path = params_nested_path + [declared_param.to_s] + renamed_param_name = renamed_params[rename_path] - memo_key = optioned_param_key(renamed_param_name || declared_param, options) - passed_param = passed_params[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 - end + 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 end end end def handle_passed_param(params_nested_path, has_passed_children = false, &_block)