From 997e269e9078db36be0691e0cf4a7fa9b4bb4354 Mon Sep 17 00:00:00 2001 From: Jhonathan Queiroz Date: Tue, 24 Feb 2026 16:14:02 -0300 Subject: [PATCH] Fix patternToPath to throw when path parameter is missing --- packages/go_router/CHANGELOG.md | 4 ++++ packages/go_router/lib/src/path_utils.dart | 6 +++++- packages/go_router/pubspec.yaml | 2 +- packages/go_router/test/path_utils_test.dart | 14 ++++++++++++++ 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/packages/go_router/CHANGELOG.md b/packages/go_router/CHANGELOG.md index f1d95f90c9fc..150e543ee5e9 100644 --- a/packages/go_router/CHANGELOG.md +++ b/packages/go_router/CHANGELOG.md @@ -1,3 +1,7 @@ +## 17.1.1 + +- Fixes `patternToPath` to throw `GoException` when a path parameter is missing instead of producing URLs containing the literal "null". + ## 17.1.0 - Adds `TypedQueryParameter` annotation to override parameter names in `TypedGoRoute` constructors. diff --git a/packages/go_router/lib/src/path_utils.dart b/packages/go_router/lib/src/path_utils.dart index ed1ce7cc2015..e4e2b323651f 100644 --- a/packages/go_router/lib/src/path_utils.dart +++ b/packages/go_router/lib/src/path_utils.dart @@ -85,7 +85,11 @@ String patternToPath(String pattern, Map 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'); + } + buffer.write(value); start = match.end; } diff --git a/packages/go_router/pubspec.yaml b/packages/go_router/pubspec.yaml index 55ad56b1058c..91402a841cb7 100644 --- a/packages/go_router/pubspec.yaml +++ b/packages/go_router/pubspec.yaml @@ -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: 17.1.0 +version: 17.1.1 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 diff --git a/packages/go_router/test/path_utils_test.dart b/packages/go_router/test/path_utils_test.dart index 6dbf91102676..3bbbb13a632a 100644 --- a/packages/go_router/test/path_utils_test.dart +++ b/packages/go_router/test/path_utils_test.dart @@ -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() { @@ -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 = {'id': '123'}; + expect( + () => patternToPath(pattern, incompleteParams), + throwsA(isA().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);