diff --git a/CHANGELOG.md b/CHANGELOG.md index c0584c57c..6f4dc9b37 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ * [#2693](https://github.com/ruby-grape/grape/pull/2693): Introduce `Grape::Exceptions::ErrorResponse` value object to replace the implicit-schema Hash thrown via `throw` - [@ericproulx](https://github.com/ericproulx). * [#2701](https://github.com/ruby-grape/grape/pull/2701): Replace `.tap` usages in `lib/` with explicit local variables - [@ericproulx](https://github.com/ericproulx). * [#2704](https://github.com/ruby-grape/grape/pull/2704): Add `Grape::Endpoint#logger` so the API's configured logger is reachable inside route handlers, filters, and `rescue_from` blocks without a helper - [@ericproulx](https://github.com/ericproulx). +* [#2707](https://github.com/ruby-grape/grape/pull/2707): Tighten six guard conditions in `lib/` via De Morgan and `blank?`/`present?`/`include?` rewrites; no behaviour change - [@ericproulx](https://github.com/ericproulx). * Your contribution here. #### Fixes diff --git a/lib/grape/api.rb b/lib/grape/api.rb index 90824e607..4edae21fd 100644 --- a/lib/grape/api.rb +++ b/lib/grape/api.rb @@ -10,8 +10,11 @@ class API Helpers = Grape::DSL::Helpers::BaseHelper class Boolean + VALUES = [true, false].freeze + private_constant :VALUES + def self.build(val) - return nil if val != true && val != false + return nil unless VALUES.include?(val) new end diff --git a/lib/grape/dsl/routing.rb b/lib/grape/dsl/routing.rb index b8ff0be4d..ca5b5e374 100644 --- a/lib/grape/dsl/routing.rb +++ b/lib/grape/dsl/routing.rb @@ -163,7 +163,7 @@ def route(methods, paths = ['/'], route_options = {}, &) endpoint_description = inheritable_setting.route[:description] all_route_options = { params: endpoint_params } all_route_options.deep_merge!(endpoint_description) if endpoint_description - all_route_options.deep_merge!(route_options) if route_options&.any? + all_route_options.deep_merge!(route_options) if route_options.present? new_endpoint = Grape::Endpoint.new( inheritable_setting, diff --git a/lib/grape/request.rb b/lib/grape/request.rb index f972c011a..9cbc14ac5 100644 --- a/lib/grape/request.rb +++ b/lib/grape/request.rb @@ -161,9 +161,10 @@ def cookies def make_params params = @params_builder.call(rack_params) routing_args = env[Grape::Env::GRAPE_ROUTING_ARGS] - return params unless routing_args&.any? { |k, _| k != :version && k != :route_info } + filtered = routing_args&.except(:version, :route_info) + return params if filtered.blank? - params.deep_merge!(routing_args.except(:version, :route_info)) + params.deep_merge!(filtered) rescue *Grape::RACK_ERRORS raise Grape::Exceptions::RequestError end diff --git a/lib/grape/validations/params_scope.rb b/lib/grape/validations/params_scope.rb index b9002a7e7..c209a0644 100644 --- a/lib/grape/validations/params_scope.rb +++ b/lib/grape/validations/params_scope.rb @@ -460,9 +460,9 @@ def guess_coerce_type(coerce_type, *values_list) end def check_incompatible_option_values(default, values, except_values) - return unless default && !default.is_a?(Proc) + return if default.nil? || default.is_a?(Proc) - raise Grape::Exceptions::IncompatibleOptionValues.new(:default, default, :values, values) if values && !values.is_a?(Proc) && !Array(default).all? { |def_val| values.include?(def_val) } + raise Grape::Exceptions::IncompatibleOptionValues.new(:default, default, :values, values) if values && !values.is_a?(Proc) && Array(default).any? { |def_val| !values.include?(def_val) } return unless except_values && !except_values.is_a?(Proc) && Array(default).any? { |def_val| except_values.include?(def_val) } diff --git a/lib/grape/validations/validators/base.rb b/lib/grape/validations/validators/base.rb index 3cb947f5c..9d82f575c 100644 --- a/lib/grape/validations/validators/base.rb +++ b/lib/grape/validations/validators/base.rb @@ -148,7 +148,7 @@ def option_value end def scrub(value) - return value unless value.respond_to?(:valid_encoding?) && !value.valid_encoding? + return value if !value.respond_to?(:valid_encoding?) || value.valid_encoding? value.scrub end