Browse Source

fix(dynamite): fix header name generation with empty operationID

Signed-off-by: Nikolas Rimikis <leptopoda@users.noreply.github.com>
pull/1024/head
Nikolas Rimikis 1 year ago
parent
commit
79e2f5e1c0
No known key found for this signature in database
GPG Key ID: 85ED1DE9786A4FF2
  1. 38
      packages/dynamite/dynamite/lib/src/builder/client.dart

38
packages/dynamite/dynamite/lib/src/builder/client.dart

@ -183,19 +183,19 @@ Iterable<Method> buildTags(
for (final operationEntry in pathEntry.value.operations.entries) { for (final operationEntry in pathEntry.value.operations.entries) {
final httpMethod = operationEntry.key.name; final httpMethod = operationEntry.key.name;
final operation = operationEntry.value; final operation = operationEntry.value;
final operationId = operation.operationId ?? toDartName('$httpMethod-${pathEntry.key}'); final operationName = operation.operationId ?? toDartName('$httpMethod-${pathEntry.key}');
final parameters = [ final parameters = [
...?pathEntry.value.parameters, ...?pathEntry.value.parameters,
...?operation.parameters, ...?operation.parameters,
]..sort(sortRequiredParameters); ]..sort(sortRequiredParameters);
final name = toDartName(filterMethodName(operationId, tag ?? '')); final name = toDartName(filterMethodName(operationName, tag ?? ''));
var responses = <openapi.Response, List<int>>{}; var responses = <openapi.Response, List<int>>{};
if (operation.responses != null) { if (operation.responses != null) {
for (final responseEntry in operation.responses!.entries) { for (final responseEntry in operation.responses!.entries) {
final statusCode = int.tryParse(responseEntry.key); final statusCode = int.tryParse(responseEntry.key);
if (statusCode == null) { 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; continue;
} }
final response = responseEntry.value; final response = responseEntry.value;
@ -205,7 +205,7 @@ Iterable<Method> buildTags(
} }
if (responses.length > 1) { 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]); responses = Map.fromEntries([responses.entries.first]);
} }
} }
@ -251,7 +251,7 @@ Iterable<Method> buildTags(
spec, spec,
state, state,
toDartName( toDartName(
'$operationId-${parameter.name}', '$operationName-${parameter.name}',
uppercaseFirstCharacter: true, uppercaseFirstCharacter: true,
), ),
parameter.schema!, parameter.schema!,
@ -277,20 +277,26 @@ Iterable<Method> buildTags(
buildPatternCheck(result, parameter).forEach(code.writeln); buildPatternCheck(result, parameter).forEach(code.writeln);
buildParameterSerialization(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) { for (final responseEntry in responses.entries) {
final response = responseEntry.key; final response = responseEntry.key;
final statusCodes = responseEntry.value; final statusCodes = responseEntry.value;
TypeResult? headersType; TypeResult? headersType;
if (response.headers != null) { if (response.headers != null) {
final identifier = final identifierBuilder = StringBuffer();
'${tag != null ? toDartName(tag, uppercaseFirstCharacter: true) : null}${toDartName(operationId, uppercaseFirstCharacter: true)}Headers'; if (tag != null) {
identifierBuilder.write(toDartName(tag, uppercaseFirstCharacter: true));
}
identifierBuilder
..write(toDartName(operationName, uppercaseFirstCharacter: true))
..write('Headers');
headersType = resolveObject( headersType = resolveObject(
spec, spec,
state, state,
identifier, identifierBuilder.toString(),
openapi.Schema( openapi.Schema(
(final b) => b (final b) => b
..properties.replace( ..properties.replace(
@ -306,14 +312,20 @@ Iterable<Method> buildTags(
); );
} }
final identifierBuilder = StringBuffer()
..write(operationName)
..write('-response');
if (responses.entries.length > 1) {
identifierBuilder
..write('-')
..write(responses.entries.toList().indexOf(responseEntry));
}
final dataType = resolveMimeTypeDecode( final dataType = resolveMimeTypeDecode(
response, response,
spec, spec,
state, state,
toDartName( toDartName(identifierBuilder.toString(), uppercaseFirstCharacter: true),
'$operationId-response${responses.entries.length > 1 ? '-${responses.entries.toList().indexOf(responseEntry)}' : ''}',
uppercaseFirstCharacter: true,
),
); );
var path = pathEntry.key; var path = pathEntry.key;

Loading…
Cancel
Save