Skip to content

Commit 44c1fee

Browse files
authored
[dart] fix some compilation issues and added casts for analyzer implicit-casts flag (#8244)
* fix: missing dart casts and analyzer issues * chore: regenerate dart template files * refactor: remove unneeded implicit-casts:false and regenerate sources * chore: regenerate templates after merge * chore: regenerate dart pet store codes
1 parent 48e05ce commit 44c1fee

58 files changed

Lines changed: 321 additions & 303 deletions

Some content is hidden

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

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
@@ -64,6 +64,7 @@ public void processOpts() {
6464

6565
final String libFolder = sourceFolder + File.separator + "lib";
6666
supportingFiles.add(new SupportingFile("pubspec.mustache", "", "pubspec.yaml"));
67+
supportingFiles.add(new SupportingFile("analysis_options.mustache", "", "analysis_options.yaml"));
6768
supportingFiles.add(new SupportingFile("api_client.mustache", libFolder, "api_client.dart"));
6869
supportingFiles.add(new SupportingFile("api_exception.mustache", libFolder, "api_exception.dart"));
6970
supportingFiles.add(new SupportingFile("api_helper.mustache", libFolder, "api_helper.dart"));
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
analyzer:
2+
strong-mode:
3+
implicit-casts: true

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class ApiClient {
6565
Future<Response> invokeAPI(
6666
String path,
6767
String method,
68-
Iterable<QueryParam> queryParams,
68+
List<QueryParam> queryParams,
6969
Object body,
7070
Map<String, String> headerParams,
7171
Map<String, String> formParams,
@@ -177,13 +177,13 @@ class ApiClient {
177177
List<QueryParam> queryParams,
178178
Map<String, String> headerParams,
179179
) {
180-
authNames.forEach((authName) {
180+
for(final authName in authNames) {
181181
final auth = _authentications[authName];
182182
if (auth == null) {
183183
throw ArgumentError('Authentication undefined: $authName');
184184
}
185185
auth.applyToParams(queryParams, headerParams);
186-
});
186+
}
187187
}
188188
189189
{{#native_serialization}}

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,19 @@ class {{{classname}}} {
132132
{{/items.complexType}}
133133
{{/items.isArray}}
134134
{{^items.isArray}}
135+
{{#items.isMap}}
135136
{{{name}}}: json[r'{{{baseName}}}'] == null
136137
? null
137-
: {{{complexType}}}.mapFromJson(json[r'{{{baseName}}}']),
138+
{{#items.complexType}}
139+
: {{items.complexType}}.mapFromJson(json[r'{{{baseName}}}']),
140+
{{/items.complexType}}
141+
{{^items.complexType}}
142+
: (json[r'{{{baseName}}}'] as Map).cast<String, Map>(),
143+
{{/items.complexType}}
144+
{{/items.isMap}}
145+
{{^items.isMap}}
146+
{{{name}}}: json[r'{{{baseName}}}']
147+
{{/items.isMap}}
138148
{{/items.isArray}}
139149
{{/isMap}}
140150
{{^isMap}}
@@ -189,22 +199,22 @@ class {{{classname}}} {
189199
static List<{{{classname}}}> listFromJson(List<dynamic> json, {bool emptyIsNull, bool growable,}) =>
190200
json == null || json.isEmpty
191201
? true == emptyIsNull ? null : <{{{classname}}}>[]
192-
: json.map((v) => {{{classname}}}.fromJson(v)).toList(growable: true == growable);
202+
: json.map((dynamic value) => {{{classname}}}.fromJson(value)).toList(growable: true == growable);
193203

194204
static Map<String, {{{classname}}}> mapFromJson(Map<String, dynamic> json) {
195205
final map = <String, {{{classname}}}>{};
196-
if (json != null && json.isNotEmpty) {
197-
json.forEach((String key, dynamic v) => map[key] = {{{classname}}}.fromJson(v));
206+
if (json?.isNotEmpty == true) {
207+
json.forEach((key, value) => map[key] = {{{classname}}}.fromJson(value));
198208
}
199209
return map;
200210
}
201211

202212
// maps a json object with a list of {{{classname}}}-objects as value to a dart map
203213
static Map<String, List<{{{classname}}}>> mapListFromJson(Map<String, dynamic> json, {bool emptyIsNull, bool growable,}) {
204214
final map = <String, List<{{{classname}}}>>{};
205-
if (json != null && json.isNotEmpty) {
206-
json.forEach((String key, dynamic v) {
207-
map[key] = {{{classname}}}.listFromJson(v, emptyIsNull: emptyIsNull, growable: growable);
215+
if (json?.isNotEmpty == true) {
216+
json.forEach((key, value) {
217+
map[key] = {{{classname}}}.listFromJson(value, emptyIsNull: emptyIsNull, growable: growable,);
208218
});
209219
}
210220
return map;

samples/openapi3/client/petstore/dart2/petstore_client_lib/.openapi-generator/FILES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.gitignore
22
.travis.yml
33
README.md
4+
analysis_options.yaml
45
doc/ApiResponse.md
56
doc/Category.md
67
doc/Order.md
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
analyzer:
2+
strong-mode:
3+
implicit-casts: true

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class ApiClient {
5858
Future<Response> invokeAPI(
5959
String path,
6060
String method,
61-
Iterable<QueryParam> queryParams,
61+
List<QueryParam> queryParams,
6262
Object body,
6363
Map<String, String> headerParams,
6464
Map<String, String> formParams,
@@ -168,13 +168,13 @@ class ApiClient {
168168
List<QueryParam> queryParams,
169169
Map<String, String> headerParams,
170170
) {
171-
authNames.forEach((authName) {
171+
for(final authName in authNames) {
172172
final auth = _authentications[authName];
173173
if (auth == null) {
174174
throw ArgumentError('Authentication undefined: $authName');
175175
}
176176
auth.applyToParams(queryParams, headerParams);
177-
});
177+
}
178178
}
179179

180180
static dynamic _deserialize(dynamic value, String targetType, {bool growable}) {

samples/openapi3/client/petstore/dart2/petstore_client_lib/lib/model/api_response.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,22 @@ class ApiResponse {
6565
static List<ApiResponse> listFromJson(List<dynamic> json, {bool emptyIsNull, bool growable,}) =>
6666
json == null || json.isEmpty
6767
? true == emptyIsNull ? null : <ApiResponse>[]
68-
: json.map((v) => ApiResponse.fromJson(v)).toList(growable: true == growable);
68+
: json.map((dynamic value) => ApiResponse.fromJson(value)).toList(growable: true == growable);
6969

7070
static Map<String, ApiResponse> mapFromJson(Map<String, dynamic> json) {
7171
final map = <String, ApiResponse>{};
72-
if (json != null && json.isNotEmpty) {
73-
json.forEach((String key, dynamic v) => map[key] = ApiResponse.fromJson(v));
72+
if (json?.isNotEmpty == true) {
73+
json.forEach((key, value) => map[key] = ApiResponse.fromJson(value));
7474
}
7575
return map;
7676
}
7777

7878
// maps a json object with a list of ApiResponse-objects as value to a dart map
7979
static Map<String, List<ApiResponse>> mapListFromJson(Map<String, dynamic> json, {bool emptyIsNull, bool growable,}) {
8080
final map = <String, List<ApiResponse>>{};
81-
if (json != null && json.isNotEmpty) {
82-
json.forEach((String key, dynamic v) {
83-
map[key] = ApiResponse.listFromJson(v, emptyIsNull: emptyIsNull, growable: growable);
81+
if (json?.isNotEmpty == true) {
82+
json.forEach((key, value) {
83+
map[key] = ApiResponse.listFromJson(value, emptyIsNull: emptyIsNull, growable: growable,);
8484
});
8585
}
8686
return map;

samples/openapi3/client/petstore/dart2/petstore_client_lib/lib/model/category.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,22 @@ class Category {
5656
static List<Category> listFromJson(List<dynamic> json, {bool emptyIsNull, bool growable,}) =>
5757
json == null || json.isEmpty
5858
? true == emptyIsNull ? null : <Category>[]
59-
: json.map((v) => Category.fromJson(v)).toList(growable: true == growable);
59+
: json.map((dynamic value) => Category.fromJson(value)).toList(growable: true == growable);
6060

6161
static Map<String, Category> mapFromJson(Map<String, dynamic> json) {
6262
final map = <String, Category>{};
63-
if (json != null && json.isNotEmpty) {
64-
json.forEach((String key, dynamic v) => map[key] = Category.fromJson(v));
63+
if (json?.isNotEmpty == true) {
64+
json.forEach((key, value) => map[key] = Category.fromJson(value));
6565
}
6666
return map;
6767
}
6868

6969
// maps a json object with a list of Category-objects as value to a dart map
7070
static Map<String, List<Category>> mapListFromJson(Map<String, dynamic> json, {bool emptyIsNull, bool growable,}) {
7171
final map = <String, List<Category>>{};
72-
if (json != null && json.isNotEmpty) {
73-
json.forEach((String key, dynamic v) {
74-
map[key] = Category.listFromJson(v, emptyIsNull: emptyIsNull, growable: growable);
72+
if (json?.isNotEmpty == true) {
73+
json.forEach((key, value) {
74+
map[key] = Category.listFromJson(value, emptyIsNull: emptyIsNull, growable: growable,);
7575
});
7676
}
7777
return map;

samples/openapi3/client/petstore/dart2/petstore_client_lib/lib/model/order.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,22 +95,22 @@ class Order {
9595
static List<Order> listFromJson(List<dynamic> json, {bool emptyIsNull, bool growable,}) =>
9696
json == null || json.isEmpty
9797
? true == emptyIsNull ? null : <Order>[]
98-
: json.map((v) => Order.fromJson(v)).toList(growable: true == growable);
98+
: json.map((dynamic value) => Order.fromJson(value)).toList(growable: true == growable);
9999

100100
static Map<String, Order> mapFromJson(Map<String, dynamic> json) {
101101
final map = <String, Order>{};
102-
if (json != null && json.isNotEmpty) {
103-
json.forEach((String key, dynamic v) => map[key] = Order.fromJson(v));
102+
if (json?.isNotEmpty == true) {
103+
json.forEach((key, value) => map[key] = Order.fromJson(value));
104104
}
105105
return map;
106106
}
107107

108108
// maps a json object with a list of Order-objects as value to a dart map
109109
static Map<String, List<Order>> mapListFromJson(Map<String, dynamic> json, {bool emptyIsNull, bool growable,}) {
110110
final map = <String, List<Order>>{};
111-
if (json != null && json.isNotEmpty) {
112-
json.forEach((String key, dynamic v) {
113-
map[key] = Order.listFromJson(v, emptyIsNull: emptyIsNull, growable: growable);
111+
if (json?.isNotEmpty == true) {
112+
json.forEach((key, value) {
113+
map[key] = Order.listFromJson(value, emptyIsNull: emptyIsNull, growable: growable,);
114114
});
115115
}
116116
return map;

0 commit comments

Comments
 (0)