From 7a599fe0c60024ae2506b78a25352bbd01ef9b46 Mon Sep 17 00:00:00 2001 From: jld3103 Date: Wed, 1 Mar 2023 10:45:51 +0100 Subject: [PATCH] dynamite: Support nested tags --- .../dynamite/lib/src/openapi_builder.dart | 50 ++++++++++++------- 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/packages/dynamite/lib/src/openapi_builder.dart b/packages/dynamite/lib/src/openapi_builder.dart index 033e5dd9..ebf680af 100644 --- a/packages/dynamite/lib/src/openapi_builder.dart +++ b/packages/dynamite/lib/src/openapi_builder.dart @@ -28,7 +28,7 @@ class OpenAPIBuilder implements Builder { throw Exception('Only OpenAPI ${supportedVersions.join(', ')} are supported'); } - final tags = { + var tags = [ null, if (spec.paths != null) ...{ for (final pathItem in spec.paths!.values) ...{ @@ -39,7 +39,22 @@ class OpenAPIBuilder implements Builder { }, }, }, - }; + ]; + for (final tag in tags.toList()) { + final tagPart = tag?.split('/').first; + if (!tags.contains(tagPart)) { + tags.add(tagPart); + } + } + tags = tags + ..sort( + (final a, final b) => a == null + ? -1 + : b == null + ? 1 + : a.compareTo(b), + ); + final hasAnySecurity = spec.security?.isNotEmpty ?? false; final state = State(prefix); @@ -341,10 +356,6 @@ class OpenAPIBuilder implements Builder { } } - if (paths.isEmpty && !isRootClient) { - continue; - } - output.add( Class( (final b) { @@ -446,18 +457,6 @@ class OpenAPIBuilder implements Builder { ), ) ..methods.addAll([ - if (isRootClient) ...[ - for (final tag in tags.where((final tag) => tag != null).toList().cast()) ...[ - Method( - (final b) => b - ..name = _toDartName(tag) - ..lambda = true - ..type = MethodType.getter - ..returns = refer('$prefix${_clientName(tag)}') - ..body = Code('$prefix${_clientName(tag)}(this)'), - ), - ], - ], Method( (final b) => b ..name = 'doRequest' @@ -546,6 +545,21 @@ class OpenAPIBuilder implements Builder { ) ..methods.addAll( [ + for (final t in tags + .whereType() + .where( + (final t) => (tag != null && (t.startsWith('$tag/'))) || (tag == null && !t.contains('/')), + ) + .toList()) ...[ + Method( + (final b) => b + ..name = _toDartName(tag == null ? t : t.substring('$tag/'.length)) + ..lambda = true + ..type = MethodType.getter + ..returns = refer('$prefix${_clientName(t)}') + ..body = Code('$prefix${_clientName(t)}(${isRootClient ? 'this' : 'rootClient'})'), + ), + ], for (final path in paths.keys) ...[ for (final httpMethod in paths[path]!.operations.keys) ...[ Method(