Skip to content

Commit 41ee988

Browse files
authored
Merge pull request #1962 from dblock/reapply-1961
Re-apply the micro-optimizations and parent fix from #1961.
2 parents 16057ad + fe50fe8 commit 41ee988

File tree

5 files changed

+23
-6
lines changed

5 files changed

+23
-6
lines changed

lib/grape/dsl/settings.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,11 @@ def within_namespace(&_block)
171171
# the superclass's :inheritable_setting.
172172
def build_top_level_setting
173173
Grape::Util::InheritableSetting.new.tap do |setting|
174-
if defined?(superclass) && superclass.respond_to?(:inheritable_setting) && superclass != Grape::API
174+
# Doesn't try to inherit settings from +Grape::API::Instance+ which also responds to
175+
# +inheritable_setting+, however, it doesn't contain any user-defined settings.
176+
# Otherwise, it would lead to an extra instance of +Grape::Util::InheritableSetting+
177+
# in the chain for every endpoint.
178+
if defined?(superclass) && superclass.respond_to?(:inheritable_setting) && superclass != Grape::API::Instance
175179
setting.inherit_from superclass.inheritable_setting
176180
end
177181
end

lib/grape/util/stackable_values.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def initialize(*_args)
1313
@frozen_values = {}
1414
end
1515

16+
# Even if there is no value, an empty array will be returned.
1617
def [](name)
1718
return @frozen_values[name] if @frozen_values.key? name
1819

lib/grape/validations/params_scope.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ def configuration
4545
# @return [Boolean] whether or not this entire scope needs to be
4646
# validated
4747
def should_validate?(parameters)
48-
return false if @optional && (params(parameters).blank? || all_element_blank?(parameters))
49-
return false unless meets_dependency?(params(parameters), parameters)
48+
scoped_params = params(parameters)
49+
50+
return false if @optional && (scoped_params.blank? || all_element_blank?(scoped_params))
51+
return false unless meets_dependency?(scoped_params, parameters)
5052
return true if parent.nil?
5153
parent.should_validate?(parameters)
5254
end
@@ -446,8 +448,8 @@ def options_key?(type, key, validations)
446448
validations[type].respond_to?(:key?) && validations[type].key?(key) && !validations[type][key].nil?
447449
end
448450

449-
def all_element_blank?(parameters)
450-
params(parameters).respond_to?(:all?) && params(parameters).all?(&:blank?)
451+
def all_element_blank?(scoped_params)
452+
scoped_params.respond_to?(:all?) && scoped_params.all?(&:blank?)
451453
end
452454

453455
# Validators don't have access to each other and they don't need, however,

lib/grape/validations/types/build_coercer.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,11 @@ def self.cache_instance(type, method, strict, &_block)
8383
end
8484

8585
def self.cache_key(type, method, strict)
86-
[type, method, strict].compact.map(&:to_s).join('_')
86+
[type, method, strict].each_with_object(+'_') do |val, memo|
87+
next if val.nil?
88+
89+
memo << '_' << val.to_s
90+
end
8791
end
8892

8993
instance_variable_set(:@__cache, {})

spec/grape/api/instance_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,10 @@ def app
4545
expect(last_response.body).to eq 'success'
4646
end
4747
end
48+
49+
context 'top level setting' do
50+
it 'does not inherit settings from the superclass (Grape::API::Instance)' do
51+
expect(an_instance.top_level_setting.parent).to be_nil
52+
end
53+
end
4854
end

0 commit comments

Comments
 (0)