Browse Source

Merge pull request #463 from provokateurin/fix/correctly-await-responses

dynamite: Correctly await responses where necessary
pull/454/head
Kate 1 year ago committed by GitHub
parent
commit
1fda209e98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 11
      packages/dynamite/dynamite/lib/src/openapi_builder.dart

11
packages/dynamite/dynamite/lib/src/openapi_builder.dart

@ -603,6 +603,7 @@ class OpenAPIBuilder implements Builder {
String? dataType;
String? dataValue;
bool? dataNeedsAwait;
if (response.content != null) {
if (response.content!.length > 1) {
throw Exception('Can not work with multiple mime types right now');
@ -627,20 +628,24 @@ class OpenAPIBuilder implements Builder {
mimeType.startsWith('image/')) {
dataType = 'Uint8List';
dataValue = 'response.bodyBytes';
dataNeedsAwait = true;
} else if (mimeType.startsWith('text/')) {
dataType = 'String';
dataValue = 'await response.body';
dataValue = 'response.body';
dataNeedsAwait = true;
} else if (mimeType == 'application/json') {
dataType = result.name;
if (result.name == 'dynamic') {
dataValue = '';
} else if (result.name == 'String') {
// avoid unnecessary_await_in_return lint
dataValue = 'response.body';
dataNeedsAwait = true;
} else if (result is TypeResultEnum || result is TypeResultBase) {
dataValue = result.deserialize(result.decode('await response.body'));
dataNeedsAwait = false;
} else {
dataValue = result.deserialize('await response.jsonBody');
dataNeedsAwait = false;
}
} else {
throw Exception('Can not parse mime type "$mimeType"');
@ -651,7 +656,7 @@ class OpenAPIBuilder implements Builder {
if (headersType != null && dataType != null) {
b.returns = refer('Future<${classPrefix}Response<$dataType, $headersType>>');
code.write(
'return ${classPrefix}Response<$dataType, $headersType>($dataValue, $headersValue,);',
'return ${classPrefix}Response<$dataType, $headersType>(${dataNeedsAwait ?? false ? 'await ' : ''}$dataValue, $headersValue,);',
);
} else if (headersType != null) {
b.returns = refer('Future<$headersType>');

Loading…
Cancel
Save