Skip to content

Commit

Permalink
[go_router] Fix redirect being called with empty location for unknown…
Browse files Browse the repository at this point in the history
… routes (flutter#3548)

[go_router] Fix redirect being called with empty location for unknown routes
  • Loading branch information
Amir-P authored Apr 3, 2023
1 parent 03e19a9 commit bc15c97
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 2 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,4 @@ TheOneWithTheBraid <[email protected]>
Rulong Chen(陈汝龙) <[email protected]>
Hwanseok Kang <[email protected]>
Twin Sun, LLC <[email protected]>
Amir Panahandeh <[email protected]>
4 changes: 4 additions & 0 deletions packages/go_router/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 6.5.3

- Fixes redirect being called with an empty location for unknown routes.

## 6.5.2

- NoTransitionPage now has an instant reverse transition
Expand Down
8 changes: 7 additions & 1 deletion packages/go_router/lib/src/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import 'configuration.dart';
import 'delegate.dart';
import 'information_provider.dart';
import 'logging.dart';
import 'match.dart';
import 'matching.dart';
import 'path_utils.dart';
import 'redirection.dart';

/// Converts between incoming URLs and a [RouteMatchList] using [RouteMatcher].
Expand Down Expand Up @@ -63,7 +65,11 @@ class GoRouteInformationParser extends RouteInformationParser<RouteMatchList> {

// If there is a matching error for the initial location, we should
// still try to process the top-level redirects.
initialMatches = RouteMatchList.empty;
initialMatches = RouteMatchList(
<RouteMatch>[],
Uri.parse(canonicalUri(routeInformation.location!)),
const <String, String>{},
);
}
Future<RouteMatchList> processRedirectorResult(RouteMatchList matches) {
if (matches.isEmpty) {
Expand Down
2 changes: 1 addition & 1 deletion packages/go_router/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: go_router
description: A declarative router for Flutter based on Navigation 2 supporting
deep linking, data-driven routes and more
version: 6.5.2
version: 6.5.3
repository: https://github.com/flutter/packages/tree/main/packages/go_router
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+go_router%22

Expand Down
32 changes: 32 additions & 0 deletions packages/go_router/test/parser_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,38 @@ void main() {
'Exception: no routes for location: /def');
});

testWidgets(
'GoRouteInformationParser calls redirector with correct uri when unknown route',
(WidgetTester tester) async {
String? lastRedirectLocation;
final List<GoRoute> routes = <GoRoute>[
GoRoute(
path: '/',
builder: (_, __) => const Placeholder(),
routes: <GoRoute>[
GoRoute(
path: 'abc',
builder: (_, __) => const Placeholder(),
),
],
),
];
final GoRouteInformationParser parser = await createParser(
tester,
routes: routes,
redirectLimit: 100,
redirect: (_, GoRouterState state) {
lastRedirectLocation = state.location;
return null;
},
);

final BuildContext context = tester.element(find.byType(Router<Object>));
await parser.parseRouteInformationWithDependencies(
const RouteInformation(location: '/def'), context);
expect(lastRedirectLocation, '/def');
});

testWidgets('GoRouteInformationParser can work with route parameters',
(WidgetTester tester) async {
final List<GoRoute> routes = <GoRoute>[
Expand Down

0 comments on commit bc15c97

Please sign in to comment.