Skip to content

fix(project config): Don't mutate the datafile object #462

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 3 commits into from
Apr 22, 2020
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
30 changes: 29 additions & 1 deletion packages/optimizely-sdk/lib/core/project_config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,41 @@ var EXPERIMENT_RUNNING_STATUS = 'Running';
var RESERVED_ATTRIBUTE_PREFIX = '$opt_';
var MODULE_NAME = 'PROJECT_CONFIG';

function createMutationSafeDatafileCopy(datafile) {
var datafileCopy = fns.assign({}, datafile);
datafileCopy.audiences = (datafile.audiences || []).map(function(audience) {
return fns.assign({}, audience);
});
datafileCopy.experiments = (datafile.experiments || []).map(function(experiment) {
return fns.assign({}, experiment);
});
datafileCopy.featureFlags = (datafile.featureFlags || []).map(function(featureFlag) {
return fns.assign({}, featureFlag);
});
datafileCopy.groups = (datafile.groups || []).map(function(group) {
var groupCopy = fns.assign({}, group);
groupCopy.experiments = (group.experiments || []).map(function(experiment) {
return fns.assign({}, experiment);
});
return groupCopy;
});
datafileCopy.rollouts = (datafile.rollouts || []).map(function(rollout) {
var rolloutCopy = fns.assign({}, rollout);
rolloutCopy.experiments = (rollout.experiments || []).map(function(experiment) {
return fns.assign({}, experiment);
});
return rolloutCopy;
});
return datafileCopy;
}

/**
* Creates projectConfig object to be used for quick project property lookup
* @param {Object} datafile JSON datafile representing the project
* @return {Object} Object representing project configuration
*/
export var createProjectConfig = function(datafile) {
var projectConfig = fns.assign({}, datafile);
var projectConfig = createMutationSafeDatafileCopy(datafile);

/*
* Conditions of audiences in projectConfig.typedAudiences are not
Expand Down
16 changes: 14 additions & 2 deletions packages/optimizely-sdk/lib/core/project_config/index.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,26 @@ import configValidator from '../../utils/config_validator';
var logger = getLogger();

describe('lib/core/project_config', function() {
var parsedAudiences = testDatafile.getParsedAudiences;
describe('createProjectConfig method', function() {
it('should set properties correctly when createProjectConfig is called', function() {
var testData = testDatafile.getTestProjectConfig();
var configObj = projectConfig.createProjectConfig(testData);

forEach(testData.audiences, function(audience) {
audience.conditions = audience.conditions;
audience.conditions = JSON.parse(audience.conditions);
});

assert.strictEqual(configObj.accountId, testData.accountId);
assert.strictEqual(configObj.projectId, testData.projectId);
assert.strictEqual(configObj.revision, testData.revision);
assert.deepEqual(configObj.events, testData.events);
assert.deepEqual(configObj.audiences, testData.audiences);
testData.groups.forEach(function(group) {
group.experiments.forEach(function(experiment) {
experiment.groupId = group.id;
experiment.variationKeyMap = fns.keyBy(experiment.variations, 'key');
});
});
assert.deepEqual(configObj.groups, testData.groups);

var expectedGroupIdMap = {
Expand Down Expand Up @@ -170,6 +175,13 @@ describe('lib/core/project_config', function() {
assert.deepEqual(configObj.variationIdMap, expectedVariationIdMap);
});

it('should not mutate the datafile', function() {
var datafile = testDatafile.getTypedAudiencesConfig();
var datafileClone = cloneDeep(datafile);
projectConfig.createProjectConfig(datafile);
assert.deepEqual(datafileClone, datafile);
});

describe('feature management', function() {
var configObj;
beforeEach(function() {
Expand Down
22 changes: 22 additions & 0 deletions packages/optimizely-sdk/lib/optimizely/index.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,28 @@ describe('lib/optimizely', function() {
});
});
});

it('should support constructing two instances using the same datafile object', function() {
var datafile = testData.getTypedAudiencesConfig();
var optlyInstance = new Optimizely({
clientEngine: 'node-sdk',
datafile: datafile,
errorHandler: stubErrorHandler,
eventDispatcher: stubEventDispatcher,
jsonSchemaValidator: jsonSchemaValidator,
logger: createdLogger,
});
assert.instanceOf(optlyInstance, Optimizely);
var optlyInstance2 = new Optimizely({
clientEngine: 'node-sdk',
datafile: datafile,
errorHandler: stubErrorHandler,
eventDispatcher: stubEventDispatcher,
jsonSchemaValidator: jsonSchemaValidator,
logger: createdLogger,
});
assert.instanceOf(optlyInstance2, Optimizely);
});
});
});

Expand Down