Skip to content
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

Do not mutate params when encoding arrays as indexed objects #526

Merged
merged 2 commits into from
Nov 26, 2018
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
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