Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dart2 template linting #10263

Merged
merged 57 commits into from
Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from 49 commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
4347316
Bump jimschubert/query-tag-action from 1 to 2
dependabot[bot] May 12, 2021
f063ff0
Merge pull request #8 from noordawod/dependabot/github_actions/jimsch…
noordawod Aug 2, 2021
ff87fe5
Merge branch 'OpenAPITools:master' into master
noordawod Aug 2, 2021
2629f88
Merge remote-tracking branch 'upstream/master'
noordawod Aug 2, 2021
9d79c48
Merge branch 'master' of noordawod.github.com:noordawod/openapi-gener…
noordawod Aug 2, 2021
5f5ce99
Merge remote-tracking branch 'upstream/master'
noordawod Aug 3, 2021
98198c2
Merge remote-tracking branch 'upstream/master'
noordawod Aug 25, 2021
93da7c0
Updated Dart2 template to fix linting errors when using recommended r…
noordawod Aug 25, 2021
fb5d2c4
Updated Petstore code.
noordawod Aug 25, 2021
a0b747a
Revert to having no EOF marker at end of few files.
noordawod Aug 25, 2021
4055634
Fix a call on null value.
noordawod Aug 25, 2021
5a53064
Updated Petstore code.
noordawod Aug 25, 2021
6f64de5
Add more checks for nulls.
noordawod Aug 25, 2021
ee54b38
Updated Petstore code.
noordawod Aug 25, 2021
bcb8af1
Updated Petstore code.
noordawod Aug 25, 2021
0084652
Revert back decoding an enum value.
noordawod Aug 25, 2021
1af1860
Revert back decoding an enum value.
noordawod Aug 25, 2021
b8c28f1
Updated Petstore code.
noordawod Aug 25, 2021
013843b
Revert to older version of _convertParametersForCollectionFormat method.
noordawod Aug 25, 2021
fe20341
Updated Petstore code.
noordawod Aug 25, 2021
cf73474
Merge remote-tracking branch 'upstream/master'
noordawod Aug 26, 2021
668c692
Merge branch 'master' into refactor/dart2-template-linting
noordawod Aug 26, 2021
f572c36
Updated template and Petstore files.
noordawod Aug 26, 2021
292cd9f
Merge remote-tracking branch 'upstream/master'
noordawod Aug 27, 2021
a1406d4
Merge branch 'master' into refactor/dart2-template-linting
noordawod Aug 27, 2021
aa6bd92
Adjust doc for analysis_options file.
noordawod Aug 27, 2021
3fbde61
Updated Petstore sources.
noordawod Aug 27, 2021
96dfba4
Shorten command to replace path parameters.
noordawod Aug 27, 2021
8382502
Updated Petstore code.
noordawod Aug 27, 2021
0903f7c
Add a space for readability.
noordawod Aug 27, 2021
905a232
Merge remote-tracking branch 'upstream/master'
noordawod Aug 28, 2021
bd5853b
Merge branch 'master' into refactor/dart2-template-linting
noordawod Aug 28, 2021
3ceea62
Merge remote-tracking branch 'upstream/master'
noordawod Aug 30, 2021
a2d96d6
Merge branch 'master' into refactor/dart2-template-linting
noordawod Aug 30, 2021
d842e19
Merge remote-tracking branch 'upstream/master'
noordawod Aug 31, 2021
06669eb
Merge branch 'master' into refactor/dart2-template-linting
noordawod Aug 31, 2021
877fef2
Adjust template per feedback.
noordawod Aug 31, 2021
7e48798
Remove unneeded break statements.
noordawod Aug 31, 2021
ec86071
Updated Petstore code.
noordawod Aug 31, 2021
99d632d
Keep a new line character when replacing parameter names (like before).
noordawod Aug 31, 2021
8a923ea
Remove string interpolation.
noordawod Aug 31, 2021
1337835
Updated Petstore code.
noordawod Aug 31, 2021
d1f6825
Merge remote-tracking branch 'upstream/master'
noordawod Aug 31, 2021
2353a6f
Merge branch 'master' into refactor/dart2-template-linting
noordawod Aug 31, 2021
0057d35
Remove commented code.
noordawod Aug 31, 2021
1a0464b
Regenerate Petstore code.
noordawod Aug 31, 2021
f53a4c3
Merge remote-tracking branch 'upstream/master'
noordawod Sep 3, 2021
cb663ee
Merge branch 'master' into refactor/dart2-template-linting
noordawod Sep 3, 2021
b059270
Regenerated Petstore code.
noordawod Sep 3, 2021
83708a1
Use another parameter name to stop variable shadowing.
noordawod Sep 4, 2021
0d3b281
Regenerated Petstore code.
noordawod Sep 4, 2021
8270e9a
Put description adjacent to the class.
noordawod Sep 4, 2021
e558cda
Add another global ignore for all files: constant_identifier_names
noordawod Sep 4, 2021
c1b16d1
Use const for contentTypes and authNames.
noordawod Sep 4, 2021
ba239df
Regenerate Petstore code.
noordawod Sep 4, 2021
f1ac801
Use shorter code to check for an empty array.
noordawod Sep 4, 2021
da1501e
Regenerate Petstore code.
noordawod Sep 4, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
analyzer:
strong-mode:
implicit-casts: true
12 changes: 7 additions & 5 deletions modules/openapi-generator/src/main/resources/dart2/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class {{{classname}}} {
///
{{/-last}}
{{/allParams}}
Future<Response> {{{nickname}}}WithHttpInfo({{#allParams}}{{#required}}{{{dataType}}} {{{paramName}}}{{^-last}}, {{/-last}}{{/required}}{{/allParams}}{{#hasOptionalParams}}{ {{#allParams}}{{^required}}{{{dataType}}} {{{paramName}}}{{^-last}}, {{/-last}}{{/required}}{{/allParams}} }{{/hasOptionalParams}}) async {
Future<Response> {{{nickname}}}WithHttpInfo({{#allParams}}{{#required}}{{{dataType}}} {{{paramName}}},{{^-last}} {{/-last}}{{/required}}{{/allParams}}{{#hasOptionalParams}}{ {{#allParams}}{{^required}}{{{dataType}}} {{{paramName}}},{{^-last}} {{/-last}}{{/required}}{{/allParams}} }{{/hasOptionalParams}}) async {
noordawod marked this conversation as resolved.
Show resolved Hide resolved
{{#hasParams}}
// Verify required params are set.
{{#allParams}}
Expand All @@ -61,9 +61,11 @@ class {{{classname}}} {
{{/allParams}}

{{/hasParams}}
// ignore: prefer_const_declarations
noordawod marked this conversation as resolved.
Show resolved Hide resolved
final path = r'{{{path}}}'{{#pathParams}}
.replaceAll('{' + '{{{baseName}}}' + '}', {{{paramName}}}.toString()){{/pathParams}};
.replaceAll({{=<% %>=}}'{<% baseName %>}'<%={{ }}=%>, {{{paramName}}}{{^isString}}.toString(){{/isString}}){{/pathParams}};

// ignore: prefer_final_locals
Object postBody{{#bodyParam}} = {{{paramName}}}{{/bodyParam}};

final queryParams = <QueryParam>[];
Expand Down Expand Up @@ -131,7 +133,7 @@ class {{{classname}}} {
{{/formParams}}
{{/isMultipart}}

return await apiClient.invokeAPI(
return apiClient.invokeAPI(
path,
'{{{httpMethod}}}',
queryParams,
Expand Down Expand Up @@ -174,7 +176,7 @@ class {{{classname}}} {
{{/-last}}
{{/allParams}}
Future<{{{returnType}}}{{^returnType}}void{{/returnType}}> {{{nickname}}}({{#allParams}}{{#required}}{{{dataType}}} {{{paramName}}}{{^-last}}, {{/-last}}{{/required}}{{/allParams}}{{#hasOptionalParams}}{ {{#allParams}}{{^required}}{{{dataType}}} {{{paramName}}}{{^-last}}, {{/-last}}{{/required}}{{/allParams}} }{{/hasOptionalParams}}) async {
final response = await {{{nickname}}}WithHttpInfo({{#allParams}}{{#required}}{{{paramName}}}{{^-last}}, {{/-last}}{{/required}}{{/allParams}}{{#hasOptionalParams}} {{#allParams}}{{^required}}{{{paramName}}}: {{{paramName}}}{{^-last}}, {{/-last}}{{/required}}{{/allParams}} {{/hasOptionalParams}});
final response = await {{{nickname}}}WithHttpInfo({{#allParams}}{{#required}}{{{paramName}}},{{^-last}} {{/-last}}{{/required}}{{/allParams}}{{#hasOptionalParams}} {{#allParams}}{{^required}}{{{paramName}}}: {{{paramName}}},{{^-last}} {{/-last}}{{/required}}{{/allParams}} {{/hasOptionalParams}});
if (response.statusCode >= HttpStatus.badRequest) {
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
}
Expand Down Expand Up @@ -223,7 +225,7 @@ class {{{classname}}} {
{{/isArray}}
{{/json_serializable}}
}
return Future<{{{returnType}}}>.value(null);
return Future<{{{returnType}}}>.value();
{{/returnType}}
}
{{/operation}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class ApiClient {
? '?${urlEncodedQueryParams.join('&')}'
: '';

final Uri uri = Uri.parse('$basePath$path$queryString');
final uri = Uri.parse('$basePath$path$queryString');

if (nullableContentType != null) {
headerParams['Content-Type'] = nullableContentType;
Expand All @@ -102,7 +102,8 @@ class ApiClient {
body.finalize().listen(
request.sink.add,
onDone: request.sink.close,
onError: (error, trace) => request.sink.close(),
// ignore: avoid_types_on_closure_parameters
onError: (Object error, StackTrace trace) => request.sink.close(),
cancelOnError: true,
);
final response = await _client.send(request);
Expand Down Expand Up @@ -191,18 +192,17 @@ class ApiClient {
try {
switch (targetType) {
case 'String':
noordawod marked this conversation as resolved.
Show resolved Hide resolved
return '$value';
return value is String ? value : value.toString();
case 'int':
return value is int ? value : int.parse('$value');
case 'double':
return value is double ? value : double.parse('$value');
case 'bool':
if (value is bool) {
return value;
}
final valueString = '$value'.toLowerCase();
return valueString == 'true' || valueString == '1';
break;
case 'double':
return value is double ? value : double.parse('$value');
{{#models}}
{{#model}}
case '{{{classname}}}':
Expand All @@ -220,25 +220,24 @@ class ApiClient {
if (value is List && (match = _regList.firstMatch(targetType)) != null) {
targetType = match[1]; // ignore: parameter_assignments
return value
.map((v) => _deserialize(v, targetType, growable: growable))
.map<dynamic>((dynamic value) => _deserialize(value, targetType, growable: growable))
noordawod marked this conversation as resolved.
Show resolved Hide resolved
.toList(growable: growable);
}
if (value is Set && (match = _regSet.firstMatch(targetType)) != null) {
targetType = match[1]; // ignore: parameter_assignments
return value
.map((v) => _deserialize(v, targetType, growable: growable))
.map<dynamic>((dynamic value) => _deserialize(value, targetType, growable: growable))
.toSet();
}
if (value is Map && (match = _regMap.firstMatch(targetType)) != null) {
targetType = match[1]; // ignore: parameter_assignments
return Map.fromIterables(
value.keys,
value.values.map((v) => _deserialize(v, targetType, growable: growable)),
return Map<String, dynamic>.fromIterables(
value.keys.cast<String>(),
value.values.map<dynamic>((dynamic value) => _deserialize(value, targetType, growable: growable)),
);
}
break;
}
} catch (error, trace) {
} on Exception catch (error, trace) {
throw ApiException.withInner(HttpStatus.internalServerError, 'Exception during deserialization.', error, trace,);
}
throw ApiException(HttpStatus.internalServerError, 'Could not find a suitable class for deserialization',);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,21 @@ Iterable<QueryParam> _convertParametersForCollectionFormat(
// preconditions
if (name != null && name.isNotEmpty && value != null) {
if (value is List) {
// get the collection format, default: csv
collectionFormat = (collectionFormat == null || collectionFormat.isEmpty)
? 'csv'
: collectionFormat;

if (collectionFormat == 'multi') {
return value.map((v) => QueryParam(name, parameterToString(v)));
return value.map((dynamic value) => QueryParam(name, parameterToString(value)),);
noordawod marked this conversation as resolved.
Show resolved Hide resolved
}

// Default collection format is 'csv'.
if (collectionFormat == null || collectionFormat.isEmpty) {
// ignore: parameter_assignments
collectionFormat = 'csv';
}

final delimiter = _delimiters[collectionFormat] ?? ',';

params.add(QueryParam(name, value.map((v) => parameterToString(v)).join(delimiter)));
params.add(QueryParam(name, value.map<dynamic>(parameterToString).join(delimiter)),);
} else {
params.add(QueryParam(name, parameterToString(value)));
params.add(QueryParam(name, parameterToString(value),));
}
}

Expand Down Expand Up @@ -69,3 +70,36 @@ Future<String> _decodeBodyBytes(Response response) async {
? response.bodyBytes == null ? null : utf8.decode(response.bodyBytes)
: response.body;
}

/// Returns a valid [T] value found at the specified Map [key], null otherwise.
T mapValueOfType<T>(dynamic map, String key) {
final dynamic value = map is Map ? map[key] : null;
return value is T ? value : null;
}

/// Returns a valid Map<K, V> found at the specified Map [key], null otherwise.
Map<K, V> mapCastOfType<K, V>(dynamic map, String key) {
final dynamic value = map is Map ? map[key] : null;
return value is Map ? value.cast<K, V>() : null;
}

/// Returns a valid [DateTime] found at the specified Map [key], null otherwise.
DateTime mapDateTime(dynamic map, String key, [String pattern]) {
final dynamic value = map is Map ? map[key] : null;
if (value != null) {
int millis;
if (value is int) {
millis = value;
} else if (value is String) {
if (pattern == _dateEpochMarker) {
millis = int.tryParse(value);
} else {
return DateTime.tryParse(value);
}
}
if (millis != null) {
return DateTime.fromMillisecondsSinceEpoch(millis, isUtc: true);
}
}
return null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ class ApiKeyAuth implements Authentication {
} else if (location == 'header' && value != null) {
headerParams[paramName] = value;
} else if (location == 'cookie' && value != null) {
headerParams.update('Cookie', (String existingCookie) {
return '$existingCookie; $paramName=$value';
}, ifAbsent: () => '$paramName=$value');
headerParams.update(
'Cookie',
(existingCookie) => '$existingCookie; $paramName=$value',
ifAbsent: () => '$paramName=$value',
);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{{>header}}
{{>part_of}}
// ignore: one_member_abstracts
abstract class Authentication {
/// Apply authentication settings to header and query params.
void applyToParams(List<QueryParam> queryParams, Map<String, String> headerParams);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class HttpBasicAuth implements Authentication {

@override
void applyToParams(List<QueryParam> queryParams, Map<String, String> headerParams) {
final credentials = (username ?? '') + ':' + (password ?? '');
final credentials = '${username ?? ''}:${password ?? ''}';
headerParams['Authorization'] = 'Basic ${base64.encode(utf8.encode(credentials))}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class HttpBearerAuth implements Authentication {
if (accessToken is! String && accessToken is! HttpBearerAuthProvider) {
throw ArgumentError('Type of Bearer accessToken should be a String or a String Function().');
}
this._accessToken = accessToken;
_accessToken = accessToken;
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,7 @@ class {{{classname}}} {
Map<String, dynamic> toJson() => _${{{classname}}}ToJson(this);

@override
String toString() {
return toJson().toString();
}

String toString() => toJson().toString();
}
{{#vars}}
{{#isEnum}}
Expand Down
Loading