From e01fa9e1f13a570dc3b75326e210f22cf9b4f117 Mon Sep 17 00:00:00 2001 From: jld3103 Date: Wed, 1 Mar 2023 10:04:01 +0100 Subject: [PATCH] dynamite: Trim tags from method names --- .../dynamite/lib/src/openapi_builder.dart | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/packages/dynamite/lib/src/openapi_builder.dart b/packages/dynamite/lib/src/openapi_builder.dart index ebf680af..49bfb532 100644 --- a/packages/dynamite/lib/src/openapi_builder.dart +++ b/packages/dynamite/lib/src/openapi_builder.dart @@ -573,9 +573,8 @@ class OpenAPIBuilder implements Builder { ...pathParameters, if (operation.parameters != null) ...operation.parameters!, ]; - final methodName = _toDartName(operationId); b - ..name = methodName + ..name = _toDartName(_filterMethodName(operationId, tag ?? '')) ..modifier = MethodModifier.async ..docs.addAll([ ..._descriptionToDocs(operation.summary), @@ -710,7 +709,7 @@ class OpenAPIBuilder implements Builder { final result = resolveType( spec, state, - _toDartName('$methodName-request-$mimeType', uppercaseFirstCharacter: true), + _toDartName('$operationId-request-$mimeType', uppercaseFirstCharacter: true), mediaType.schema!, ); final parameterName = _toDartName(result.name.replaceFirst(prefix, '')); @@ -825,7 +824,7 @@ class OpenAPIBuilder implements Builder { spec, state, _toDartName( - '$methodName-response-$statusCode-$mimeType', + '$operationId-response-$statusCode-$mimeType', uppercaseFirstCharacter: true, ), mediaType.schema!, @@ -1089,6 +1088,18 @@ List _descriptionToDocs(final String? description) => [ ], ]; +String _filterMethodName(final String operationId, final String tag) { + final expandedTag = tag.split('/').toList(); + final parts = operationId.split('-'); + final output = []; + for (var i = 0; i < parts.length; i++) { + if (expandedTag.length <= i || expandedTag[i] != parts[i]) { + output.add(parts[i]); + } + } + return output.join('-'); +} + class State { State(this.prefix);