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

fix(linky): throw error if input is not a string #13693

Closed

Conversation

gkalpak
Copy link
Member

@gkalpak gkalpak commented Jan 6, 2016

BREAKING CHANGE:

Before this change, the filter assumed that the input (if not undefined/null) was of type 'string'
and that certain methods (such as .match()) would be available on it. Passing a non-string value
would most likely result in a not-very-useful error being thrown (trying to call a method that does
not exist) or in unexpected behavior (if the input happened to have the assumed methods).

After this change, a proper (informative) error will be thrown. If you want to pass non-string
values through linky, you need to explicitly convert them to strings first.
Since input values could be initialized asynchronously, undefined or null will still be
returned unchanged (without throwing an error).

Closes #13547

@gkalpak
Copy link
Member Author

gkalpak commented Jan 6, 2016

TBH, I don't expect this change to actually break (m)any apps, since an error was thrown anyway (unless non-string inut had a "properly" behaving .match() method, which is unlikely).

We should, of course, keep the BC notice, but I wouldn't be too concerned about it.

@lgalfaso
Copy link
Contributor

lgalfaso commented Jan 6, 2016

just two observations:

  • looks inconsistent (eg not to throw on false but throw on true)
  • due to the BC nature and that we are in the final stages for 1.5, I would push this to 1.6

@petebacondarwin
Copy link
Contributor

I think this is not really much of a BC. It is really just providing a better error message for an invalid input.

Regarding the fact that it does not throw on false: this is standard practice for filters, since often they receive null or undefined initially while waiting for watch values to kick in.

@lgalfaso
Copy link
Contributor

lgalfaso commented Jan 6, 2016

@petebacondarwin I am ok with doing nothing with null/undefined, but handling true and false different, sounds strange

@petebacondarwin
Copy link
Contributor

Yes, I take it back in other filters we now test == null. E.g. https://github.com/angular/angular.js/blob/master/src/ng/filter/orderBy.js#L201
We should do this here too.

@gkalpak
Copy link
Member Author

gkalpak commented Jan 7, 2016

I tried to stay as close as possible to what happens now (in order to minimize the impact of the BC). Currently, falsy values are returned as is (i.e. won't throw an error).

But, I agree the current behavior is not correct. I'll make the suggested change and put this on the 1.6 milestone.

@gkalpak gkalpak modified the milestones: 1.6.x, Backlog Jan 7, 2016
@gkalpak gkalpak force-pushed the fix-linky-throw-on-non-string-values branch from 8cc5da1 to 7ee3586 Compare January 7, 2016 14:35
@gkalpak
Copy link
Member Author

gkalpak commented Jan 7, 2016

Updated, PTAL.

@petebacondarwin
Copy link
Contributor

LGTM

if (!text) return text;
if (text == null) return text;
if (!isString(text)) throw linkyMinErr('notstring', 'Expected string but received: {0}', text);

var match;
Copy link
Contributor

Choose a reason for hiding this comment

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

I am ok one way or another, but should we short-circuit this for the case that text === ''?

Copy link
Contributor

Choose a reason for hiding this comment

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

We might as well. It is in a separate module and so it doesn't impact on the size of the core angular.js file. It saves a few microseconds per digest if the string is empty as we don't need to run the regex or the sanitizer.

@lgalfaso
Copy link
Contributor

lgalfaso commented Jan 7, 2016

One small comment that can be ignored. Otherwise, LGTM

@petebacondarwin
Copy link
Contributor

Since it is barely a much of a breaking change I would like to get this into RC 1 - are there any strong objections to this?

@lgalfaso
Copy link
Contributor

lgalfaso commented Jan 7, 2016

If @petebacondarwin is ok with this making it into 1.5, then I am ok

@petebacondarwin
Copy link
Contributor

Let's add the short-circuit and merge into RC 1

BREAKING CHANGE:

Before this change, the filter assumed that the input (if not undefined/null) was of type 'string'
and that certain methods (such as `.match()`) would be available on it. Passing a non-string value
would most likely result in a not-very-useful error being thrown (trying to call a method that does
not exist) or in unexpected behavior (if the input happened to have the assumed methods).

After this change, a proper (informative) error will be thrown. If you want to pass non-string
values through `linky`, you need to explicitly convert them to strings first.
Since input values could be initialized asynchronously, `undefined` or `null` will still be
returned unchanged (without throwing an error).

Closes angular#13547
@gkalpak gkalpak force-pushed the fix-linky-throw-on-non-string-values branch from 7ee3586 to d49fb5c Compare January 8, 2016 10:51
@gkalpak gkalpak closed this in 98c2db7 Jan 8, 2016
@petebacondarwin
Copy link
Contributor

Smashing!

@petebacondarwin
Copy link
Contributor

@gkalpak - you need to put the "Closes" statements before the "BREAKING CHANGES" statement, because the changelog.js generator doesn't strip them out of the BREAKING CHANGES block.

@gkalpak
Copy link
Member Author

gkalpak commented Jan 8, 2016

Oops, you're right ! Noted ;)

@gkalpak gkalpak deleted the fix-linky-throw-on-non-string-values branch January 11, 2016 10:14
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