Skip to content

Commit 1dad759

Browse files
authored
[go_router] flutter#127016 preserved route name case when caching _nameToPath (flutter#4196)
preserved route name case when caching `_nameToPath` by not using `toLowerCase()` Related issue: [flutter#127016](flutter#127016)
1 parent 0507297 commit 1dad759

File tree

6 files changed

+84
-12
lines changed

6 files changed

+84
-12
lines changed

packages/go_router/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 8.0.3
2+
3+
- Makes namedLocation and route name related APIs case sensitive.
4+
15
## 8.0.2
26

37
- Fixes a bug in `debugLogDiagnostics` to support StatefulShellRoute.

packages/go_router/lib/src/configuration.dart

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,8 @@ class RouteConfiguration {
221221
'${queryParameters.isEmpty ? '' : ', queryParameters: $queryParameters'}');
222222
return true;
223223
}());
224-
final String keyName = name.toLowerCase();
225-
assert(_nameToPath.containsKey(keyName), 'unknown route name: $name');
226-
final String path = _nameToPath[keyName]!;
224+
assert(_nameToPath.containsKey(name), 'unknown route name: $name');
225+
final String path = _nameToPath[name]!;
227226
assert(() {
228227
// Check that all required params are present
229228
final List<String> paramNames = <String>[];
@@ -564,7 +563,7 @@ class RouteConfiguration {
564563
final String fullPath = concatenatePaths(parentFullPath, route.path);
565564

566565
if (route.name != null) {
567-
final String name = route.name!.toLowerCase();
566+
final String name = route.name!;
568567
assert(
569568
!_nameToPath.containsKey(name),
570569
'duplication fullpaths for name '

packages/go_router/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: go_router
22
description: A declarative router for Flutter based on Navigation 2 supporting
33
deep linking, data-driven routes and more
4-
version: 8.0.2
4+
version: 8.0.3
55
repository: https://github.com/flutter/packages/tree/main/packages/go_router
66
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+go_router%22
77

packages/go_router/test/go_router_test.dart

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,8 +1493,7 @@ void main() {
14931493
}, throwsA(isAssertionError));
14941494
});
14951495

1496-
testWidgets('match case insensitive w/ params',
1497-
(WidgetTester tester) async {
1496+
testWidgets('cannot match case insensitive', (WidgetTester tester) async {
14981497
final List<GoRoute> routes = <GoRoute>[
14991498
GoRoute(
15001499
name: 'home',
@@ -1524,8 +1523,15 @@ void main() {
15241523
];
15251524

15261525
final GoRouter router = await createRouter(routes, tester);
1527-
router.goNamed('person',
1528-
pathParameters: <String, String>{'fid': 'f2', 'pid': 'p1'});
1526+
expect(
1527+
() {
1528+
router.goNamed(
1529+
'person',
1530+
pathParameters: <String, String>{'fid': 'f2', 'pid': 'p1'},
1531+
);
1532+
},
1533+
throwsAssertionError,
1534+
);
15291535
});
15301536

15311537
testWidgets('too few params', (WidgetTester tester) async {
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter/material.dart';
6+
import 'package:flutter_test/flutter_test.dart';
7+
import 'package:go_router/go_router.dart';
8+
9+
void main() {
10+
testWidgets(
11+
'Route names are case sensitive',
12+
(WidgetTester tester) async {
13+
// config router with 2 routes with the same name but different case (Name, name)
14+
final GoRouter router = GoRouter(
15+
routes: <GoRoute>[
16+
GoRoute(
17+
path: '/',
18+
name: 'Name',
19+
builder: (_, __) => const ScreenA(),
20+
),
21+
GoRoute(
22+
path: '/path',
23+
name: 'name',
24+
builder: (_, __) => const ScreenB(),
25+
),
26+
],
27+
);
28+
29+
// run MaterialApp, initial screen path is '/' -> ScreenA
30+
await tester.pumpWidget(
31+
MaterialApp.router(
32+
routerConfig: router,
33+
title: 'GoRouter Testcase',
34+
),
35+
);
36+
37+
// go to ScreenB
38+
router.goNamed('name');
39+
await tester.pumpAndSettle();
40+
expect(find.byType(ScreenB), findsOneWidget);
41+
42+
// go to ScreenA
43+
router.goNamed('Name');
44+
await tester.pumpAndSettle();
45+
expect(find.byType(ScreenA), findsOneWidget);
46+
},
47+
);
48+
}
49+
50+
class ScreenA extends StatelessWidget {
51+
const ScreenA({super.key});
52+
53+
@override
54+
Widget build(BuildContext context) {
55+
return Container();
56+
}
57+
}
58+
59+
class ScreenB extends StatelessWidget {
60+
const ScreenB({super.key});
61+
62+
@override
63+
Widget build(BuildContext context) {
64+
return Container();
65+
}
66+
}

packages/go_router/test/parser_test.dart

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,8 @@ void main() {
118118
);
119119

120120
expect(configuration.namedLocation('lowercase'), '/abc');
121-
expect(configuration.namedLocation('LOWERCASE'), '/abc');
122121
expect(configuration.namedLocation('camelCase'), '/efg');
123-
expect(configuration.namedLocation('camelcase'), '/efg');
124122
expect(configuration.namedLocation('snake_case'), '/hij');
125-
expect(configuration.namedLocation('SNAKE_CASE'), '/hij');
126123

127124
// With query parameters
128125
expect(configuration.namedLocation('lowercase'), '/abc');

0 commit comments

Comments
 (0)