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

fix(orderBy): Throw exception when orderBy is given a non array-like object. #11719

Closed
wants to merge 6 commits into from

Conversation

nhodges
Copy link
Contributor

@nhodges nhodges commented Apr 24, 2015

Throws exception when orderBy is given a non array-like object to sort.

Closes #11255.

Review on Reviewable

@petebacondarwin petebacondarwin added this to the 1.5.x - migration-facilitation milestone Jun 30, 2015
@@ -155,7 +155,7 @@
orderByFilter.$inject = ['$parse'];
function orderByFilter($parse) {
return function(array, sortPredicate, reverseOrder) {
if (!(isArrayLike(array))) return array;
if (!(isArrayLike(array))) throw minErr('filter')('notarray', 'Expected array but received: {0}', array);
Copy link
Contributor

Choose a reason for hiding this comment

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

Sadly this error code is specific to the filterFilter so we have to provide a new one and associated documentation in docs/content/error or alternatively update the documentation to make this error code more generic.

@fhernandezn
Copy link

@nhodges @petebacondarwin Hi guys, I've been working in the documentation needed for this pull request. Please look at this

master...frankweb:orderby-notarray-documentation

I'd like to contribute to this pull request by providing documentation.

@nhodges
Copy link
Contributor Author

nhodges commented Oct 12, 2015

👍 @Frankweb for providing documentation, I updated my PR to be fast forwardable.

@fhernandezn
Copy link

@nhodges 👍 do you want to include my documentation in your PR?

@googlebot
Copy link

We found a Contributor License Agreement for you (the sender of this pull request) and all commit authors, but as best as we can tell these commits were authored by someone else. If that's the case, please add them to this pull request and have them confirm that they're okay with these commits being contributed to Google. If we're mistaken and you did author these commits, just reply here to confirm.

@nhodges
Copy link
Contributor Author

nhodges commented Oct 13, 2015

@Frankweb You got it :)

@nhodges
Copy link
Contributor Author

nhodges commented Oct 13, 2015

@Frankweb looks like the task to build the docs is failing after merging, can you take a look?

@fhernandezn
Copy link

@nhodges Of course, I'll take a look in a while :)

@@ -177,7 +177,7 @@ orderByFilter.$inject = ['$parse'];
function orderByFilter($parse) {
return function(array, sortPredicate, reverseOrder) {

if (!(isArrayLike(array))) return array;
if (!(isArrayLike(array))) throw minErr('filter')('notarray', 'Expected array but received: {0}', array);

Choose a reason for hiding this comment

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

grunt test runs without errors if I change minErr('filter') to minErr('orderBy')

@nhodges
Copy link
Contributor Author

nhodges commented Oct 14, 2015

👍 @Frankweb thanks for the heads up. Fixed and we have a green build again.

@gkalpak
Copy link
Member

gkalpak commented Oct 15, 2015

@nhodges, could you take care of the unresolved comments ?

},
three: {
name: 'something 3'
},
Copy link
Member

Choose a reason for hiding this comment

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

Also, the indentation is funny for this object. TBH, I would change the example to something like:

<!-- HTML -->
<label>
  Order by:
  <select ng-model="orderProp" ng-options="prop for prop in ['id', 'name']"></select>
</label>
<div ng-repeat="(key, value) in myObj | orderBy:orderProp">
  {{ key }} : {{ value }}
</div>
/* JS */
angular.module('aModule', [])
  .controller('aController', function($scope) {
    var myObj = {
      one: {id: 1, name: 'Some thing'},
      two: {id: 2, name: 'Another thing'},
      three: {id: 3, name: 'A third thing'}
    };

    $scope.arrFromMyObj = Object.keys(myObj).map(function(key) {
      return myObj[key];
    });
  });

@nhodges
Copy link
Contributor Author

nhodges commented Oct 15, 2015

@gkalpak You got it. Pushed.

@nhodges
Copy link
Contributor Author

nhodges commented Oct 19, 2015

@gkalpak @petebacondarwin @Frankweb

Anything else I can take care of?

@fhernandezn
Copy link

@nhodges Sorry for the last comment, I already deleted it, my comment was about other PR.

@fhernandezn
Copy link

There is only one problem with the PR, I think all the commits should be made from one account, since you merged my branch this PR has commits with two accounts. Because of this, cla label says no @Narretz @petebacondarwin should @nhodges do another PR or what should he do?

@nhodges
Copy link
Contributor Author

nhodges commented Oct 20, 2015

Hey Francisco,

I think if you just sign the CLA should be good?

Made with thumbs.

On Oct 19, 2015, at 5:38 PM, Francisco Hernández notifications@github.com wrote:

There is only one problem with the PR, I think all the commits should be made from one account, since you merged my branch this PR has commits with two accounts. Because of this, cla label says no @Narretz @petebacondarwin should @nhodges do another PR or what should he do?


Reply to this email directly or view it on GitHub.

@fhernandezn
Copy link

@nhodges I guess we should comment something to the googlebot, but I don't know exactly what.

@fhernandezn
Copy link

@googlebot I'm okay with these commits.

@fhernandezn
Copy link

@googlebot I authored these commits

@nhodges
Copy link
Contributor Author

nhodges commented Oct 20, 2015

@Frankweb

Hey Francisco, can you try clicking the link below and filling out the form?

http://code.google.com/legal/individual-cla-v1.0.html

Thanks!

@fhernandezn
Copy link

@nhodges Of course, but I'm already signed in. I'll try again.

@petebacondarwin
Copy link
Contributor

@nhodges and @Frankweb - as long as you have both signed the CLA then this is fine. The Google Bot always gets confused when a PR contains commits from more than one author.

@fhernandezn
Copy link

@petebacondarwin Thank you it's good to know that. Do you need another change to this PR?

</div>
```

orderBy must be used with an array so a subset of items can be returned.
Copy link
Contributor

Choose a reason for hiding this comment

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

orderBy must be used with an array-like value so a subset of items can be returned.

@petebacondarwin
Copy link
Contributor

I have added a few minor comments.
Can you also update the documentation comment to make it clear that people can pass in array-like objects and that a non-array like object will result in an error?
Finally, since this is a breaking change, we need a BREAKING CHANGES block in one of the commits, if you could provide one.
We can squash all these commits together when we merge. It is likely to go into one commit. Can @nhodges and @Frankweb agree on who would be the final author of this?

@nhodges
Copy link
Contributor Author

nhodges commented Oct 23, 2015

@petebacondarwin @Frankweb

I went ahead and took care of the requested changes to docs and added a test that takes in a strong and returns a sorted array.

@fhernandezn
Copy link

@nhodges Good! Please squash the commits, this is your pull request.

Order by:
<select ng-model="orderProp" ng-options="prop for prop in ['id', 'name']"></select>
</label>
<div ng-repeat="(key, value) in myObj | orderBy:orderProp">
Copy link
Member

Choose a reason for hiding this comment

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

myObj --> arrFromMyObj

@gkalpak
Copy link
Member

gkalpak commented Oct 27, 2015

With #11719 (comment), I think we are good to go 😄
(I left couple of minor comments (typos), but I will take care of them while merging.)

Thx @nhodges and @Frankweb (and @petebacondarwin) for working on this one !

@gkalpak
Copy link
Member

gkalpak commented Oct 27, 2015

@nhodges, actually could you rebase your changes on master. It's been a long time since the first commit...

nhodges and others added 5 commits October 28, 2015 10:21
BREAKING CHANGES:

These changes alter the behavior of AngularJS such that it will no
longer throw an error for non array values, as it will accept
array-like values as well. It will throw an error if no array-like
value is passed.
@nhodges
Copy link
Contributor Author

nhodges commented Oct 28, 2015

@gkalpak rebased. LMK if I can do anything else or if you want me to take care of the changes above.

@@ -177,7 +177,7 @@ orderByFilter.$inject = ['$parse'];
function orderByFilter($parse) {
return function(array, sortPredicate, reverseOrder) {

if (!(isArrayLike(array))) return array;
if (!(isArrayLike(array))) throw minErr('orderBy')('notarray', 'Expected array but received: {0}', array);
Copy link
Member

Choose a reason for hiding this comment

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

I was in the middle of merging when I realized there is an issue here: We need to allow null/undefined (in order to support asnc loading).
This block needs to be modified (see filter#L132-L138 for an example).

Choose a reason for hiding this comment

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

@gkalpak I guess something like

if(!array) return array;

if (!(isArrayLike(array))) throw minErr('orderBy')('notarray', 'Expected array but received: {0}', array);

would be easier to read than the example.

This is just an idea,of course :) what do you think about that?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think that is about right and similar to what we do in other filters.

Copy link
Member

Choose a reason for hiding this comment

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

In filterFilter we only allow null, undefined and array-like (and I think that should we do here as well). So it would be more like:

if (array == null) return array;

@gkalpak
Copy link
Member

gkalpak commented Oct 28, 2015

@nhodges, there seems to be one last little thing to do 😄
Could you take care of #11719 (comment) and add a test that verifies null/undefined do not result in an error ?

@nhodges
Copy link
Contributor Author

nhodges commented Oct 29, 2015

@gkalpak Sure thing, please take a look at the latest changeset. Cheers!

@gkalpak gkalpak closed this in 2a85a63 Oct 31, 2015
@gkalpak
Copy link
Member

gkalpak commented Oct 31, 2015

I updated the documentation (per #11719 (comment)), made the change described in #11719 (comment), fixed some typos in the error message and merged !

Well done everyone 😃

gkalpak pushed a commit to gkalpak/angular.js that referenced this pull request Nov 23, 2015
BREAKING CHANGE:
Previously, an non array-like input would pass through the orderBy filter
unchanged.
Now, an error is thrown. This can be worked around by converting an object
to an array, either manually or using a filter such as
https://github.com/petebacondarwin/angular-toArrayFilter.
(`null` and `undefined` still pass through without an error, in order to
support asynchronous loading of resources.)

Closes angular#11255
Closes angular#11719
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Using orderBy on an object should cause an error or warning
5 participants