Skip to content

Commit 800293c

Browse files
gasugesuwing328
andauthored
[Dart] Fix "basic" auth method and Add Bearer token support (#5743)
* added auth check and lint * fixed basic auth condition * Added bearer auth * updated samples * update dart petstore samples Co-authored-by: William Cheng <[email protected]>
1 parent 64c9950 commit 800293c

File tree

16 files changed

+249
-38
lines changed

16 files changed

+249
-38
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/DartClientCodegen.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ public void processOpts() {
280280
final String authFolder = sourceFolder + File.separator + "lib" + File.separator + "auth";
281281
supportingFiles.add(new SupportingFile("auth/authentication.mustache", authFolder, "authentication.dart"));
282282
supportingFiles.add(new SupportingFile("auth/http_basic_auth.mustache", authFolder, "http_basic_auth.dart"));
283+
supportingFiles.add(new SupportingFile("auth/http_bearer_auth.mustache", authFolder, "http_bearer_auth.dart"));
283284
supportingFiles.add(new SupportingFile("auth/api_key_auth.mustache", authFolder, "api_key_auth.dart"));
284285
supportingFiles.add(new SupportingFile("auth/oauth.mustache", authFolder, "oauth.dart"));
285286
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,19 @@ import 'package:{{pubName}}/api.dart';
5353
{{#hasAuthMethods}}
5454
{{#authMethods}}
5555
{{#isBasic}}
56+
{{#isBasicBasic}}
5657
// TODO Configure HTTP basic authorization: {{{name}}}
5758
//defaultApiClient.getAuthentication<HttpBasicAuth>('{{{name}}}').username = 'YOUR_USERNAME'
5859
//defaultApiClient.getAuthentication<HttpBasicAuth>('{{{name}}}').password = 'YOUR_PASSWORD';
60+
{{/isBasicBasic}}
61+
{{#isBasicBearer}}
62+
// TODO Configure HTTP Bearer authorization: {{{name}}}
63+
// Case 1. Use String Token
64+
//defaultApiClient.getAuthentication<HttpBearerAuth>('{{{name}}}').setAccessToken('YOUR_ACCESS_TOKEN');
65+
// Case 2. Use Function which generate token.
66+
// String yourTokenGeneratorFunction() { ... }
67+
//defaultApiClient.getAuthentication<HttpBearerAuth>('{{{name}}}').setAccessToken(yourTokenGeneratorFunction);
68+
{{/isBasicBearer}}
5969
{{/isBasic}}
6070
{{#isApiKey}}
6171
// TODO Configure API key authorization: {{{name}}}
@@ -110,7 +120,13 @@ Class | Method | HTTP request | Description
110120
- **API key parameter name**: {{{keyParamName}}}
111121
- **Location**: {{#isKeyInQuery}}URL query string{{/isKeyInQuery}}{{#isKeyInHeader}}HTTP header{{/isKeyInHeader}}
112122
{{/isApiKey}}
113-
{{#isBasic}}- **Type**: HTTP basic authentication
123+
{{#isBasic}}
124+
{{#isBasicBasic}}
125+
- **Type**: HTTP basicc authentication
126+
{{/isBasicBasic}}
127+
{{#isBasicBearer}}
128+
- **Type**: HTTP Bearer authentication
129+
{{/isBasicBearer}}
114130
{{/isBasic}}
115131
{{#isOAuth}}- **Type**: OAuth
116132
- **Flow**: {{{flow}}}

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,25 @@ class ApiClient {
1919
final _regMap = RegExp(r'^Map<String,(.*)>$');
2020

2121
ApiClient({this.basePath = "{{{basePath}}}"}) {
22-
// Setup authentications (key: authentication name, value: authentication).{{#authMethods}}{{#isBasic}}
23-
_authentications['{{name}}'] = HttpBasicAuth();{{/isBasic}}{{#isApiKey}}
24-
_authentications['{{name}}'] = ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{keyParamName}}");{{/isApiKey}}{{#isOAuth}}
25-
_authentications['{{name}}'] = OAuth();{{/isOAuth}}{{/authMethods}}
22+
{{#hasAuthMethods}}
23+
// Setup authentications (key: authentication name, value: authentication).
24+
{{#authMethods}}
25+
{{#isBasic}}
26+
{{#isBasicBasic}}
27+
_authentications['{{name}}'] = HttpBasicAuth();
28+
{{/isBasicBasic}}
29+
{{#isBasicBearer}}
30+
_authentications['{{name}}'] = HttpBearerAuth();
31+
{{/isBasicBearer}}
32+
{{/isBasic}}
33+
{{#isApiKey}}
34+
_authentications['{{name}}'] = ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{keyParamName}}");
35+
{{/isApiKey}}
36+
{{#isOAuth}}
37+
_authentications['{{name}}'] = OAuth();
38+
{{/isOAuth}}
39+
{{/authMethods}}
40+
{{/hasAuthMethods}}
2641
}
2742

2843
void addDefaultHeader(String key, String value) {

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,19 @@ import 'package:{{pubName}}/api.dart';
2828
{{#hasAuthMethods}}
2929
{{#authMethods}}
3030
{{#isBasic}}
31+
{{#isBasicBasic}}
3132
// TODO Configure HTTP basic authorization: {{{name}}}
3233
//defaultApiClient.getAuthentication<HttpBasicAuth>('{{{name}}}').username = 'YOUR_USERNAME'
3334
//defaultApiClient.getAuthentication<HttpBasicAuth>('{{{name}}}').password = 'YOUR_PASSWORD';
35+
{{/isBasicBasic}}
36+
{{#isBasicBearer}}
37+
// TODO Configure HTTP Bearer authorization: {{{name}}}
38+
// Case 1. Use String Token
39+
//defaultApiClient.getAuthentication<HttpBearerAuth>('{{{name}}}').setAccessToken('YOUR_ACCESS_TOKEN');
40+
// Case 2. Use Function which generate token.
41+
// String yourTokenGeneratorFunction() { ... }
42+
//defaultApiClient.getAuthentication<HttpBearerAuth>('{{{name}}}').setAccessToken(yourTokenGeneratorFunction);
43+
{{/isBasicBearer}}
3444
{{/isBasic}}
3545
{{#isApiKey}}
3646
// TODO Configure API key authorization: {{{name}}}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ part 'auth/authentication.dart';
1111
part 'auth/api_key_auth.dart';
1212
part 'auth/oauth.dart';
1313
part 'auth/http_basic_auth.dart';
14+
part 'auth/http_bearer_auth.dart';
1415

1516
{{#apiInfo}}{{#apis}}part 'api/{{classFilename}}.dart';
1617
{{/apis}}{{/apiInfo}}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
part of {{pubName}}.api;
2+
3+
class HttpBearerAuth implements Authentication {
4+
dynamic _accessToken;
5+
6+
HttpBearerAuth() { }
7+
8+
@override
9+
void applyToParams(List<QueryParam> queryParams, Map<String, String> headerParams) {
10+
if (_accessToken is String) {
11+
headerParams["Authorization"] = "Bearer " + _accessToken;
12+
} else if (_accessToken is String Function()){
13+
headerParams["Authorization"] = "Bearer " + _accessToken();
14+
} else {
15+
throw ArgumentError('Type of Bearer accessToken should be String or String Function().');
16+
}
17+
}
18+
19+
void setAccessToken(dynamic accessToken) {
20+
if (!((accessToken is String) | (accessToken is String Function()))){
21+
throw ArgumentError('Type of Bearer accessToken should be String or String Function().');
22+
}
23+
this._accessToken = accessToken;
24+
}
25+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4.3.0-SNAPSHOT
1+
4.3.1-SNAPSHOT
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# openapi.model.InlineObject
2+
3+
## Load the model package
4+
```dart
5+
import 'package:openapi/api.dart';
6+
```
7+
8+
## Properties
9+
Name | Type | Description | Notes
10+
------------ | ------------- | ------------- | -------------
11+
**name** | **String** | Updated name of the pet | [optional] [default to null]
12+
**status** | **String** | Updated status of the pet | [optional] [default to null]
13+
14+
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
15+
16+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# openapi.model.InlineObject1
2+
3+
## Load the model package
4+
```dart
5+
import 'package:openapi/api.dart';
6+
```
7+
8+
## Properties
9+
Name | Type | Description | Notes
10+
------------ | ------------- | ------------- | -------------
11+
**additionalMetadata** | **String** | Additional data to pass to server | [optional] [default to null]
12+
**file** | [**MultipartFile**](File.md) | file to upload | [optional] [default to null]
13+
14+
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
15+
16+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
part of openapi.api;
2+
3+
class HttpBearerAuth implements Authentication {
4+
dynamic _accessToken;
5+
6+
HttpBearerAuth() { }
7+
8+
@override
9+
void applyToParams(List<QueryParam> queryParams, Map<String, String> headerParams) {
10+
if (_accessToken is String) {
11+
headerParams["Authorization"] = "Bearer " + _accessToken;
12+
} else if (_accessToken is String Function()){
13+
headerParams["Authorization"] = "Bearer " + _accessToken();
14+
} else {
15+
throw ArgumentError('Type of Bearer accessToken should be String or String Function().');
16+
}
17+
}
18+
19+
void setAccessToken(dynamic accessToken) {
20+
if (!((accessToken is String) | (accessToken is String Function()))){
21+
throw ArgumentError('Type of Bearer accessToken should be String or String Function().');
22+
}
23+
this._accessToken = accessToken;
24+
}
25+
}

samples/client/petstore/dart/flutter_petstore/openapi/lib/model/inline_object.dart

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ part of openapi.api;
22

33
class InlineObject {
44
/* Updated name of the pet */
5-
String name = null;
5+
String name = null;
66
/* Updated status of the pet */
7-
String status = null;
7+
String status = null;
88
InlineObject();
99

1010
@override
@@ -14,27 +14,33 @@ class InlineObject {
1414

1515
InlineObject.fromJson(Map<String, dynamic> json) {
1616
if (json == null) return;
17-
name = json['name'];
18-
status = json['status'];
17+
if (json['name'] == null) {
18+
name = null;
19+
} else {
20+
name = json['name'];
21+
}
22+
if (json['status'] == null) {
23+
status = null;
24+
} else {
25+
status = json['status'];
26+
}
1927
}
2028

2129
Map<String, dynamic> toJson() {
22-
Map <String, dynamic> json = {};
23-
if (name != null)
24-
json['name'] = name;
25-
if (status != null)
26-
json['status'] = status;
27-
return json;
30+
return {
31+
'name': name,
32+
'status': status
33+
};
2834
}
2935

3036
static List<InlineObject> listFromJson(List<dynamic> json) {
31-
return json == null ? List<InlineObject>() : json.map((value) => InlineObject.fromJson(value)).toList();
37+
return json == null ? new List<InlineObject>() : json.map((value) => new InlineObject.fromJson(value)).toList();
3238
}
3339

34-
static Map<String, InlineObject> mapFromJson(Map<String, dynamic> json) {
35-
var map = Map<String, InlineObject>();
36-
if (json != null && json.isNotEmpty) {
37-
json.forEach((String key, dynamic value) => map[key] = InlineObject.fromJson(value));
40+
static Map<String, InlineObject> mapFromJson(Map<String, Map<String, dynamic>> json) {
41+
var map = new Map<String, InlineObject>();
42+
if (json != null && json.length > 0) {
43+
json.forEach((String key, Map<String, dynamic> value) => map[key] = new InlineObject.fromJson(value));
3844
}
3945
return map;
4046
}

samples/client/petstore/dart/flutter_petstore/openapi/lib/model/inline_object1.dart

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ part of openapi.api;
22

33
class InlineObject1 {
44
/* Additional data to pass to server */
5-
String additionalMetadata = null;
5+
String additionalMetadata = null;
66
/* file to upload */
7-
MultipartFile file = null;
7+
MultipartFile file = null;
88
InlineObject1();
99

1010
@override
@@ -14,29 +14,33 @@ class InlineObject1 {
1414

1515
InlineObject1.fromJson(Map<String, dynamic> json) {
1616
if (json == null) return;
17-
additionalMetadata = json['additionalMetadata'];
18-
file = (json['file'] == null) ?
19-
null :
20-
File.fromJson(json['file']);
17+
if (json['additionalMetadata'] == null) {
18+
additionalMetadata = null;
19+
} else {
20+
additionalMetadata = json['additionalMetadata'];
21+
}
22+
if (json['file'] == null) {
23+
file = null;
24+
} else {
25+
file = new File.fromJson(json['file']);
26+
}
2127
}
2228

2329
Map<String, dynamic> toJson() {
24-
Map <String, dynamic> json = {};
25-
if (additionalMetadata != null)
26-
json['additionalMetadata'] = additionalMetadata;
27-
if (file != null)
28-
json['file'] = file;
29-
return json;
30+
return {
31+
'additionalMetadata': additionalMetadata,
32+
'file': file
33+
};
3034
}
3135

3236
static List<InlineObject1> listFromJson(List<dynamic> json) {
33-
return json == null ? List<InlineObject1>() : json.map((value) => InlineObject1.fromJson(value)).toList();
37+
return json == null ? new List<InlineObject1>() : json.map((value) => new InlineObject1.fromJson(value)).toList();
3438
}
3539

36-
static Map<String, InlineObject1> mapFromJson(Map<String, dynamic> json) {
37-
var map = Map<String, InlineObject1>();
38-
if (json != null && json.isNotEmpty) {
39-
json.forEach((String key, dynamic value) => map[key] = InlineObject1.fromJson(value));
40+
static Map<String, InlineObject1> mapFromJson(Map<String, Map<String, dynamic>> json) {
41+
var map = new Map<String, InlineObject1>();
42+
if (json != null && json.length > 0) {
43+
json.forEach((String key, Map<String, dynamic> value) => map[key] = new InlineObject1.fromJson(value));
4044
}
4145
return map;
4246
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
part of openapi.api;
2+
3+
class HttpBearerAuth implements Authentication {
4+
dynamic _accessToken;
5+
6+
HttpBearerAuth() { }
7+
8+
@override
9+
void applyToParams(List<QueryParam> queryParams, Map<String, String> headerParams) {
10+
if (_accessToken is String) {
11+
headerParams["Authorization"] = "Bearer " + _accessToken;
12+
} else if (_accessToken is String Function()){
13+
headerParams["Authorization"] = "Bearer " + _accessToken();
14+
} else {
15+
throw ArgumentError('Type of Bearer accessToken should be String or String Function().');
16+
}
17+
}
18+
19+
void setAccessToken(dynamic accessToken) {
20+
if (!((accessToken is String) | (accessToken is String Function()))){
21+
throw ArgumentError('Type of Bearer accessToken should be String or String Function().');
22+
}
23+
this._accessToken = accessToken;
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
part of openapi.api;
2+
3+
class HttpBearerAuth implements Authentication {
4+
dynamic _accessToken;
5+
6+
HttpBearerAuth() { }
7+
8+
@override
9+
void applyToParams(List<QueryParam> queryParams, Map<String, String> headerParams) {
10+
if (_accessToken is String) {
11+
headerParams["Authorization"] = "Bearer " + _accessToken;
12+
} else if (_accessToken is String Function()){
13+
headerParams["Authorization"] = "Bearer " + _accessToken();
14+
} else {
15+
throw ArgumentError('Type of Bearer accessToken should be String or String Function().');
16+
}
17+
}
18+
19+
void setAccessToken(dynamic accessToken) {
20+
if (!((accessToken is String) | (accessToken is String Function()))){
21+
throw ArgumentError('Type of Bearer accessToken should be String or String Function().');
22+
}
23+
this._accessToken = accessToken;
24+
}
25+
}

samples/client/petstore/dart2/petstore_client_lib/lib/api.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ part 'auth/authentication.dart';
1111
part 'auth/api_key_auth.dart';
1212
part 'auth/oauth.dart';
1313
part 'auth/http_basic_auth.dart';
14+
part 'auth/http_bearer_auth.dart';
1415

1516
part 'api/pet_api.dart';
1617
part 'api/store_api.dart';
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
part of openapi.api;
2+
3+
class HttpBearerAuth implements Authentication {
4+
dynamic _accessToken;
5+
6+
HttpBearerAuth() { }
7+
8+
@override
9+
void applyToParams(List<QueryParam> queryParams, Map<String, String> headerParams) {
10+
if (_accessToken is String) {
11+
headerParams["Authorization"] = "Bearer " + _accessToken;
12+
} else if (_accessToken is String Function()){
13+
headerParams["Authorization"] = "Bearer " + _accessToken();
14+
} else {
15+
throw ArgumentError('Type of Bearer accessToken should be String or String Function().');
16+
}
17+
}
18+
19+
void setAccessToken(dynamic accessToken) {
20+
if (!((accessToken is String) | (accessToken is String Function()))){
21+
throw ArgumentError('Type of Bearer accessToken should be String or String Function().');
22+
}
23+
this._accessToken = accessToken;
24+
}
25+
}

0 commit comments

Comments
 (0)