Skip to content

feat(api): Feature variable APIs now return default variable value when featureEnabled property is false. #249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Apr 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ describe('lib/core/decision_service', function() {
],
'featureEnabled': false,
'key': 'variation2'
}
},
},
},
variation: {
Expand Down
10 changes: 5 additions & 5 deletions packages/optimizely-sdk/lib/core/project_config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -496,16 +496,15 @@ module.exports = {

/**
* Get the value of the given variable for the given variation. If the given
* variable has no value for the given variation, return the variable's
* default value. Log an error message if the variation is invalid. If the
* variable has no value for the given variation, return null. Log an error message if the variation is invalid. If the
* variable or variation are invalid, return null.
* @param {Object} projectConfig
* @param {Object} variable
* @param {Object} variation
* @param {Object} logger
* @return {string|null} The value of the given variable for the given
* variation, or the variable default value if the given variable has no value
* for the given variation, or null if the variation or variable are invalid
* variation, or null if the given variable has no value
* for the given variation or if the variation or variable are invalid
*/
getVariableValueForVariation: function(projectConfig, variable, variation, logger) {
if (!variable || !variation) {
Expand All @@ -519,7 +518,8 @@ module.exports = {

var variableUsages = projectConfig.variationVariableUsageMap[variation.id];
var variableUsage = variableUsages[variable.id];
return variableUsage ? variableUsage.value : variable.defaultValue;

return variableUsage ? variableUsage.value : null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The doc comment for this function is now incorrect, given this change.

},

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,11 +453,11 @@ describe('lib/core/project_config', function() {
assert.strictEqual(result, null);
});

it('returns the variable default value if the variation does not have a value for this variable', function() {
it('returns null if the variation does not have a value for this variable', function() {
var variation = configObj.variationIdMap['595008']; // This variation has no variable values associated with it
var variable = configObj.featureKeyMap.test_feature_for_experiment.variableKeyMap.num_buttons;
var result = projectConfig.getVariableValueForVariation(configObj, variable, variation, featureManagementLogger);
assert.strictEqual(result, '10');
assert.isNull(result);
});
});

Expand Down
20 changes: 15 additions & 5 deletions packages/optimizely-sdk/lib/optimizely/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -632,17 +632,27 @@ Optimizely.prototype._getFeatureVariableForType = function(featureKey, variableK
return null;
}

var variableValue;
var featureEnabled = false;
var variableValue = variable.defaultValue;
var decision = this.decisionService.getVariationForFeature(this.configObj, featureFlag, userId, attributes);

if (decision.variation !== null) {
featureEnabled = decision.variation.featureEnabled;
variableValue = projectConfig.getVariableValueForVariation(this.configObj, variable, decision.variation, this.logger);
this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.USER_RECEIVED_VARIABLE_VALUE, MODULE_NAME, variableKey, featureFlag.key, variableValue, userId));
var value = projectConfig.getVariableValueForVariation(this.configObj, variable, decision.variation, this.logger);
if (value !== null) {
if (featureEnabled === true) {
variableValue = value;
this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.USER_RECEIVED_VARIABLE_VALUE, MODULE_NAME, variableKey, featureFlag.key, variableValue, userId));
} else {
this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.FEATURE_NOT_ENABLED_RETURN_DEFAULT_VARIABLE_VALUE, MODULE_NAME,
featureFlag.key, userId, variableKey));
}
} else {
this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.VARIABLE_NOT_USED_RETURN_DEFAULT_VARIABLE_VALUE, MODULE_NAME, variableKey, decision.variation.key));
}
} else {
variableValue = variable.defaultValue;
this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.USER_RECEIVED_DEFAULT_VARIABLE_VALUE, MODULE_NAME, userId, variableKey, featureFlag.key));
this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.USER_RECEIVED_DEFAULT_VARIABLE_VALUE, MODULE_NAME, userId,
variableKey, featureFlag.key));
}

var experimentKey = null;
Expand Down
Loading