Browse Source

fix(dynamite): Fix conflicting variables

Signed-off-by: jld3103 <jld3103yt@gmail.com>
pull/582/head
jld3103 1 year ago
parent
commit
768f8edaf9
No known key found for this signature in database
GPG Key ID: 9062417B9E8EB7B3
  1. 52
      packages/dynamite/dynamite/lib/src/openapi_builder.dart

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

@ -276,7 +276,7 @@ class OpenAPIBuilder implements Builder {
..fields.add( ..fields.add(
Field( Field(
(final b) => b (final b) => b
..name = 'rootClient' ..name = '_rootClient'
..type = refer('${classPrefix}Client') ..type = refer('${classPrefix}Client')
..modifier = FieldModifier.final$, ..modifier = FieldModifier.final$,
), ),
@ -286,7 +286,7 @@ class OpenAPIBuilder implements Builder {
(final b) => b.requiredParameters.add( (final b) => b.requiredParameters.add(
Parameter( Parameter(
(final b) => b (final b) => b
..name = 'rootClient' ..name = '_rootClient'
..toThis = true, ..toThis = true,
), ),
), ),
@ -315,7 +315,7 @@ class OpenAPIBuilder implements Builder {
..lambda = true ..lambda = true
..type = MethodType.getter ..type = MethodType.getter
..returns = refer('$classPrefix${_clientName(t)}') ..returns = refer('$classPrefix${_clientName(t)}')
..body = Code('$classPrefix${_clientName(t)}(${isRootClient ? 'this' : 'rootClient'})'), ..body = Code('$classPrefix${_clientName(t)}(${isRootClient ? 'this' : '_rootClient'})'),
), ),
], ],
for (final path in paths.keys) ...[ for (final path in paths.keys) ...[
@ -364,10 +364,10 @@ class OpenAPIBuilder implements Builder {
{}) {})
.join(','); .join(',');
final code = StringBuffer(''' final code = StringBuffer('''
var path = '$path'; var _path = '$path';
final queryParameters = <String, dynamic>{}; final _queryParameters = <String, dynamic>{};
final headers = <String, String>{${acceptHeader.isNotEmpty ? "'Accept': '$acceptHeader'," : ''}}; final _headers = <String, String>{${acceptHeader.isNotEmpty ? "'Accept': '$acceptHeader'," : ''}};
Uint8List? body; Uint8List? _body;
'''); ''');
final security = operation.security ?? spec.security ?? []; final security = operation.security ?? spec.security ?? [];
@ -377,8 +377,8 @@ class OpenAPIBuilder implements Builder {
for (final requirement in securityRequirements) { for (final requirement in securityRequirements) {
final securityScheme = spec.components!.securitySchemes![requirement.keys.single]!; final securityScheme = spec.components!.securitySchemes![requirement.keys.single]!;
code.write(''' code.write('''
if (${isRootClient ? '' : 'rootClient.'}authentications.where((final a) => a.type == '${securityScheme.type}' && a.scheme == '${securityScheme.scheme}').isNotEmpty) { if (${isRootClient ? 'this' : '_rootClient'}.authentications.where((final a) => a.type == '${securityScheme.type}' && a.scheme == '${securityScheme.scheme}').isNotEmpty) {
headers.addAll(${isRootClient ? '' : 'rootClient.'}authentications.singleWhere((final a) => a.type == '${securityScheme.type}' && a.scheme == '${securityScheme.scheme}').headers); _headers.addAll(${isRootClient ? 'this' : '_rootClient'}.authentications.singleWhere((final a) => a.type == '${securityScheme.type}' && a.scheme == '${securityScheme.scheme}').headers);
} }
'''); ''');
if (securityRequirements.last != requirement) { if (securityRequirements.last != requirement) {
@ -475,15 +475,15 @@ class OpenAPIBuilder implements Builder {
switch (parameter.in_) { switch (parameter.in_) {
case 'path': case 'path':
code.write( code.write(
"path = path.replaceAll('{${parameter.name}}', Uri.encodeQueryComponent($value));", "_path = _path.replaceAll('{${parameter.name}}', Uri.encodeQueryComponent($value));",
); );
case 'query': case 'query':
code.write( code.write(
"queryParameters['${parameter.name}'] = $value;", "_queryParameters['${parameter.name}'] = $value;",
); );
case 'header': case 'header':
code.write( code.write(
"headers['${parameter.name}'] = $value;", "_headers['${parameter.name}'] = $value;",
); );
default: default:
throw Exception('Can not work with parameter in "${parameter.in_}"'); throw Exception('Can not work with parameter in "${parameter.in_}"');
@ -503,7 +503,7 @@ class OpenAPIBuilder implements Builder {
for (final mimeType in operation.requestBody!.content!.keys) { for (final mimeType in operation.requestBody!.content!.keys) {
final mediaType = operation.requestBody!.content![mimeType]!; final mediaType = operation.requestBody!.content![mimeType]!;
code.write("headers['Content-Type'] = '$mimeType';"); code.write("_headers['Content-Type'] = '$mimeType';");
final dartParameterNullable = _isDartParameterNullable( final dartParameterNullable = _isDartParameterNullable(
operation.requestBody!.required, operation.requestBody!.required,
@ -541,7 +541,7 @@ class OpenAPIBuilder implements Builder {
code.write('if ($parameterName != null) {'); code.write('if ($parameterName != null) {');
} }
code.write( code.write(
'body = Uint8List.fromList(utf8.encode(${result.encode(parameterName, mimeType: mimeType)}));', '_body = Uint8List.fromList(utf8.encode(${result.encode(parameterName, mimeType: mimeType)}));',
); );
if (dartParameterNullable) { if (dartParameterNullable) {
code.write('}'); code.write('}');
@ -554,11 +554,11 @@ class OpenAPIBuilder implements Builder {
code.write( code.write(
''' '''
final response = await ${isRootClient ? '' : 'rootClient.'}doRequest( final _response = await ${isRootClient ? 'this' : '_rootClient'}.doRequest(
'$httpMethod', '$httpMethod',
Uri(path: path, queryParameters: queryParameters.isNotEmpty ? queryParameters : null).toString(), Uri(path: _path, queryParameters: _queryParameters.isNotEmpty ? _queryParameters : null).toString(),
headers, _headers,
body, _body,
); );
''', ''',
); );
@ -569,7 +569,7 @@ class OpenAPIBuilder implements Builder {
} }
for (final statusCode in operation.responses!.keys) { for (final statusCode in operation.responses!.keys) {
final response = operation.responses![statusCode]!; final response = operation.responses![statusCode]!;
code.write('if (response.statusCode == $statusCode) {'); code.write('if (_response.statusCode == $statusCode) {');
String? headersType; String? headersType;
String? headersValue; String? headersValue;
@ -592,7 +592,7 @@ class OpenAPIBuilder implements Builder {
isHeader: true, isHeader: true,
); );
headersType = result.name; headersType = result.name;
headersValue = result.deserialize('response.responseHeaders'); headersValue = result.deserialize('_response.responseHeaders');
} }
String? dataType; String? dataType;
@ -620,24 +620,24 @@ class OpenAPIBuilder implements Builder {
mimeType == 'application/octet-stream' || mimeType == 'application/octet-stream' ||
mimeType.startsWith('image/')) { mimeType.startsWith('image/')) {
dataType = 'Uint8List'; dataType = 'Uint8List';
dataValue = 'response.bodyBytes'; dataValue = '_response.bodyBytes';
dataNeedsAwait = true; dataNeedsAwait = true;
} else if (mimeType.startsWith('text/')) { } else if (mimeType.startsWith('text/')) {
dataType = 'String'; dataType = 'String';
dataValue = 'response.body'; dataValue = '_response.body';
dataNeedsAwait = true; dataNeedsAwait = true;
} else if (mimeType == 'application/json') { } else if (mimeType == 'application/json') {
dataType = result.name; dataType = result.name;
if (result.name == 'dynamic') { if (result.name == 'dynamic') {
dataValue = ''; dataValue = '';
} else if (result.name == 'String') { } else if (result.name == 'String') {
dataValue = 'response.body'; dataValue = '_response.body';
dataNeedsAwait = true; dataNeedsAwait = true;
} else if (result is TypeResultEnum || result is TypeResultBase) { } else if (result is TypeResultEnum || result is TypeResultBase) {
dataValue = result.deserialize(result.decode('await response.body')); dataValue = result.deserialize(result.decode('await _response.body'));
dataNeedsAwait = false; dataNeedsAwait = false;
} else { } else {
dataValue = result.deserialize('await response.jsonBody'); dataValue = result.deserialize('await _response.jsonBody');
dataNeedsAwait = false; dataNeedsAwait = false;
} }
} else { } else {
@ -665,7 +665,7 @@ class OpenAPIBuilder implements Builder {
code.write('}'); code.write('}');
} }
code.write( code.write(
'throw await ${classPrefix}ApiException.fromResponse(response); // coverage:ignore-line\n', 'throw await ${classPrefix}ApiException.fromResponse(_response); // coverage:ignore-line\n',
); );
} else { } else {
b.returns = refer('Future'); b.returns = refer('Future');

Loading…
Cancel
Save