From 280e5b3aab35cd1bdf4d689d12d1fb19fe23d556 Mon Sep 17 00:00:00 2001 From: Eric Proulx Date: Mon, 15 May 2023 22:43:48 +0200 Subject: [PATCH] Use Active support functions (#2326) * Replace deep_mergeable_hash.rb and deep_symbolize_hash.rb by ActiveSupport functions Use each_with_object instead of each/inject etc ... * Replace if env[...] by env.key? Use deep_dup instead of dup for rack_params * Use presence instead of ternary Replace unless present? by if blank? * Replace !include? by exclude? * Use ActiveSupport duplicable? * Fix rubocop and update CHANGELOG * Add minimal version for ActiveSupport >=5 * Remove extra * in CHANGELOG * Next version 1.8.0 --- CHANGELOG.md | 3 +- README.md | 2 +- grape.gemspec | 2 +- lib/grape.rb | 4 +-- lib/grape/content_types.rb | 10 ++---- lib/grape/dsl/inside_route.rb | 4 +-- lib/grape/dsl/settings.rb | 8 ++--- lib/grape/endpoint.rb | 7 ++-- lib/grape/error_formatter/base.rb | 2 +- lib/grape/exceptions/base.rb | 4 +-- lib/grape/exceptions/validation_errors.rb | 7 +--- .../hash_with_indifferent_access.rb | 6 ++-- lib/grape/extensions/deep_mergeable_hash.rb | 21 ------------ lib/grape/extensions/deep_symbolize_hash.rb | 32 ------------------- lib/grape/extensions/hash.rb | 11 +++---- lib/grape/extensions/hashie/mash.rb | 6 ++-- lib/grape/formatter/serializable_hash.rb | 14 ++++---- lib/grape/middleware/auth/base.rb | 2 +- lib/grape/middleware/formatter.rb | 2 +- lib/grape/middleware/versioner/header.rb | 30 +++++++---------- lib/grape/util/lazy_value.rb | 14 ++------ lib/grape/util/strict_hash_configuration.rb | 7 ++-- lib/grape/validations/params_scope.rb | 2 +- .../validations/types/custom_type_coercer.rb | 18 ++--------- lib/grape/validations/validators/base.rb | 2 +- .../validators/default_validator.rb | 22 ++----------- lib/grape/version.rb | 2 +- spec/grape/api_spec.rb | 2 +- 28 files changed, 63 insertions(+), 183 deletions(-) delete mode 100644 lib/grape/extensions/deep_mergeable_hash.rb delete mode 100644 lib/grape/extensions/deep_symbolize_hash.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 99d0d957c1..b862edf6a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,8 @@ -### 1.7.2 (Next) +### 1.8.0 (Next) #### Features +* [#2326](https://github.com/ruby-grape/grape/pull/2326): Use ActiveSupport extensions - [@ericproulx](https://github.com/ericproulx). * Your contribution here. #### Fixes diff --git a/README.md b/README.md index 8ff988925d..a334a72a88 100644 --- a/README.md +++ b/README.md @@ -159,7 +159,7 @@ content negotiation, versioning and much more. ## Stable Release -You're reading the documentation for the next release of Grape, which should be **1.7.2**. +You're reading the documentation for the next release of Grape, which should be **1.8.0**. Please read [UPGRADING](UPGRADING.md) when upgrading from a previous version. The current stable release is [1.7.1](https://github.com/ruby-grape/grape/blob/v1.7.1/README.md). diff --git a/grape.gemspec b/grape.gemspec index bee87fbc6a..e79e1c0ed4 100644 --- a/grape.gemspec +++ b/grape.gemspec @@ -20,7 +20,7 @@ Gem::Specification.new do |s| 'source_code_uri' => "https://github.com/ruby-grape/grape/tree/v#{s.version}" } - s.add_runtime_dependency 'activesupport' + s.add_runtime_dependency 'activesupport', '>= 5' s.add_runtime_dependency 'builder' s.add_runtime_dependency 'dry-types', '>= 1.1' s.add_runtime_dependency 'mustermann-grape', '~> 1.0.0' diff --git a/lib/grape.rb b/lib/grape.rb index f51a2e8b07..ed309e16f5 100644 --- a/lib/grape.rb +++ b/lib/grape.rb @@ -20,9 +20,11 @@ require 'active_support/core_ext/hash/deep_merge' require 'active_support/core_ext/hash/except' require 'active_support/core_ext/hash/indifferent_access' +require 'active_support/core_ext/hash/keys' require 'active_support/core_ext/hash/reverse_merge' require 'active_support/core_ext/hash/slice' require 'active_support/core_ext/object/blank' +require 'active_support/core_ext/object/duplicable' require 'active_support/dependencies/autoload' require 'active_support/notifications' require 'i18n' @@ -92,8 +94,6 @@ module Exceptions module Extensions extend ::ActiveSupport::Autoload eager_autoload do - autoload :DeepMergeableHash - autoload :DeepSymbolizeHash autoload :Hash end module ActiveSupport diff --git a/lib/grape/content_types.rb b/lib/grape/content_types.rb index 2c19f97317..c6f295154a 100644 --- a/lib/grape/content_types.rb +++ b/lib/grape/content_types.rb @@ -17,17 +17,11 @@ module ContentTypes class << self def content_types_for_settings(settings) - return if settings.blank? - - settings.each_with_object({}) { |value, result| result.merge!(value) } + settings&.inject(:merge!) end def content_types_for(from_settings) - if from_settings.present? - from_settings - else - Grape::ContentTypes::CONTENT_TYPES.merge(default_elements) - end + from_settings.presence || Grape::ContentTypes::CONTENT_TYPES.merge(default_elements) end end end diff --git a/lib/grape/dsl/inside_route.rb b/lib/grape/dsl/inside_route.rb index a54967ead3..e2ee006c78 100644 --- a/lib/grape/dsl/inside_route.rb +++ b/lib/grape/dsl/inside_route.rb @@ -103,7 +103,7 @@ def handle_passed_param(params_nested_path, has_passed_children = false, &_block if type == 'Hash' && !has_children {} - elsif type == 'Array' || (type&.start_with?('[') && !type&.include?(',')) + elsif type == 'Array' || (type&.start_with?('[') && type&.exclude?(',')) [] elsif type == 'Set' || type&.start_with?('# value) - end - end end end end diff --git a/lib/grape/validations/validators/base.rb b/lib/grape/validations/validators/base.rb index 2c8403bbc5..86813bf9be 100644 --- a/lib/grape/validations/validators/base.rb +++ b/lib/grape/validations/validators/base.rb @@ -71,7 +71,7 @@ def self.convert_to_short_name(klass) end def self.inherited(klass) - return unless klass.name.present? + return if klass.name.blank? Validations.register_validator(convert_to_short_name(klass), klass) end diff --git a/lib/grape/validations/validators/default_validator.rb b/lib/grape/validations/validators/default_validator.rb index 8ed5936756..4058d7b1dd 100644 --- a/lib/grape/validations/validators/default_validator.rb +++ b/lib/grape/validations/validators/default_validator.rb @@ -12,10 +12,10 @@ def initialize(attrs, options, required, scope, **opts) def validate_param!(attr_name, params) params[attr_name] = if @default.is_a? Proc @default.call - elsif @default.frozen? || !duplicatable?(@default) + elsif @default.frozen? || !@default.duplicable? @default else - duplicate(@default) + @default.dup end end @@ -27,24 +27,6 @@ def validate!(params) validate_param!(attr_name, resource_params) if resource_params.is_a?(Hash) && resource_params[attr_name].nil? end end - - private - - # return true if we might be able to dup this object - def duplicatable?(obj) - !obj.nil? && - obj != true && - obj != false && - !obj.is_a?(Symbol) && - !obj.is_a?(Numeric) - end - - # make a best effort to dup the object - def duplicate(obj) - obj.dup - rescue TypeError - obj - end end end end diff --git a/lib/grape/version.rb b/lib/grape/version.rb index 6b84a9611f..64b7c47854 100644 --- a/lib/grape/version.rb +++ b/lib/grape/version.rb @@ -2,5 +2,5 @@ module Grape # The current version of Grape. - VERSION = '1.7.2' + VERSION = '1.8.0' end diff --git a/spec/grape/api_spec.rb b/spec/grape/api_spec.rb index 008a420260..44dc83153e 100644 --- a/spec/grape/api_spec.rb +++ b/spec/grape/api_spec.rb @@ -3311,7 +3311,7 @@ def static it 'is able to cascade' do subject.mount lambda { |env| headers = {} - headers['X-Cascade'] == 'pass' unless env['PATH_INFO'].include?('boo') + headers['X-Cascade'] == 'pass' if env['PATH_INFO'].exclude?('boo') [200, headers, ['Farfegnugen']] } => '/'