Browse Source

feat(dynamite): Support multiple status codes with same schema

Signed-off-by: jld3103 <jld3103yt@gmail.com>
pull/715/head
jld3103 1 year ago
parent
commit
033e1088e9
No known key found for this signature in database
GPG Key ID: 9062417B9E8EB7B3
  1. 42
      packages/dynamite/dynamite/lib/src/builder/client.dart

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

@ -8,6 +8,7 @@ import 'package:dynamite/src/helpers/dynamite.dart';
import 'package:dynamite/src/helpers/type_result.dart'; import 'package:dynamite/src/helpers/type_result.dart';
import 'package:dynamite/src/models/open_api.dart'; import 'package:dynamite/src/models/open_api.dart';
import 'package:dynamite/src/models/path_item.dart'; import 'package:dynamite/src/models/path_item.dart';
import 'package:dynamite/src/models/response.dart';
import 'package:dynamite/src/models/schema.dart'; import 'package:dynamite/src/models/schema.dart';
import 'package:dynamite/src/type_result/type_result.dart'; import 'package:dynamite/src/type_result/type_result.dart';
@ -297,13 +298,28 @@ Iterable<Method> buildTags(
b.annotations.add(refer('Deprecated').call([refer("''")])); b.annotations.add(refer('Deprecated').call([refer("''")]));
} }
final acceptHeader = operation.responses?.values var responses = <Response, List<int>>{};
if (operation.responses != null) {
for (final responseEntry in operation.responses!.entries) {
final statusCode = int.parse(responseEntry.key);
final response = responseEntry.value;
responses[response] ??= [];
responses[response]!.add(statusCode);
}
if (responses.length > 1) {
print('$operationId uses more than one response schema but we only generate the first one');
responses = Map.fromEntries([responses.entries.first]);
}
}
final acceptHeader = responses.keys
.map((final response) => response.content?.keys) .map((final response) => response.content?.keys)
.whereNotNull() .whereNotNull()
.expand((final element) => element) .expand((final element) => element)
.toSet() .toSet()
.join(',') ?? .join(',');
'';
final code = StringBuffer(''' final code = StringBuffer('''
var _path = '${pathEntry.key}'; var _path = '${pathEntry.key}';
final _queryParameters = <String, dynamic>{}; final _queryParameters = <String, dynamic>{};
@ -497,14 +513,12 @@ final _response = await ${isRootClient ? 'this' : '_rootClient'}.doRequest(
''', ''',
); );
if (operation.responses != null) { for (final responseEntry in responses.entries) {
if (operation.responses!.length > 1) { final response = responseEntry.key;
throw Exception('Can not work with multiple status codes right now'); final statusCodes = responseEntry.value;
} code.write(
for (final responseEntry in operation.responses!.entries) { 'if (${statusCodes.map((final statusCode) => '_response.statusCode == $statusCode').join(' || ')}) {',
final statusCode = responseEntry.key; );
final response = responseEntry.value;
code.write('if (_response.statusCode == $statusCode) {');
String? headersType; String? headersType;
String? headersValue; String? headersValue;
@ -544,7 +558,7 @@ final _response = await ${isRootClient ? 'this' : '_rootClient'}.doRequest(
spec, spec,
state, state,
toDartName( toDartName(
'$operationId-response-$statusCode-$mimeType', '$operationId-response${responses.entries.length > 1 ? '-${responses.entries.toList().indexOf(responseEntry)}' : ''}-$mimeType',
uppercaseFirstCharacter: true, uppercaseFirstCharacter: true,
), ),
mediaType.schema!, mediaType.schema!,
@ -599,9 +613,7 @@ final _response = await ${isRootClient ? 'this' : '_rootClient'}.doRequest(
code.write( code.write(
'throw await ${state.classPrefix}ApiException.fromResponse(_response); // coverage:ignore-line\n', 'throw await ${state.classPrefix}ApiException.fromResponse(_response); // coverage:ignore-line\n',
); );
} else {
b.returns = refer('Future');
}
b.body = Code(code.toString()); b.body = Code(code.toString());
}, },
); );

Loading…
Cancel
Save