-
Notifications
You must be signed in to change notification settings - Fork 751
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 user-supplied opts #1041
Conversation
lib/makeRequest.js
Outdated
@@ -1,6 +1,7 @@ | |||
'use strict'; | |||
|
|||
const utils = require('./utils'); | |||
const cloneDeep = require('lodash.clonedeep'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd try to avoid the extra dependency if at all possible. Do you think it is necessary to deep clone? I haven't checked, but my guess would be we aren't doing any mutations more than a single level down and a shallow clone {...opts}
would be enough.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using shallow copying with the ...
operator, the opts do get mutated, so I think we do need a deep copy. What works is JSON.parse(JSON.stringify(args))
, of course this isn't the most robust way of copying. Any thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually I may have misread your comment, I will try the shallow copy one level down and report back.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shallow copying one level down works! We do not need this dependency anymore. ptal @richardm-stripe
1adaf27
to
606b56a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Hey @vcheung-stripe and @richardm-stripe, we're using Node 6 this line broke our build -- Node 6 doesn't support the spread operator in objects:
Can we change this to something else? Version deprecations shouldn't be done in a minor release at any rate. I'll open a PR shortly. |
Hi @jacoblee93, we ended support for Node 6 as of version 8 of stripe-node: https://github.com/stripe/stripe-node/wiki/Migration-guide-for-v8 I admit that finding that information was harder that it should have been. I'll put up a PR shortly that clearly states our supported versions in the main readme. |
Dang. Thanks for letting me know. |
Summary
When a user supplies opts to a function, the user's opts are mutated. This becomes a problem when a user supplies an opt that is deprecated; e.g.
stripe_account
instead ofstripeAccount
. In this scenario:Because of this mutation, the user is warned that they have supplied both of these args, when in fact they have only supplied one.
This change makes a shallow copy of the user's opts so that this does not happen.
Motivation
fixes #1029
Test plan