From 79e2f5e1c0995eed0d762c21909f61d64ade09a6 Mon Sep 17 00:00:00 2001 From: Nikolas Rimikis Date: Wed, 25 Oct 2023 16:28:52 +0200 Subject: [PATCH] fix(dynamite): fix header name generation with empty operationID Signed-off-by: Nikolas Rimikis --- .../dynamite/lib/src/builder/client.dart | 38 ++++++++++++------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/packages/dynamite/dynamite/lib/src/builder/client.dart b/packages/dynamite/dynamite/lib/src/builder/client.dart index e5490b4d..51e5bf5e 100644 --- a/packages/dynamite/dynamite/lib/src/builder/client.dart +++ b/packages/dynamite/dynamite/lib/src/builder/client.dart @@ -183,19 +183,19 @@ Iterable buildTags( for (final operationEntry in pathEntry.value.operations.entries) { final httpMethod = operationEntry.key.name; final operation = operationEntry.value; - final operationId = operation.operationId ?? toDartName('$httpMethod-${pathEntry.key}'); + final operationName = operation.operationId ?? toDartName('$httpMethod-${pathEntry.key}'); final parameters = [ ...?pathEntry.value.parameters, ...?operation.parameters, ]..sort(sortRequiredParameters); - final name = toDartName(filterMethodName(operationId, tag ?? '')); + final name = toDartName(filterMethodName(operationName, tag ?? '')); var responses = >{}; if (operation.responses != null) { for (final responseEntry in operation.responses!.entries) { final statusCode = int.tryParse(responseEntry.key); if (statusCode == null) { - print('Default responses are not supported right now. Skipping it for $operationId'); + print('Default responses are not supported right now. Skipping it for $operationName'); continue; } final response = responseEntry.value; @@ -205,7 +205,7 @@ Iterable buildTags( } if (responses.length > 1) { - print('$operationId uses more than one response schema but we only generate the first one'); + print('$operationName uses more than one response schema but we only generate the first one'); responses = Map.fromEntries([responses.entries.first]); } } @@ -251,7 +251,7 @@ Iterable buildTags( spec, state, toDartName( - '$operationId-${parameter.name}', + '$operationName-${parameter.name}', uppercaseFirstCharacter: true, ), parameter.schema!, @@ -277,20 +277,26 @@ Iterable buildTags( buildPatternCheck(result, parameter).forEach(code.writeln); buildParameterSerialization(result, parameter).forEach(code.writeln); } - resolveMimeTypeEncode(operation, spec, state, operationId, operationParameters).forEach(code.writeln); + resolveMimeTypeEncode(operation, spec, state, operationName, operationParameters).forEach(code.writeln); for (final responseEntry in responses.entries) { final response = responseEntry.key; final statusCodes = responseEntry.value; TypeResult? headersType; + if (response.headers != null) { - final identifier = - '${tag != null ? toDartName(tag, uppercaseFirstCharacter: true) : null}${toDartName(operationId, uppercaseFirstCharacter: true)}Headers'; + final identifierBuilder = StringBuffer(); + if (tag != null) { + identifierBuilder.write(toDartName(tag, uppercaseFirstCharacter: true)); + } + identifierBuilder + ..write(toDartName(operationName, uppercaseFirstCharacter: true)) + ..write('Headers'); headersType = resolveObject( spec, state, - identifier, + identifierBuilder.toString(), openapi.Schema( (final b) => b ..properties.replace( @@ -306,14 +312,20 @@ Iterable buildTags( ); } + final identifierBuilder = StringBuffer() + ..write(operationName) + ..write('-response'); + if (responses.entries.length > 1) { + identifierBuilder + ..write('-') + ..write(responses.entries.toList().indexOf(responseEntry)); + } + final dataType = resolveMimeTypeDecode( response, spec, state, - toDartName( - '$operationId-response${responses.entries.length > 1 ? '-${responses.entries.toList().indexOf(responseEntry)}' : ''}', - uppercaseFirstCharacter: true, - ), + toDartName(identifierBuilder.toString(), uppercaseFirstCharacter: true), ); var path = pathEntry.key;