From dadf5a53c2611ac9b405ea349e11ba562db31662 Mon Sep 17 00:00:00 2001 From: Nikolas Rimikis Date: Sat, 9 Dec 2023 19:50:31 +0100 Subject: [PATCH] feat(dynamite): validate path parameters Signed-off-by: Nikolas Rimikis --- .../dynamite/lib/src/models/openapi/open_api.dart | 12 ++++++++++++ packages/dynamite/dynamite/pubspec.yaml | 1 + .../lib/parameters.openapi.dart | 4 ++-- .../lib/parameters.openapi.json | 4 ++-- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/packages/dynamite/dynamite/lib/src/models/openapi/open_api.dart b/packages/dynamite/dynamite/lib/src/models/openapi/open_api.dart index 56f9be669ad..bc1a54abcd4 100644 --- a/packages/dynamite/dynamite/lib/src/models/openapi/open_api.dart +++ b/packages/dynamite/dynamite/lib/src/models/openapi/open_api.dart @@ -1,11 +1,13 @@ import 'package:built_collection/built_collection.dart'; import 'package:built_value/built_value.dart'; import 'package:built_value/serializer.dart'; +import 'package:dynamite/src/models/exceptions.dart'; import 'package:dynamite/src/models/openapi/components.dart'; import 'package:dynamite/src/models/openapi/info.dart'; import 'package:dynamite/src/models/openapi/path_item.dart'; import 'package:dynamite/src/models/openapi/server.dart'; import 'package:dynamite/src/models/openapi/tag.dart'; +import 'package:uri/uri.dart'; part 'open_api.g.dart'; @@ -42,6 +44,16 @@ abstract class OpenAPI implements Built { ), ); } + + for (final path in b.paths.build().keys) { + try { + UriTemplate(path); + } on ParseException { + throw OpenAPISpecError( + 'Path parameters must only contain alphanumeric characters, underscores or percent encoded values: $path', + ); + } + } } Iterable formattedTagsFor(final String? tag) sync* { diff --git a/packages/dynamite/dynamite/pubspec.yaml b/packages/dynamite/dynamite/pubspec.yaml index 1ee2816fe1a..f8f4cc93a53 100644 --- a/packages/dynamite/dynamite/pubspec.yaml +++ b/packages/dynamite/dynamite/pubspec.yaml @@ -15,6 +15,7 @@ dependencies: intersperse: ^2.0.0 meta: ^1.0.0 path: ^1.0.0 + uri: ^1.0.0 version: ^3.0.0 dev_dependencies: diff --git a/packages/dynamite/dynamite_end_to_end_test/lib/parameters.openapi.dart b/packages/dynamite/dynamite_end_to_end_test/lib/parameters.openapi.dart index a7297abe8f4..458f0b93c3c 100644 --- a/packages/dynamite/dynamite_end_to_end_test/lib/parameters.openapi.dart +++ b/packages/dynamite/dynamite_end_to_end_test/lib/parameters.openapi.dart @@ -153,8 +153,8 @@ class Client extends DynamiteClient { }; Uint8List? body; - pathParameters['path-parameter'] = pathParameter; - var uri = Uri.parse(UriTemplate('/{path-parameter}').expand(pathParameters)); + pathParameters['path_parameter'] = pathParameter; + var uri = Uri.parse(UriTemplate('/{path_parameter}').expand(pathParameters)); if (queryParameters.isNotEmpty) { uri = uri.replace(queryParameters: queryParameters); } diff --git a/packages/dynamite/dynamite_end_to_end_test/lib/parameters.openapi.json b/packages/dynamite/dynamite_end_to_end_test/lib/parameters.openapi.json index 2c850bf5a3a..ecf3fd6713c 100644 --- a/packages/dynamite/dynamite_end_to_end_test/lib/parameters.openapi.json +++ b/packages/dynamite/dynamite_end_to_end_test/lib/parameters.openapi.json @@ -45,10 +45,10 @@ } } }, - "/{path-parameter}": { + "/{path_parameter}": { "parameters": [ { - "name": "path-parameter", + "name": "path_parameter", "in": "path", "required": true, "schema": {