Skip to content

Commit

Permalink
Merge pull request #526 from stripe/ralex/dont-mutate-params
Browse files Browse the repository at this point in the history
Do not mutate params when encoding arrays as indexed objects
  • Loading branch information
rattrayalex-stripe authored Nov 26, 2018
2 parents 314b17b + f39328e commit e40a4ce
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/makeRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function getRequestOpts(self, requestArgs, spec, overrideData) {

// Pull request data and options (headers, auth) from args.
var dataFromArgs = utils.getDataFromArgs(args);
var data = encode(Object.assign(dataFromArgs, overrideData));
var data = encode(Object.assign({}, dataFromArgs, overrideData));
var options = utils.getOptionsFromArgs(args);

// Validate that there are no more args.
Expand Down
1 change: 1 addition & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ var utils = module.exports = {
*/
encodeParamWithIntegerIndexes: function(param, data) {
if (data[param] !== undefined) {
data = Object.assign({}, data);
data[param] = utils.arrayToObject(data[param]);
}
return data;
Expand Down
8 changes: 8 additions & 0 deletions test/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,14 @@ describe('utils', function() {
var partial = utils.encodeParamWithIntegerIndexes.bind(null, 'paramToEncode');
expect(partial(data)).to.deep.equal(expectedData);
});

it('does not mutate input variables', function() {
var data = {'paramToEncode': ['value1']};
var expectedData = {'paramToEncode': {'0': 'value1'}};
expect(utils.encodeParamWithIntegerIndexes('paramToEncode', data)).to.deep.equal(expectedData);
expect(data).not.to.deep.equal(expectedData);
expect(Array.isArray(data.paramToEncode)).to.equal(true);
});
});

describe('arrayToObject', function() {
Expand Down

0 comments on commit e40a4ce

Please sign in to comment.