diff --git a/CHANGELOG.md b/CHANGELOG.md index d753236ca..2c73d74b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ * [#2629](https://github.com/ruby-grape/grape/pull/2629): Refactor Router Architecture - [@ericproulx](https://github.com/ericproulx). * [#2633](https://github.com/ruby-grape/grape/pull/2633): Refactor API::Instance and reorganize DSL modules - [@ericproulx](https://github.com/ericproulx). * [#2636](https://github.com/ruby-grape/grape/pull/2636): Refactor router to simplify method signatures and reduce duplication - [@ericproulx](https://github.com/ericproulx). +* [#2637](https://github.com/ruby-grape/grape/pull/2637): Refactor declared method - [@ericproulx](https://github.com/ericproulx). * [#2639](https://github.com/ruby-grape/grape/pull/2639): Refactor mime_types_for - [@ericproulx](https://github.com/ericproulx). * [#2638](https://github.com/ruby-grape/grape/pull/2638): Remove unnecessary path string duplication - [@ericproulx](https://github.com/ericproulx). * Your contribution here. diff --git a/lib/grape/dsl/declared.rb b/lib/grape/dsl/declared.rb new file mode 100644 index 000000000..bd18af33e --- /dev/null +++ b/lib/grape/dsl/declared.rb @@ -0,0 +1,134 @@ +# frozen_string_literal: true + +module Grape + module DSL + module Declared + # Denotes a situation where a DSL method has been invoked in a + # filter which it should not yet be available in + class MethodNotYetAvailable < StandardError + def initialize(msg = '#declared is not available prior to parameter validation') + super + end + end + + # A filtering method that will return a hash + # consisting only of keys that have been declared by a + # `params` statement against the current/target endpoint or parent + # namespaces. + # @param params [Hash] The initial hash to filter. Usually this will just be `params` + # @param options [Hash] Can pass `:include_missing`, `:stringify` and `:include_parent_namespaces` + # options. `:include_parent_namespaces` defaults to true, hence must be set to false if + # you want only to return params declared against the current/target endpoint. + def declared(passed_params, options = {}, declared_params = nil, params_nested_path = []) + raise MethodNotYetAvailable unless before_filter_passed + + options.reverse_merge!(include_missing: true, include_parent_namespaces: true, evaluate_given: false) + declared_params ||= optioned_declared_params(options[:include_parent_namespaces]) + + res = if passed_params.is_a?(Array) + declared_array(passed_params, options, declared_params, params_nested_path) + else + declared_hash(passed_params, options, declared_params, params_nested_path) + end + + if (key_maps = inheritable_setting.namespace_stackable[:contract_key_map]) + key_maps.each { |key_map| key_map.write(passed_params, res) } + end + + res + end + + private + + def declared_array(passed_params, options, declared_params, params_nested_path) + passed_params.map do |passed_param| + declared(passed_param || {}, options, declared_params, params_nested_path) + end + end + + def declared_hash(passed_params, options, declared_params, params_nested_path) + 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_hash_attr(passed_params, options, declared_param_attr.key, params_nested_path, memo) + end + end + + def declared_hash_attr(passed_params, options, declared_param, params_nested_path, memo) + renamed_params = inheritable_setting.route[: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) + + 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[memo_key] = handle_passed_param(params_nested_path_dup, has_passed_children: 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. + return unless options[:include_missing] || passed_params.try(:key?, declared_param) + + 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] + + 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) + return yield if has_passed_children + + key = params_nested_path[0] + key += "[#{params_nested_path[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.exclude?(',')) + [] + elsif type == 'Set' || type&.start_with?('# 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.exclude?(',')) - [] - elsif type == 'Set' || type&.start_with?('#