Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

fix($resource): Delete $cancelRequest from toJSON() #15244

Closed
wants to merge 5 commits into from

Conversation

hereblur
Copy link
Contributor

What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)
Bug fix

What is the current behavior? (You can also link to an open issue here)
result of ngResource.toJSON still include $cancelRequest

What is the new behavior (if this is a feature change)?
remove delete $cancelRequest from ngResource.toJSON's result.

Does this PR introduce a breaking change?
No

Please check if the PR fulfills these requirements

Other information:

@gkalpak
Copy link
Member

gkalpak commented Oct 11, 2016

Thx for the PR. This was an intentional omission though. Since $cancelRequest is always a function (and JSON.stringify() ignores functions), it shouldn't make any difference.

I am going to close this, but if you have a special usecase that breaks because of $cancelRequest(), please share below and I'll be happy to investigate.

@hereblur
Copy link
Contributor Author

In my opinion, from the method name toJSON() we should be expected for a valid and ready to use of a JSON object.

In many cases, people need the literal object from server without extra keys.
I got wrong number when I do Object.keys( result ).length.

I think one should not have to do JSON.parse(JSON.stringifyr(result)) to get it works.

Thank you.

@gkalpak
Copy link
Member

gkalpak commented Oct 13, 2016

toJSON is a special method recognized by JSON.stringify() and is only intended to be used implicitly by that - i.e. not directly by the user (MDN docs).

That said, I guess it doesn't hurt to remove one more property ourselves (instead of relying on JSON.stringify()), so why not?

Reopening, but we need some tests for this (if we are making the change, we should make sure there will be no regressions in the future).

@gkalpak
Copy link
Member

gkalpak commented Oct 13, 2016

Hm...I just realized toJSON is publicly documented and the docs state that the returned object does not contain any of the extra properties. So, we should definitely fix that.

Thx @hereblur bringing it up.

@gkalpak gkalpak assigned gkalpak and unassigned gkalpak Oct 13, 2016
@petebacondarwin petebacondarwin added this to the 1.6.x (post 1.6.0) milestone Oct 13, 2016
hereblur pushed a commit to hereblur/angular.js that referenced this pull request Oct 13, 2016
@googlebot
Copy link

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed, please reply here (e.g. I signed it!) and we'll verify. Thanks.


  • If you've already signed a CLA, it's possible we don't have your GitHub username or you're using a different email address. Check your existing CLA data and verify that your email is set on your git commits.
  • If you signed the CLA as a corporation, please let us know the company's name.

1 similar comment
@googlebot
Copy link

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed, please reply here (e.g. I signed it!) and we'll verify. Thanks.


  • If you've already signed a CLA, it's possible we don't have your GitHub username or you're using a different email address. Check your existing CLA data and verify that your email is set on your git commits.
  • If you signed the CLA as a corporation, please let us know the company's name.

@hereblur
Copy link
Contributor Author

I signed it!

@googlebot
Copy link

We found a Contributor License Agreement for you (the sender of this pull request), but were unable to find agreements for the commit author(s). If you authored these, maybe you used a different email address in the git commits than was used to sign the CLA (login here to double check)? If these were authored by someone else, then they will need to sign a CLA as well, and confirm that they're okay with these being contributed to Google.

1 similar comment
@googlebot
Copy link

We found a Contributor License Agreement for you (the sender of this pull request), but were unable to find agreements for the commit author(s). If you authored these, maybe you used a different email address in the git commits than was used to sign the CLA (login here to double check)? If these were authored by someone else, then they will need to sign a CLA as well, and confirm that they're okay with these being contributed to Google.

@googlebot
Copy link

CLAs look good, thanks!

1 similar comment
@googlebot
Copy link

CLAs look good, thanks!

result.toJSON() still include $cancelRequest to the JSON output.
just simply delete it.
@hereblur
Copy link
Contributor Author

@gkalpak Cool!, Thank you.
And I just pushed the test.

@@ -750,6 +750,7 @@ describe('basic usage', function() {
var json = JSON.parse(angular.toJson(cc));
expect(json.$promise).not.toBeDefined();
expect(json.$resolved).not.toBeDefined();
expect(json.$cancelRequest).not.toBeDefined();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add an expectation that is was there before? (Similar to what we have for $promise/$resolved.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just realized that this test uses JSON.parse(angular.toJson(cc)), which means it would pass even without this PR.
Can you add a new test (similar to this one) that uses cc.toJSON() directly? (Leave this one as is - it doesn't hurt.)

Improved test, per @gkalpak comment, add an expectation that is was there before.
@@ -750,6 +750,7 @@ describe('basic usage', function() {
var json = JSON.parse(angular.toJson(cc));
expect(json.$promise).not.toBeDefined();
expect(json.$resolved).not.toBeDefined();
expect(json.$cancelRequest).not.toBeDefined();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just realized that this test uses JSON.parse(angular.toJson(cc)), which means it would pass even without this PR.
Can you add a new test (similar to this one) that uses cc.toJSON() directly? (Leave this one as is - it doesn't hurt.)

    create separated test since the existing test always pass without this PR.
@@ -737,7 +740,7 @@ describe('basic usage', function() {
expect(person2).toEqual(jasmine.any(Person));
});

it('should not include $promise and $resolved when resource is toJson\'ed', function() {
it('should not include $promise, $resolved when resource is toJson\'ed', function() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revert this change.

@@ -29,6 +29,9 @@ describe('basic usage', function() {
}
}

},
{
cancellable: true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: It probably doesn't matter, but I would rather keep changes local, instead of affecting all tests. I.e. create a new, cancellable resource for your test.

        create separated cancellable resource for test.

expect(creditCard.$cancelRequest).toBeDefined();

$httpBackend.flush();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we need to make sure It was there before remove it. same as the test next to it (for $promise and $resolved).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be there even before flush $http, isn't it?
If $httpBackend doesn't complain, just remove the flushing.

gkalpak pushed a commit that referenced this pull request Dec 29, 2016
@gkalpak gkalpak closed this in 922a83e Dec 29, 2016
gkalpak pushed a commit that referenced this pull request Dec 29, 2016
ellimist pushed a commit to ellimist/angular.js that referenced this pull request Mar 15, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants