-
Notifications
You must be signed in to change notification settings - Fork 27.5k
fix(angular.toJson): only strip properties beginning with $$, not $ #6253
Conversation
Thanks for the PR! Please check the items below to help us merge this faster. See the contributing docs for more information.
If you need to make changes to your pull request, you can update the commit with Thanks again for your help! |
@@ -446,6 +446,13 @@ angular.module('ngResource', ['ng']). | |||
shallowClearAndCopy(value || {}, this); | |||
} | |||
|
|||
Resource.prototype.toJSON = function () { |
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.
this is a feature, unrelated to the commit message
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.
Got it. I'll break this PR down to 2 commits then
I'd have:
feat($resource): allow props beginning with $ to be used on resources
fix(angular.toJson): only strip properties beginning with $$, not $
Sounds good?
Proposing 2. without 1. would mean a PR that fails the tests
Done: I split the PR into 2 commits. I'm not 100% confident about my commit naming and ordering. If anyone has any issues with it, feel free to comment and I'll have some fun with git magic.
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.
The names of the commits seem fine. The ordering doesn't particularly matter, but I'd probably put the fix first.
@btford oops, my rebasing removed your comment?
I addressed your comments: added a test for Let me know if you want me to change anything else, or if this strategy isn't the way we want to go to solve the problem. Edit: worth clarifying I believe: the tests that are failing in travis are failing without my commits, so this PR has nothing to do with them. |
@rodyhaddad np, this is a known thing. |
+1 This makes a lot of sense. The developer can always do reassignments with ngInit in the event of a naming collision with a single $. |
Code-wise, this LGTM. I might mark this as a breaking change in the commit message. Someone might be relying on the @IgorMinar thoughts? I know you favored a whitelist approach, but regardless I'd like to get this fix in soon, since it leads to surprising/confusing behavior for many developers and it's relatively low-hanging fruit. |
+1 for shipping this version of the fix |
sounds good. can we mark this as a breaking change though so nobody is surprised? |
Agreed. I'll update the commit and merge this. |
@IgorMinar done. |
This is still causing a problem, and is an ugly hack. Why not track exactly what properties are being added by Angular and then only remove those? |
what problem is it causing for you? |
Same as #1463 was causing for other people. If I have my own properties in my own object prefixed with a '$$', I expect them to be there and stay there at all times, unless I remove them myself. |
And no, the solution isn't to add another dollar sign. |
The |
I understand. The proprietary service I work with uses double dollar sign for some parameters. What if the $$-properties are all moved under a single special angularjs property, and namespaced that way? |
@btford would this fix be backported to 1.2.x? This is a fix I really require, but cannot upgrade my angular to 1.3.x yet. |
same of @kavuri , would be great to send keys including $ characters inside body request in the 1.2 too |
This PR makes
angular.toJson
strip properties beginning with$$
, instead of$
Rationale behind this:
2 years ago, angular switched from using it's own json serializer to the native
JSON.stringify
in 35125d2.In the old serializer, angular was stripping the following properties:
this
,$parent
and anything starting with$$
When we switched to the native
JSON.stringify
, this got changed to stripping anything starting with$
I don't know if this greedier stripping resulted from an internal discussion, but it seems to be causing problems to those who use properties beginning with
$
in their model (see #1463)So I reverted to having
angular.toJson
only strip props that start with$$
There's the issue that ngResource now defines the
$promise
and$resolved
props on any resource instance: two properties that we don't want serialized and sent to the server on$save
.I worked around this by having resources utilize the
toJSON
method to not include both these properties on serialization.I didn't go with the whitelist approach mentioned in #1463 because it would require a lookup in an array/hash for every property of an object being serialized, and it would be an app-wide config potentially requiring a lot of whitelisting. A better alternative would be a blacklist of [$$hashKey, $promise, $resolved], or what I'm doing here.