Skip to content

Commit 999169a

Browse files
committed
feat(api): Feature variable APIs return default variable value when featureEnabled property is false.
1 parent 021bd08 commit 999169a

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

lib/optimizely.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ def get_feature_variable_for_type(feature_flag_key, variable_key, variable_type,
473473
variation = decision['variation']
474474
variation_variable_usages = @config.variation_id_to_variable_usage_map[variation['id']]
475475
variable_id = variable['id']
476-
if variation_variable_usages&.key?(variable_id)
476+
if variation['featureEnabled'] && variation_variable_usages&.key?(variable_id)
477477
variable_value = variation_variable_usages[variable_id]['value']
478478
@logger.log(Logger::INFO,
479479
"Got variable value '#{variable_value}' for variable '#{variable_key}' of feature flag '#{feature_flag_key}'.")

spec/project_spec.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1732,6 +1732,53 @@ class InvalidErrorHandler; end
17321732
end
17331733
end
17341734

1735+
describe '#get_feature_variable_for_type with default variables' do
1736+
it 'should return default variable type and value, when user in experiment and feature is not enabled' do
1737+
integer_feature = project_instance.config.feature_flag_key_map['integer_single_variable_feature']
1738+
experiment_to_return = project_instance.config.experiment_id_map[integer_feature['experimentIds'][0]]
1739+
variation_to_return = experiment_to_return['variations'][0]
1740+
variation_to_return['featureEnabled'] = false
1741+
decision_to_return = Optimizely::DecisionService::Decision.new(
1742+
experiment_to_return,
1743+
variation_to_return,
1744+
Optimizely::DecisionService::DECISION_SOURCE_EXPERIMENT
1745+
)
1746+
1747+
allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_to_return)
1748+
1749+
expect(project_instance.send(
1750+
:get_feature_variable_for_type,
1751+
'integer_single_variable_feature',
1752+
'integer_variable',
1753+
'integer',
1754+
'test_user',
1755+
'browser_type' => 'firefox'
1756+
)).to eq(7)
1757+
end
1758+
1759+
it 'should return default variable type and value, when user in rollout and feature is not enabled' do
1760+
experiment_to_return = config_body['rollouts'][0]['experiments'][1]
1761+
variation_to_return = experiment_to_return['variations'][0]
1762+
decision_to_return = Optimizely::DecisionService::Decision.new(
1763+
experiment_to_return,
1764+
variation_to_return,
1765+
Optimizely::DecisionService::DECISION_SOURCE_ROLLOUT
1766+
)
1767+
allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_to_return)
1768+
1769+
expect(variation_to_return['featureEnabled']).to be false
1770+
1771+
expect(project_instance.send(
1772+
:get_feature_variable_for_type,
1773+
'boolean_single_variable_feature',
1774+
'boolean_variable',
1775+
'boolean',
1776+
'test_user',
1777+
{}
1778+
)).to eq(true)
1779+
end
1780+
end
1781+
17351782
describe 'when forced variation is used' do
17361783
# setForcedVariation on a paused experiment and then call getVariation.
17371784
it 'should return null when getVariation is called on a paused experiment after setForcedVariation' do

0 commit comments

Comments
 (0)