From 1568f794ec426f3d451244bd1b4f3438be0a2b3c Mon Sep 17 00:00:00 2001 From: Aaron Lasseigne Date: Thu, 23 Apr 2020 11:20:35 -0500 Subject: [PATCH] returning false or nil from safe_converter could be problematic for boolean values --- .rubocop.yml | 2 ++ lib/active_interaction/filter.rb | 16 ++-------------- .../filters/abstract_date_time_filter.rb | 4 ++++ .../filters/abstract_numeric_filter.rb | 4 ++++ lib/active_interaction/filters/array_filter.rb | 6 +++++- lib/active_interaction/filters/boolean_filter.rb | 4 ++++ lib/active_interaction/filters/hash_filter.rb | 6 +++++- lib/active_interaction/filters/string_filter.rb | 6 +++++- lib/active_interaction/filters/symbol_filter.rb | 4 ++++ lib/active_interaction/filters/time_filter.rb | 2 ++ 10 files changed, 37 insertions(+), 17 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 2c2edc8d..a10e4432 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -26,6 +26,8 @@ Layout/MultilineOperationIndentation: Metrics/BlockLength: Exclude: - spec/**/* +Metrics/MethodLength: + Max: 15 Style/FrozenStringLiteralComment: Exclude: - '**/*.gemspec' diff --git a/lib/active_interaction/filter.rb b/lib/active_interaction/filter.rb index 678ba254..48c1cbe8 100644 --- a/lib/active_interaction/filter.rb +++ b/lib/active_interaction/filter.rb @@ -173,7 +173,7 @@ def default? # @private # rubocop:disable Metrics/MethodLength def cast(value, context, convert: true, reconstantize: true) - if safe_matches?(value) + if matches?(value) adjust_output(value, context) # we can't use `nil?` because BasicObject doesn't have it elsif value == nil # rubocop:disable Style/NilComparison @@ -186,7 +186,7 @@ def cast(value, context, convert: true, reconstantize: true) reconstantize: false ) elsif convert - public_send(__method__, safe_convert(value), context, + public_send(__method__, convert(value), context, convert: false, reconstantize: reconstantize ) @@ -217,12 +217,6 @@ def matches?(_value) false end - def safe_matches?(value) - matches?(value) - rescue NoMethodError - false - end - def adjust_output(value, _context) value end @@ -231,12 +225,6 @@ def convert(value) value end - def safe_convert(value) - convert(value) - rescue NoMethodError - false - end - # @param value [Object] # @return [String] def describe(value) diff --git a/lib/active_interaction/filters/abstract_date_time_filter.rb b/lib/active_interaction/filters/abstract_date_time_filter.rb index 520a0519..12041ff4 100644 --- a/lib/active_interaction/filters/abstract_date_time_filter.rb +++ b/lib/active_interaction/filters/abstract_date_time_filter.rb @@ -20,6 +20,8 @@ def klasses def matches?(value) klasses.any? { |klass| value.is_a?(klass) } + rescue NoMethodError # BasicObject + false end def convert(value) @@ -32,6 +34,8 @@ def convert(value) end rescue ArgumentError value + rescue NoMethodError # BasicObject + super end def convert_string(value) diff --git a/lib/active_interaction/filters/abstract_numeric_filter.rb b/lib/active_interaction/filters/abstract_numeric_filter.rb index 5874a32d..64cd0000 100644 --- a/lib/active_interaction/filters/abstract_numeric_filter.rb +++ b/lib/active_interaction/filters/abstract_numeric_filter.rb @@ -15,6 +15,8 @@ def database_column_type def matches?(value) value.is_a?(klass) + rescue NoMethodError # BasicObject + false end def convert(value) @@ -27,6 +29,8 @@ def convert(value) else super end + rescue NoMethodError # BasicObject + super end def converter(value) diff --git a/lib/active_interaction/filters/array_filter.rb b/lib/active_interaction/filters/array_filter.rb index 0291d405..980d2b1d 100644 --- a/lib/active_interaction/filters/array_filter.rb +++ b/lib/active_interaction/filters/array_filter.rb @@ -49,6 +49,8 @@ def klasses def matches?(value) klasses.any? { |klass| value.is_a?(klass) } + rescue NoMethodError # BasicObject + false end def adjust_output(value, context) @@ -62,8 +64,10 @@ def convert(value) if value.respond_to?(:to_ary) value.to_ary else - value + super end + rescue NoMethodError # BasicObject + super end def add_option_in_place_of_name(klass, options) diff --git a/lib/active_interaction/filters/boolean_filter.rb b/lib/active_interaction/filters/boolean_filter.rb index 7ac5c0b1..e442db3f 100644 --- a/lib/active_interaction/filters/boolean_filter.rb +++ b/lib/active_interaction/filters/boolean_filter.rb @@ -26,6 +26,8 @@ def database_column_type def matches?(value) value.is_a?(TrueClass) || value.is_a?(FalseClass) + rescue NoMethodError # BasicObject + false end def convert(value) @@ -37,6 +39,8 @@ def convert(value) else super end + rescue NoMethodError # BasicObject + super end end end diff --git a/lib/active_interaction/filters/hash_filter.rb b/lib/active_interaction/filters/hash_filter.rb index 9ee32663..c0f70971 100644 --- a/lib/active_interaction/filters/hash_filter.rb +++ b/lib/active_interaction/filters/hash_filter.rb @@ -29,6 +29,8 @@ class HashFilter < Filter def matches?(value) value.is_a?(Hash) + rescue NoMethodError # BasicObject + false end def clean_value(hash, name, filter, value, context) @@ -55,8 +57,10 @@ def convert(value) if value.respond_to?(:to_hash) value.to_hash else - value + super end + rescue NoMethodError # BasicObject + super end # rubocop:disable Style/MissingRespondToMissing, Style/MethodMissingSuper diff --git a/lib/active_interaction/filters/string_filter.rb b/lib/active_interaction/filters/string_filter.rb index 1bb8ed3a..09c0377f 100644 --- a/lib/active_interaction/filters/string_filter.rb +++ b/lib/active_interaction/filters/string_filter.rb @@ -28,6 +28,8 @@ def strip? def matches?(value) value.is_a?(String) + rescue NoMethodError # BasicObject + false end def adjust_output(value, _context) @@ -38,8 +40,10 @@ def convert(value) if value.respond_to?(:to_str) value.to_str else - value + super end + rescue NoMethodError # BasicObject + super end end end diff --git a/lib/active_interaction/filters/symbol_filter.rb b/lib/active_interaction/filters/symbol_filter.rb index 164421d4..57805660 100644 --- a/lib/active_interaction/filters/symbol_filter.rb +++ b/lib/active_interaction/filters/symbol_filter.rb @@ -20,6 +20,8 @@ class SymbolFilter < Filter def matches?(value) value.is_a?(Symbol) + rescue NoMethodError # BasicObject + false end def convert(value) @@ -28,6 +30,8 @@ def convert(value) else super end + rescue NoMethodError # BasicObject + super end end end diff --git a/lib/active_interaction/filters/time_filter.rb b/lib/active_interaction/filters/time_filter.rb index 9998f42e..8b209a66 100644 --- a/lib/active_interaction/filters/time_filter.rb +++ b/lib/active_interaction/filters/time_filter.rb @@ -57,6 +57,8 @@ def convert(value) else super end + rescue NoMethodError # BasicObject + super end end end