Skip to content

Commit

Permalink
feat(dynamite): validate path parameters
Browse files Browse the repository at this point in the history
Signed-off-by: Nikolas Rimikis <[email protected]>
  • Loading branch information
Leptopoda committed Dec 9, 2023
1 parent 7a3ce91 commit dadf5a5
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
12 changes: 12 additions & 0 deletions packages/dynamite/dynamite/lib/src/models/openapi/open_api.dart
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -42,6 +44,16 @@ abstract class OpenAPI implements Built<OpenAPI, OpenAPIBuilder> {
),
);
}

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<String> formattedTagsFor(final String? tag) sync* {
Expand Down
1 change: 1 addition & 0 deletions packages/dynamite/dynamite/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@
}
}
},
"/{path-parameter}": {
"/{path_parameter}": {
"parameters": [
{
"name": "path-parameter",
"name": "path_parameter",
"in": "path",
"required": true,
"schema": {
Expand Down

0 comments on commit dadf5a5

Please sign in to comment.