Skip to content

Commit 9d3f5a9

Browse files
janmigawing328
authored andcommitted
[Dart] Fixes TypeError in Dart 2 mode (#7959)
* Properly convert lists to dart types * Updated sample petstore client * Fixed maps in Dart strong mode Fixed list parsing for null-elements
1 parent 7de4952 commit 9d3f5a9

Some content is hidden

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

44 files changed

+358
-284
lines changed

modules/swagger-codegen/src/main/resources/dart/api.mustache

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,18 @@ class {{classname}} {
8484
if(response.statusCode >= 400) {
8585
throw new ApiException(response.statusCode, response.body);
8686
} else if(response.body != null) {
87-
return {{#returnType}}apiClient.deserialize(response.body, '{{{returnType}}}') as {{{returnType}}} {{/returnType}};
87+
return
88+
{{#isListContainer}}
89+
{{#returnType}}(apiClient.deserialize(response.body, '{{{returnType}}}') as List).map((item) => item as {{returnBaseType}}).toList();{{/returnType}}
90+
{{/isListContainer}}
91+
{{^isListContainer}}
92+
{{#isMapContainer}}
93+
{{#returnType}}new {{{returnType}}}.from(apiClient.deserialize(response.body, '{{{returnType}}}')) {{/returnType}};
94+
{{/isMapContainer}}
95+
{{^isMapContainer}}
96+
{{#returnType}}apiClient.deserialize(response.body, '{{{returnType}}}') as {{{returnType}}} {{/returnType}};
97+
{{/isMapContainer}}
98+
{{/isListContainer}}
8899
} else {
89100
return {{#returnType}}null{{/returnType}};
90101
}

modules/swagger-codegen/src/main/resources/dart/class.mustache

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,18 @@ class {{classname}} {
1919
{{^isDateTime}}
2020
{{name}} =
2121
{{#complexType}}
22-
{{#isListContainer}}{{complexType}}.listFromJson(json['{{name}}']){{/isListContainer}}{{^isListContainer}}
23-
{{#isMapContainer}}{{complexType}}.mapFromJson(json['{{name}}']){{/isMapContainer}}
24-
{{^isMapContainer}}new {{complexType}}.fromJson(json['{{name}}']){{/isMapContainer}}{{/isListContainer}}
22+
{{#isListContainer}}{{complexType}}.listFromJson(json['{{name}}']){{/isListContainer}}{{^isListContainer}}
23+
{{#isMapContainer}}{{complexType}}.mapFromJson(json['{{name}}']){{/isMapContainer}}
24+
{{^isMapContainer}}new {{complexType}}.fromJson(json['{{name}}']){{/isMapContainer}}{{/isListContainer}}
2525
{{/complexType}}
26-
{{^complexType}}json['{{name}}']{{/complexType}};
26+
{{^complexType}}
27+
{{#isListContainer}}
28+
(json['{{name}}'] as List).map((item) => item as {{items.datatype}}).toList()
29+
{{/isListContainer}}
30+
{{^isListContainer}}
31+
json['{{name}}']
32+
{{/isListContainer}}
33+
{{/complexType}};
2734
{{/isDateTime}}
2835
{{/vars}}
2936
}
@@ -36,12 +43,8 @@ class {{classname}} {
3643
};
3744
}
3845

39-
static List<{{classname}}> listFromJson(List<Map<String, dynamic>> json) {
40-
var list = new List<{{classname}}>();
41-
if (json != null && json.length > 0) {
42-
json.forEach((Map<String, dynamic> value) => list.add(new {{classname}}.fromJson(value)));
43-
}
44-
return list;
46+
static List<{{classname}}> listFromJson(List<dynamic> json) {
47+
return json == null ? new List<{{classname}}>() : json.map((value) => new {{classname}}.fromJson(value)).toList();
4548
}
4649

4750
static Map<String, {{classname}}> mapFromJson(Map<String, Map<String, dynamic>> json) {

samples/client/petstore/dart/flutter_petstore/swagger/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ try {
5959

6060
## Documentation for API Endpoints
6161

62-
All URIs are relative to *http://localhost/v2*
62+
All URIs are relative to *http://petstore.swagger.io/v2*
6363

6464
Class | Method | HTTP request | Description
6565
------------ | ------------- | ------------- | -------------

samples/client/petstore/dart/flutter_petstore/swagger/docs/PetApi.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import 'package:swagger/api.dart';
66
```
77

8-
All URIs are relative to *http://localhost/v2*
8+
All URIs are relative to *http://petstore.swagger.io/v2*
99

1010
Method | HTTP request | Description
1111
------------- | ------------- | -------------

samples/client/petstore/dart/flutter_petstore/swagger/docs/StoreApi.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import 'package:swagger/api.dart';
66
```
77

8-
All URIs are relative to *http://localhost/v2*
8+
All URIs are relative to *http://petstore.swagger.io/v2*
99

1010
Method | HTTP request | Description
1111
------------- | ------------- | -------------

samples/client/petstore/dart/flutter_petstore/swagger/docs/UserApi.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import 'package:swagger/api.dart';
66
```
77

8-
All URIs are relative to *http://localhost/v2*
8+
All URIs are relative to *http://petstore.swagger.io/v2*
99

1010
Method | HTTP request | Description
1111
------------- | ------------- | -------------

samples/client/petstore/dart/flutter_petstore/swagger/lib/api/pet_api.dart

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ class PetApi {
5353
if(response.statusCode >= 400) {
5454
throw new ApiException(response.statusCode, response.body);
5555
} else if(response.body != null) {
56-
return ;
56+
return
57+
;
5758
} else {
5859
return ;
5960
}
@@ -105,7 +106,8 @@ class PetApi {
105106
if(response.statusCode >= 400) {
106107
throw new ApiException(response.statusCode, response.body);
107108
} else if(response.body != null) {
108-
return ;
109+
return
110+
;
109111
} else {
110112
return ;
111113
}
@@ -157,7 +159,8 @@ class PetApi {
157159
if(response.statusCode >= 400) {
158160
throw new ApiException(response.statusCode, response.body);
159161
} else if(response.body != null) {
160-
return apiClient.deserialize(response.body, 'List<Pet>') as List<Pet> ;
162+
return
163+
(apiClient.deserialize(response.body, 'List<Pet>') as List).map((item) => item as Pet).toList();
161164
} else {
162165
return null;
163166
}
@@ -209,7 +212,8 @@ class PetApi {
209212
if(response.statusCode >= 400) {
210213
throw new ApiException(response.statusCode, response.body);
211214
} else if(response.body != null) {
212-
return apiClient.deserialize(response.body, 'List<Pet>') as List<Pet> ;
215+
return
216+
(apiClient.deserialize(response.body, 'List<Pet>') as List).map((item) => item as Pet).toList();
213217
} else {
214218
return null;
215219
}
@@ -260,7 +264,8 @@ class PetApi {
260264
if(response.statusCode >= 400) {
261265
throw new ApiException(response.statusCode, response.body);
262266
} else if(response.body != null) {
263-
return apiClient.deserialize(response.body, 'Pet') as Pet ;
267+
return
268+
apiClient.deserialize(response.body, 'Pet') as Pet ;
264269
} else {
265270
return null;
266271
}
@@ -311,7 +316,8 @@ class PetApi {
311316
if(response.statusCode >= 400) {
312317
throw new ApiException(response.statusCode, response.body);
313318
} else if(response.body != null) {
314-
return ;
319+
return
320+
;
315321
} else {
316322
return ;
317323
}
@@ -376,7 +382,8 @@ if (status != null)
376382
if(response.statusCode >= 400) {
377383
throw new ApiException(response.statusCode, response.body);
378384
} else if(response.body != null) {
379-
return ;
385+
return
386+
;
380387
} else {
381388
return ;
382389
}
@@ -441,7 +448,8 @@ if (status != null)
441448
if(response.statusCode >= 400) {
442449
throw new ApiException(response.statusCode, response.body);
443450
} else if(response.body != null) {
444-
return apiClient.deserialize(response.body, 'ApiResponse') as ApiResponse ;
451+
return
452+
apiClient.deserialize(response.body, 'ApiResponse') as ApiResponse ;
445453
} else {
446454
return null;
447455
}

samples/client/petstore/dart/flutter_petstore/swagger/lib/api/store_api.dart

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ class StoreApi {
5353
if(response.statusCode >= 400) {
5454
throw new ApiException(response.statusCode, response.body);
5555
} else if(response.body != null) {
56-
return ;
56+
return
57+
;
5758
} else {
5859
return ;
5960
}
@@ -101,7 +102,8 @@ class StoreApi {
101102
if(response.statusCode >= 400) {
102103
throw new ApiException(response.statusCode, response.body);
103104
} else if(response.body != null) {
104-
return apiClient.deserialize(response.body, 'Map<String, int>') as Map<String, int> ;
105+
return
106+
new Map<String, int>.from(apiClient.deserialize(response.body, 'Map<String, int>')) ;
105107
} else {
106108
return null;
107109
}
@@ -152,7 +154,8 @@ class StoreApi {
152154
if(response.statusCode >= 400) {
153155
throw new ApiException(response.statusCode, response.body);
154156
} else if(response.body != null) {
155-
return apiClient.deserialize(response.body, 'Order') as Order ;
157+
return
158+
apiClient.deserialize(response.body, 'Order') as Order ;
156159
} else {
157160
return null;
158161
}
@@ -203,7 +206,8 @@ class StoreApi {
203206
if(response.statusCode >= 400) {
204207
throw new ApiException(response.statusCode, response.body);
205208
} else if(response.body != null) {
206-
return apiClient.deserialize(response.body, 'Order') as Order ;
209+
return
210+
apiClient.deserialize(response.body, 'Order') as Order ;
207211
} else {
208212
return null;
209213
}

samples/client/petstore/dart/flutter_petstore/swagger/lib/api/user_api.dart

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ class UserApi {
5353
if(response.statusCode >= 400) {
5454
throw new ApiException(response.statusCode, response.body);
5555
} else if(response.body != null) {
56-
return ;
56+
return
57+
;
5758
} else {
5859
return ;
5960
}
@@ -104,7 +105,8 @@ class UserApi {
104105
if(response.statusCode >= 400) {
105106
throw new ApiException(response.statusCode, response.body);
106107
} else if(response.body != null) {
107-
return ;
108+
return
109+
;
108110
} else {
109111
return ;
110112
}
@@ -155,7 +157,8 @@ class UserApi {
155157
if(response.statusCode >= 400) {
156158
throw new ApiException(response.statusCode, response.body);
157159
} else if(response.body != null) {
158-
return ;
160+
return
161+
;
159162
} else {
160163
return ;
161164
}
@@ -206,7 +209,8 @@ class UserApi {
206209
if(response.statusCode >= 400) {
207210
throw new ApiException(response.statusCode, response.body);
208211
} else if(response.body != null) {
209-
return ;
212+
return
213+
;
210214
} else {
211215
return ;
212216
}
@@ -257,7 +261,8 @@ class UserApi {
257261
if(response.statusCode >= 400) {
258262
throw new ApiException(response.statusCode, response.body);
259263
} else if(response.body != null) {
260-
return apiClient.deserialize(response.body, 'User') as User ;
264+
return
265+
apiClient.deserialize(response.body, 'User') as User ;
261266
} else {
262267
return null;
263268
}
@@ -313,7 +318,8 @@ class UserApi {
313318
if(response.statusCode >= 400) {
314319
throw new ApiException(response.statusCode, response.body);
315320
} else if(response.body != null) {
316-
return apiClient.deserialize(response.body, 'String') as String ;
321+
return
322+
apiClient.deserialize(response.body, 'String') as String ;
317323
} else {
318324
return null;
319325
}
@@ -361,7 +367,8 @@ class UserApi {
361367
if(response.statusCode >= 400) {
362368
throw new ApiException(response.statusCode, response.body);
363369
} else if(response.body != null) {
364-
return ;
370+
return
371+
;
365372
} else {
366373
return ;
367374
}
@@ -415,7 +422,8 @@ class UserApi {
415422
if(response.statusCode >= 400) {
416423
throw new ApiException(response.statusCode, response.body);
417424
} else if(response.body != null) {
418-
return ;
425+
return
426+
;
419427
} else {
420428
return ;
421429
}

samples/client/petstore/dart/flutter_petstore/swagger/lib/api_client.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class ApiClient {
1818
final _RegList = new RegExp(r'^List<(.*)>$');
1919
final _RegMap = new RegExp(r'^Map<String,(.*)>$');
2020

21-
ApiClient({this.basePath: "http://localhost/v2"}) {
21+
ApiClient({this.basePath: "http://petstore.swagger.io/v2"}) {
2222
// Setup authentications (key: authentication name, value: authentication).
2323
_authentications['api_key'] = new ApiKeyAuth("header", "api_key");
2424
_authentications['petstore_auth'] = new OAuth();

0 commit comments

Comments
 (0)