From d967ec0bb6b63b6eb9af1ebf9181ef3a48e8b5cd Mon Sep 17 00:00:00 2001 From: jld3103 Date: Tue, 21 Nov 2023 21:17:48 +0100 Subject: [PATCH] feat(dynamite): Use URI templates for path parameters Signed-off-by: jld3103 --- .../dynamite/lib/src/builder/client.dart | 27 +++++++++---------- .../dynamite/lib/src/builder/imports.dart | 1 + 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/packages/dynamite/dynamite/lib/src/builder/client.dart b/packages/dynamite/dynamite/lib/src/builder/client.dart index 2a72388f..3d1033f0 100644 --- a/packages/dynamite/dynamite/lib/src/builder/client.dart +++ b/packages/dynamite/dynamite/lib/src/builder/client.dart @@ -215,6 +215,7 @@ Iterable buildTags( .join(','); code.writeln(''' + final _pathParameters = {}; final _queryParameters = {}; final _headers = {${acceptHeader.isNotEmpty ? "'Accept': '$acceptHeader'," : ''}}; Uint8List? _body; @@ -323,17 +324,12 @@ Iterable buildTags( toDartName(identifierBuilder.toString(), uppercaseFirstCharacter: true), ); - var path = pathEntry.key; - for (final pathParameter in parameters.where((final p) => p.$in == openapi.ParameterType.path)) { - path = path.replaceAll('{${pathParameter.name}}', '\$_${toDartName(pathParameter.name)}'); - } - - code.writeln( - ''' -final _path = '$path'; -final _uri = Uri(path: _path, queryParameters: _queryParameters.isNotEmpty ? _queryParameters : null); -''', - ); + code.writeln(''' +var _uri = Uri.parse(UriTemplate('${pathEntry.key}').expand(_pathParameters)); +if (_queryParameters.isNotEmpty) { + _uri = _uri.replace(queryParameters: _queryParameters); +} +'''); if (dataType != null) { returnDataType = dataType.name; @@ -429,12 +425,13 @@ Iterable buildParameterSerialization( onlyChildren: parameter.$in == openapi.ParameterType.query, ); - final assignment = switch (parameter.$in) { - openapi.ParameterType.path => 'final _${toDartName(parameter.name)} = Uri.encodeQueryComponent($value);', - openapi.ParameterType.query => "_queryParameters['${parameter.name}'] = $value;", - openapi.ParameterType.header => "_headers['${parameter.name}'] = $value;", + final mapName = switch (parameter.$in) { + openapi.ParameterType.path => '_pathParameters', + openapi.ParameterType.query => '_queryParameters', + openapi.ParameterType.header => '_headers', _ => throw UnsupportedError('Can not work with parameter "${parameter.name}" in "${parameter.$in}"'), }; + final assignment = "$mapName['${parameter.name}'] = $value;"; if (!parameter.required && (result.nullable || hasDefault)) { yield 'if( '; diff --git a/packages/dynamite/dynamite/lib/src/builder/imports.dart b/packages/dynamite/dynamite/lib/src/builder/imports.dart index 9c8fd78a..bdd3bf51 100644 --- a/packages/dynamite/dynamite/lib/src/builder/imports.dart +++ b/packages/dynamite/dynamite/lib/src/builder/imports.dart @@ -27,6 +27,7 @@ Iterable generateImports(final AssetId outputId, final State state) sync* Directive.import('package:dynamite_runtime/utils.dart'), Directive.import('package:meta/meta.dart'), Directive.import('package:universal_io/io.dart'), + Directive.import('package:uri/uri.dart'), const Code(''), ];