Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/go_router/lib/src/path_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ String patternToPath(String pattern, Map<String, String> pathParameters) {
buffer.write(pattern.substring(start, match.start));
}
final String name = match[1]!;
buffer.write(pathParameters[name]);
final String? value = pathParameters[name];
if (value == null) {
throw GoException('Missing path parameter: $name for pattern "$pattern"');
}
buffer.write(value);
start = match.end;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
changelog: |
- Fixes `patternToPath` to throw `GoException` when a path parameter is missing instead of producing URLs containing the literal "null".
version: patch
14 changes: 14 additions & 0 deletions packages/go_router/test/path_utils_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// found in the LICENSE file.

import 'package:flutter_test/flutter_test.dart';
import 'package:go_router/go_router.dart' show GoException;
import 'package:go_router/src/path_utils.dart';

void main() {
Expand Down Expand Up @@ -95,6 +96,19 @@ void main() {
expect(url, restoredUrl);
});

test('patternToPath throws when path parameter is missing', () {
const pattern = '/user/:id/book/:bookId';
final incompleteParams = <String, String>{'id': '123'};
expect(
() => patternToPath(pattern, incompleteParams),
throwsA(isA<GoException>().having(
(GoException e) => e.message,
'message',
contains('Missing path parameter: bookId'),
)),
);
});

test('concatenatePaths', () {
void verify(String pathA, String pathB, String expected) {
final String result = concatenatePaths(pathA, pathB);
Expand Down