Skip to content

Commit 9d9e0a4

Browse files
Make "applyToParams" a Future + add "this." when decoding JSON (#13120)
* Bump jimschubert/query-tag-action from 1 to 2 Bumps [jimschubert/query-tag-action](https://github.com/jimschubert/query-tag-action) from 1 to 2. - [Release notes](https://github.com/jimschubert/query-tag-action/releases) - [Commits](jimschubert/query-tag-action@v1...v2) Signed-off-by: dependabot[bot] <support@github.com> * Use a normal variable name inside a function, no need to make it private. * Pass on authentication names because authentication implementations may need those! * Generate Petstore samples. * Allow authentication implementations to apply header and query parameters asynchronously. * Allow inherited implementations to define a type for authentication class. * Generate Petstore sources. * Remove `authNames` from API client and authentication classes. * Reference properties with `this.` in case one of them has the same name as a local variable. * Regenerate Petstore source code. * Revert adding a generic T in `ApiClient`. Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
1 parent 7e4fa70 commit 9d9e0a4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+526
-559
lines changed

modules/openapi-generator/src/main/resources/dart2/api_client.mustache

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
{{>header}}
22
{{>part_of}}
33
class ApiClient {
4-
ApiClient({this.basePath = '{{{basePath}}}', this.authentication});
4+
ApiClient({this.basePath = '{{{basePath}}}', this.authentication,});
55

66
final String basePath;
7+
final Authentication? authentication;
78

89
var _client = Client();
10+
final _defaultHeaderMap = <String, String>{};
911

1012
/// Returns the current HTTP [Client] instance to use in this class.
1113
///
@@ -17,15 +19,12 @@ class ApiClient {
1719
_client = newClient;
1820
}
1921

20-
final _defaultHeaderMap = <String, String>{};
21-
final Authentication? authentication;
22+
Map<String, String> get defaultHeaderMap => _defaultHeaderMap;
2223

2324
void addDefaultHeader(String key, String value) {
2425
_defaultHeaderMap[key] = value;
2526
}
2627

27-
Map<String,String> get defaultHeaderMap => _defaultHeaderMap;
28-
2928
// We don't use a Map<String, String> for queryParams.
3029
// If collectionFormat is 'multi', a key might appear multiple times.
3130
Future<Response> invokeAPI(
@@ -37,7 +36,7 @@ class ApiClient {
3736
Map<String, String> formParams,
3837
String? contentType,
3938
) async {
40-
_updateParamsForAuth(queryParams, headerParams);
39+
await authentication?.applyToParams(queryParams, headerParams);
4140
4241
headerParams.addAll(_defaultHeaderMap);
4342
if (contentType != null) {
@@ -157,16 +156,6 @@ class ApiClient {
157156
@Deprecated('Scheduled for removal in OpenAPI Generator 6.x. Use serializeAsync() instead.')
158157
String serialize(Object? value) => value == null ? '' : json.encode(value);
159158
160-
/// Update query and header parameters based on authentication settings.
161-
void _updateParamsForAuth(
162-
List<QueryParam> queryParams,
163-
Map<String, String> headerParams,
164-
) {
165-
if (authentication != null) {
166-
authentication!.applyToParams(queryParams, headerParams);
167-
}
168-
}
169-
170159
{{#native_serialization}}
171160
static dynamic _deserialize(dynamic value, String targetType, {bool growable = false}) {
172161
try {

modules/openapi-generator/src/main/resources/dart2/auth/api_key_auth.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class ApiKeyAuth implements Authentication {
1010
String apiKey = '';
1111
1212
@override
13-
void applyToParams(List<QueryParam> queryParams, Map<String, String> headerParams) {
13+
Future<void> applyToParams(List<QueryParam> queryParams, Map<String, String> headerParams,) async {
1414
final paramValue = apiKeyPrefix.isEmpty ? apiKey : '$apiKeyPrefix $apiKey';
1515
1616
if (paramValue.isNotEmpty) {

modules/openapi-generator/src/main/resources/dart2/auth/authentication.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
// ignore: one_member_abstracts
44
abstract class Authentication {
55
/// Apply authentication settings to header and query params.
6-
void applyToParams(List<QueryParam> queryParams, Map<String, String> headerParams);
6+
Future<void> applyToParams(List<QueryParam> queryParams, Map<String, String> headerParams);
77
}

modules/openapi-generator/src/main/resources/dart2/auth/http_basic_auth.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class HttpBasicAuth implements Authentication {
77
String password;
88

99
@override
10-
void applyToParams(List<QueryParam> queryParams, Map<String, String> headerParams) {
10+
Future<void> applyToParams(List<QueryParam> queryParams, Map<String, String> headerParams,) async {
1111
if (username.isNotEmpty && password.isNotEmpty) {
1212
final credentials = '$username:$password';
1313
headerParams['Authorization'] = 'Basic ${base64.encode(utf8.encode(credentials))}';

modules/openapi-generator/src/main/resources/dart2/auth/http_bearer_auth.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class HttpBearerAuth implements Authentication {
1717
}
1818

1919
@override
20-
void applyToParams(List<QueryParam> queryParams, Map<String, String> headerParams) {
20+
Future<void> applyToParams(List<QueryParam> queryParams, Map<String, String> headerParams,) async {
2121
if (_accessToken == null) {
2222
return;
2323
}

modules/openapi-generator/src/main/resources/dart2/auth/oauth.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class OAuth implements Authentication {
66
String accessToken;
77

88
@override
9-
void applyToParams(List<QueryParam> queryParams, Map<String, String> headerParams) {
9+
Future<void> applyToParams(List<QueryParam> queryParams, Map<String, String> headerParams,) async {
1010
if (accessToken.isNotEmpty) {
1111
headerParams['Authorization'] = 'Bearer $accessToken';
1212
}

modules/openapi-generator/src/main/resources/dart2/serialization/native/native_class.mustache

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,59 +52,59 @@ class {{{classname}}} {
5252
String toString() => '{{{classname}}}[{{#vars}}{{{name}}}=${{{name}}}{{^-last}}, {{/-last}}{{/vars}}]';
5353

5454
Map<String, dynamic> toJson() {
55-
final _json = <String, dynamic>{};
55+
final json = <String, dynamic>{};
5656
{{#vars}}
5757
{{#isNullable}}
58-
if ({{{name}}} != null) {
58+
if (this.{{{name}}} != null) {
5959
{{/isNullable}}
6060
{{^isNullable}}
6161
{{^required}}
6262
{{^defaultValue}}
63-
if ({{{name}}} != null) {
63+
if (this.{{{name}}} != null) {
6464
{{/defaultValue}}
6565
{{/required}}
6666
{{/isNullable}}
6767
{{#isDateTime}}
6868
{{#pattern}}
69-
_json[r'{{{baseName}}}'] = _dateEpochMarker == '{{{pattern}}}'
70-
? {{{name}}}{{#isNullable}}!{{/isNullable}}{{^isNullable}}{{^required}}{{^defaultValue}}!{{/defaultValue}}{{/required}}{{/isNullable}}.millisecondsSinceEpoch
71-
: {{{name}}}{{#isNullable}}!{{/isNullable}}{{^isNullable}}{{^required}}{{^defaultValue}}!{{/defaultValue}}{{/required}}{{/isNullable}}.toUtc().toIso8601String();
69+
json[r'{{{baseName}}}'] = _dateEpochMarker == '{{{pattern}}}'
70+
? this.{{{name}}}{{#isNullable}}!{{/isNullable}}{{^isNullable}}{{^required}}{{^defaultValue}}!{{/defaultValue}}{{/required}}{{/isNullable}}.millisecondsSinceEpoch
71+
: this.{{{name}}}{{#isNullable}}!{{/isNullable}}{{^isNullable}}{{^required}}{{^defaultValue}}!{{/defaultValue}}{{/required}}{{/isNullable}}.toUtc().toIso8601String();
7272
{{/pattern}}
7373
{{^pattern}}
74-
_json[r'{{{baseName}}}'] = {{{name}}}{{#isNullable}}!{{/isNullable}}{{^isNullable}}{{^required}}{{^defaultValue}}!{{/defaultValue}}{{/required}}{{/isNullable}}.toUtc().toIso8601String();
74+
json[r'{{{baseName}}}'] = this.{{{name}}}{{#isNullable}}!{{/isNullable}}{{^isNullable}}{{^required}}{{^defaultValue}}!{{/defaultValue}}{{/required}}{{/isNullable}}.toUtc().toIso8601String();
7575
{{/pattern}}
7676
{{/isDateTime}}
7777
{{#isDate}}
7878
{{#pattern}}
79-
_json[r'{{{baseName}}}'] = _dateEpochMarker == '{{{pattern}}}'
80-
? {{{name}}}{{#isNullable}}!{{/isNullable}}{{^isNullable}}{{^required}}{{^defaultValue}}!{{/defaultValue}}{{/required}}{{/isNullable}}.millisecondsSinceEpoch
81-
: _dateFormatter.format({{{name}}}{{#isNullable}}!{{/isNullable}}{{^isNullable}}{{^required}}{{^defaultValue}}!{{/defaultValue}}{{/required}}{{/isNullable}}.toUtc());
79+
json[r'{{{baseName}}}'] = _dateEpochMarker == '{{{pattern}}}'
80+
? this.{{{name}}}{{#isNullable}}!{{/isNullable}}{{^isNullable}}{{^required}}{{^defaultValue}}!{{/defaultValue}}{{/required}}{{/isNullable}}.millisecondsSinceEpoch
81+
: _dateFormatter.format(this.{{{name}}}{{#isNullable}}!{{/isNullable}}{{^isNullable}}{{^required}}{{^defaultValue}}!{{/defaultValue}}{{/required}}{{/isNullable}}.toUtc());
8282
{{/pattern}}
8383
{{^pattern}}
84-
_json[r'{{{baseName}}}'] = _dateFormatter.format({{{name}}}{{#isNullable}}!{{/isNullable}}{{^isNullable}}{{^required}}{{^defaultValue}}!{{/defaultValue}}{{/required}}{{/isNullable}}.toUtc());
84+
json[r'{{{baseName}}}'] = _dateFormatter.format(this.{{{name}}}{{#isNullable}}!{{/isNullable}}{{^isNullable}}{{^required}}{{^defaultValue}}!{{/defaultValue}}{{/required}}{{/isNullable}}.toUtc());
8585
{{/pattern}}
8686
{{/isDate}}
8787
{{^isDateTime}}
8888
{{^isDate}}
89-
_json[r'{{{baseName}}}'] = {{{name}}};
89+
json[r'{{{baseName}}}'] = this.{{{name}}};
9090
{{/isDate}}
9191
{{/isDateTime}}
9292
{{#isNullable}}
9393
} else {
94-
_json[r'{{{baseName}}}'] = null;
94+
json[r'{{{baseName}}}'] = null;
9595
}
9696
{{/isNullable}}
9797
{{^isNullable}}
9898
{{^required}}
9999
{{^defaultValue}}
100100
} else {
101-
_json[r'{{{baseName}}}'] = null;
101+
json[r'{{{baseName}}}'] = null;
102102
}
103103
{{/defaultValue}}
104104
{{/required}}
105105
{{/isNullable}}
106106
{{/vars}}
107-
return _json;
107+
return json;
108108
}
109109

110110
/// Returns a new [{{{classname}}}] instance and imports its values from

samples/openapi3/client/petstore/dart2/petstore_client_lib/lib/api_client.dart

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
part of openapi.api;
1212

1313
class ApiClient {
14-
ApiClient({this.basePath = 'http://petstore.swagger.io/v2', this.authentication});
14+
ApiClient({this.basePath = 'http://petstore.swagger.io/v2', this.authentication,});
1515

1616
final String basePath;
17+
final Authentication? authentication;
1718

1819
var _client = Client();
20+
final _defaultHeaderMap = <String, String>{};
1921

2022
/// Returns the current HTTP [Client] instance to use in this class.
2123
///
@@ -27,15 +29,12 @@ class ApiClient {
2729
_client = newClient;
2830
}
2931

30-
final _defaultHeaderMap = <String, String>{};
31-
final Authentication? authentication;
32+
Map<String, String> get defaultHeaderMap => _defaultHeaderMap;
3233

3334
void addDefaultHeader(String key, String value) {
3435
_defaultHeaderMap[key] = value;
3536
}
3637

37-
Map<String,String> get defaultHeaderMap => _defaultHeaderMap;
38-
3938
// We don't use a Map<String, String> for queryParams.
4039
// If collectionFormat is 'multi', a key might appear multiple times.
4140
Future<Response> invokeAPI(
@@ -47,7 +46,7 @@ class ApiClient {
4746
Map<String, String> formParams,
4847
String? contentType,
4948
) async {
50-
_updateParamsForAuth(queryParams, headerParams);
49+
await authentication?.applyToParams(queryParams, headerParams);
5150

5251
headerParams.addAll(_defaultHeaderMap);
5352
if (contentType != null) {
@@ -165,16 +164,6 @@ class ApiClient {
165164
@Deprecated('Scheduled for removal in OpenAPI Generator 6.x. Use serializeAsync() instead.')
166165
String serialize(Object? value) => value == null ? '' : json.encode(value);
167166

168-
/// Update query and header parameters based on authentication settings.
169-
void _updateParamsForAuth(
170-
List<QueryParam> queryParams,
171-
Map<String, String> headerParams,
172-
) {
173-
if (authentication != null) {
174-
authentication!.applyToParams(queryParams, headerParams);
175-
}
176-
}
177-
178167
static dynamic _deserialize(dynamic value, String targetType, {bool growable = false}) {
179168
try {
180169
switch (targetType) {

samples/openapi3/client/petstore/dart2/petstore_client_lib/lib/auth/api_key_auth.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class ApiKeyAuth implements Authentication {
2020
String apiKey = '';
2121

2222
@override
23-
void applyToParams(List<QueryParam> queryParams, Map<String, String> headerParams) {
23+
Future<void> applyToParams(List<QueryParam> queryParams, Map<String, String> headerParams,) async {
2424
final paramValue = apiKeyPrefix.isEmpty ? apiKey : '$apiKeyPrefix $apiKey';
2525

2626
if (paramValue.isNotEmpty) {

samples/openapi3/client/petstore/dart2/petstore_client_lib/lib/auth/authentication.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ part of openapi.api;
1313
// ignore: one_member_abstracts
1414
abstract class Authentication {
1515
/// Apply authentication settings to header and query params.
16-
void applyToParams(List<QueryParam> queryParams, Map<String, String> headerParams);
16+
Future<void> applyToParams(List<QueryParam> queryParams, Map<String, String> headerParams);
1717
}

0 commit comments

Comments
 (0)