Skip to content
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

Consider tightening arguments that are dynamic #375

Closed
natebosch opened this issue Feb 8, 2020 · 6 comments · Fixed by #507
Closed

Consider tightening arguments that are dynamic #375

natebosch opened this issue Feb 8, 2020 · 6 comments · Fixed by #507
Labels
next-breaking-release Issues that are worth doing but need to wait for a breaking version bump

Comments

@natebosch
Copy link
Member

The dynamic url which may be either a String or Uri is not an idiomatic pattern in Dart 2. You lose static checking and are forced to read the doc comment to get help with usage that is normally provided in your IDE. The migration would be easy, shift the Uri.parse call to the call site instead of inside the library code.

The downside here is that there is potentially a lot of code to migrate since this is such a widely used package. That is mitigated somewhat by the fact that you'll get static checking for the migration in the vast majority of cases, only dynamic invocations would be runtime failures.

@natebosch natebosch added the next-breaking-release Issues that are worth doing but need to wait for a breaking version bump label Feb 8, 2020
@jamiewest
Copy link
Contributor

Maybe annotate the old method signature as obsolete and add a new one that only takes a Uri.

@natebosch
Copy link
Member Author

natebosch commented Feb 25, 2020

Adding an alternative would be the typical upgrade route. In this case it would likely leave us with less desirable naming and wouldn't save much cost in the migration. The vast majority of this will be caught statically and the migration is trivial so it doesn't really need an long rollout.

@jakemac53
Copy link
Contributor

jakemac53 commented Sep 29, 2020

Another alternative that came up today would be to add an extension on the Uri class which adds all of these methods, something like:

extension Http on Uri {
  Future<Response> get({Map<String, String> headers}) =>  get(this, headers: headers);
}

This allows for static safety through this api, without conflicting with the existing api.

To accomodate strings we could add a toUri extension on String:

extension Uri on String {
  Uri get toUri => Uri.parse(this);
}

So final usage is like var response = await 'https://www.google.com'.toUri.get() etc.

natebosch added a commit to natebosch/devtools that referenced this issue Dec 8, 2020
natebosch added a commit to dart-archive/googleapis_auth that referenced this issue Dec 8, 2020
natebosch added a commit to dart-archive/googleapis_auth that referenced this issue Dec 8, 2020
natebosch added a commit to dart-archive/googleapis_auth that referenced this issue Dec 8, 2020
natebosch added a commit to flutter/devtools that referenced this issue Dec 8, 2020
natebosch added a commit to dart-lang/http_multi_server that referenced this issue Dec 8, 2020
natebosch added a commit to dart-lang/linter that referenced this issue Dec 8, 2020
natebosch added a commit to dart-lang/shelf that referenced this issue Dec 8, 2020
natebosch added a commit that referenced this issue Dec 8, 2020
Fixes #375

Make all arguments that were `Object` in order to allow either `String`
or `Uri` accept only `Uri`. This gives better static checking for
calling code.
natebosch added a commit to dart-archive/shelf_web_socket that referenced this issue Dec 9, 2020
natebosch added a commit to dart-archive/stagehand that referenced this issue Dec 9, 2020
natebosch added a commit to dart-lang/pub that referenced this issue Dec 9, 2020
natebosch added a commit to flutter/plugins that referenced this issue Dec 9, 2020
natebosch added a commit to dart-lang/http_multi_server that referenced this issue Dec 9, 2020
natebosch added a commit to dart-lang/shelf that referenced this issue Dec 9, 2020
natebosch added a commit to dart-lang/linter that referenced this issue Dec 9, 2020
natebosch added a commit to dart-archive/shelf_web_socket that referenced this issue Dec 9, 2020
devoncarew pushed a commit to dart-archive/stagehand that referenced this issue Dec 9, 2020
Pass a Uri to package:http APIs; prepare for dart-lang/http#375
ditman pushed a commit to flutter/plugins that referenced this issue Dec 11, 2020
ditman pushed a commit to flutter/plugins that referenced this issue Dec 11, 2020
natebosch added a commit to dart-archive/googleapis_auth that referenced this issue Dec 16, 2020
Prepare for dart-lang/http#375

This was missed in #86 for one test but missed here since this file had
not yet been imported to google3 when I looked for cases.
natebosch added a commit to dart-archive/googleapis_auth that referenced this issue Dec 16, 2020
Prepare for dart-lang/http#375

This was fixed in #86 for one test but missed here since this file had
not yet been imported to google3 when I looked for cases.
jonasfj pushed a commit to dart-lang/pub that referenced this issue Dec 21, 2020
@Jonas-Sander
Copy link

Hm, to be honest I really like the convenience with using a String as an URL.
I never had a problem with that.
Also it may be a bit more convoluted for newcomers to the language instead of using a simple String. On the other hand they may be confused today because it's dynamic so they don't know from the IDE that they should/could use a String - they may only assume that.

I think both approaches have pros and cons. In my opinion the real fix would be to wait until there are better ways to express unions - with the current dynamic approach there may not even be breaking changes when unions do finally happen in the future (dynamic may be replaced with String | Uri which should break no code anywhere).

In the end I guess you'll already have made up your mind so idk. Just seems like a big breaking change for me with some amount of "unnecessary" cruft work (adding Uri.parse everywhere).

@jakemac53
Copy link
Contributor

Ya, it is a fairly impactful breaking change, but the migration is very mechanical and you get static errors for all the places that need to be fixed up.

We found a lot of evidence when examining existing code that the existing api was very confusing to folks - many appeared to think it only accepts a String. This lead to a lot of sub-optimal code (several instances of code converting a Uri to a String just to pass it into these functions for instance). Also a lot of people attempting to do their own uri escaping in strings, which if they just used the Uri class to build it up they would not have had to do, etc (not to mention this was never done exhaustively). People were also building up uris in very inconsistent and fragile ways. This won't prevent them from doing that (a common migration will likely involve a simple conversion to use Uri.parse), but generally we actually want people to be building up uris using the Uri class.

So, those are some of the motivations for this change.

@Jonas-Sander
Copy link

Thank you for clarifying:)

natebosch added a commit that referenced this issue Jan 14, 2021
Closes #375

Make all arguments that were `Object` in order to allow either `String`
or `Uri` accept only `Uri`. This gives better static checking for
calling code.
amantoux pushed a commit to amantoux/plugins that referenced this issue Feb 8, 2021
adsonpleal pushed a commit to nubank/plugins that referenced this issue Feb 26, 2021
lovelypuppy0607 added a commit to lovelypuppy0607/stagehand that referenced this issue May 11, 2023
Pass a Uri to package:http APIs; prepare for dart-lang/http#375
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
next-breaking-release Issues that are worth doing but need to wait for a breaking version bump
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants