Skip to content

Commit 4b0fc30

Browse files
pmundtwing328
authored andcommitted
[dart2] Fix up test code generation, double deserialization, 'new' keyword deprecation (#3576)
* dart2: Use the correct classname in test generation At present test generation has a stray hard-coded reference to the pet store Pet() class, which should reflect the actual classname under test. * dart2: Call toDouble() in generated code for double types At present the generated code does not correctly handle transitioning to double when dealing with non-integer types in JSON deserialization. This ends up with dart raising an unhandled type mismatch exception: Unhandled exception: type 'int' is not a subtype of type 'double' where ... Using the .toDouble() conversion when a double type is expected fixes this up by making the typing explicit. * dart2: Drop use of deprecated 'new' keyword in generated code The use of the 'new' keyword in dart2 is deprecated and should be avoided, as per the official guidance of the dart2 authors: https://dart.dev/guides/language/effective-dart/usage#dont-use-new * dart2: Regenerate samples for mustache template changes
1 parent 1152af4 commit 4b0fc30

113 files changed

Lines changed: 1003 additions & 746 deletions

File tree

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/resources/dart2/README.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ import 'package:{{pubName}}/api.dart';
7474
{{/authMethods}}
7575
{{/hasAuthMethods}}
7676

77-
var api_instance = new {{classname}}();
77+
var api_instance = {{classname}}();
7878
{{#allParams}}
79-
var {{paramName}} = {{#isListContainer}}[{{/isListContainer}}{{#isBodyParam}}new {{dataType}}(){{/isBodyParam}}{{^isBodyParam}}{{{example}}}{{/isBodyParam}}{{#isListContainer}}]{{/isListContainer}}; // {{{dataType}}} | {{{description}}}
79+
var {{paramName}} = {{#isListContainer}}[{{/isListContainer}}{{#isBodyParam}}{{dataType}}(){{/isBodyParam}}{{^isBodyParam}}{{{example}}}{{/isBodyParam}}{{#isListContainer}}]{{/isListContainer}}; // {{{dataType}}} | {{{description}}}
8080
{{/allParams}}
8181

8282
try {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class {{classname}} {
1919
{{#allParams}}
2020
{{#required}}
2121
if({{paramName}} == null) {
22-
throw new ApiException(400, "Missing required param: {{paramName}}");
22+
throw ApiException(400, "Missing required param: {{paramName}}");
2323
}
2424
{{/required}}
2525
{{/allParams}}
@@ -51,7 +51,7 @@ class {{classname}} {
5151

5252
if(contentType.startsWith("multipart/form-data")) {
5353
bool hasFields = false;
54-
MultipartRequest mp = new MultipartRequest(null, null);
54+
MultipartRequest mp = MultipartRequest(null, null);
5555
{{#formParams}}
5656
{{^isFile}}
5757
if ({{paramName}} != null) {
@@ -89,7 +89,7 @@ class {{classname}} {
8989
authNames);
9090

9191
if(response.statusCode >= 400) {
92-
throw new ApiException(response.statusCode, _decodeBodyBytes(response));
92+
throw ApiException(response.statusCode, _decodeBodyBytes(response));
9393
} else if(response.body != null) {
9494
{{#isListContainer}}
9595
{{#returnType}}
@@ -99,7 +99,7 @@ class {{classname}} {
9999
{{^isListContainer}}
100100
{{#isMapContainer}}
101101
{{#returnType}}
102-
return new {{{returnType}}}.from(apiClient.deserialize(_decodeBodyBytes(response), '{{{returnType}}}'));
102+
return {{{returnType}}}.from(apiClient.deserialize(_decodeBodyBytes(response), '{{{returnType}}}'));
103103
{{/returnType}};
104104
{{/isMapContainer}}
105105
{{^isMapContainer}}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ import 'package:{{pubName}}/api.dart';
4545
{{/authMethods}}
4646
{{/hasAuthMethods}}
4747

48-
var api_instance = new {{classname}}();
48+
var api_instance = {{classname}}();
4949
{{#allParams}}
50-
var {{paramName}} = {{#isListContainer}}[{{/isListContainer}}{{#isBodyParam}}new {{dataType}}(){{/isBodyParam}}{{^isBodyParam}}{{{example}}}{{/isBodyParam}}{{#isListContainer}}]{{/isListContainer}}; // {{{dataType}}} | {{{description}}}
50+
var {{paramName}} = {{#isListContainer}}[{{/isListContainer}}{{#isBodyParam}}{{dataType}}(){{/isBodyParam}}{{^isBodyParam}}{{{example}}}{{/isBodyParam}}{{#isListContainer}}]{{/isListContainer}}; // {{{dataType}}} | {{{description}}}
5151
{{/allParams}}
5252

5353
try {

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

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

66
/// tests for {{classname}}
77
void main() {
8-
var instance = new {{classname}}();
8+
var instance = {{classname}}();
99

1010
group('tests for {{classname}}', () {
1111
{{#operation}}

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class {{classname}} {
3636
{{name}} = {{complexType}}.mapFromJson(json['{{baseName}}']);
3737
{{/isMapContainer}}
3838
{{^isMapContainer}}
39-
{{name}} = new {{complexType}}.fromJson(json['{{baseName}}']);
39+
{{name}} = {{complexType}}.fromJson(json['{{baseName}}']);
4040
{{/isMapContainer}}
4141
{{/isListContainer}}
4242
{{/complexType}}
@@ -49,7 +49,12 @@ class {{classname}} {
4949
{{name}} = (json['{{baseName}}'] as Map).cast<String, {{items.datatype}}>();
5050
{{/isMapContainer}}
5151
{{^isMapContainer}}
52+
{{#isDouble}}
53+
{{name}} = json['{{baseName}}'].toDouble();
54+
{{/isDouble}}
55+
{{^isDouble}}
5256
{{name}} = json['{{baseName}}'];
57+
{{/isDouble}}
5358
{{/isMapContainer}}
5459
{{/isListContainer}}
5560
{{/complexType}}
@@ -81,13 +86,13 @@ class {{classname}} {
8186
}
8287

8388
static List<{{classname}}> listFromJson(List<dynamic> json) {
84-
return json == null ? new List<{{classname}}>() : json.map((value) => new {{classname}}.fromJson(value)).toList();
89+
return json == null ? List<{{classname}}>() : json.map((value) => {{classname}}.fromJson(value)).toList();
8590
}
8691

8792
static Map<String, {{classname}}> mapFromJson(Map<String, dynamic> json) {
88-
var map = new Map<String, {{classname}}>();
93+
var map = Map<String, {{classname}}>();
8994
if (json != null && json.isNotEmpty) {
90-
json.forEach((String key, dynamic value) => map[key] = new {{classname}}.fromJson(value));
95+
json.forEach((String key, dynamic value) => map[key] = {{classname}}.fromJson(value));
9196
}
9297
return map;
9398
}

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

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

66
// tests for {{classname}}
77
void main() {
8-
var instance = new Pet();
8+
var instance = {{classname}}();
99

1010
group('test {{classname}}', () {
1111
{{#vars}}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4.0.1-SNAPSHOT
1+
4.1.0-SNAPSHOT

samples/client/petstore/dart/flutter_petstore/openapi/.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
language: dart
44
dart:
55
# Install a specific stable release
6-
- "1.24.3"
6+
- "2.2.0"
77
install:
88
- pub get
99

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ Please follow the [installation procedure](#installation--usage) and then run th
4444
import 'package:openapi/api.dart';
4545
4646
// TODO Configure OAuth2 access token for authorization: petstore_auth
47-
//openapi.api.Configuration.accessToken = 'YOUR_ACCESS_TOKEN';
47+
//defaultApiClient.getAuthentication<OAuth>('petstore_auth').accessToken = 'YOUR_ACCESS_TOKEN';
4848
49-
var api_instance = new PetApi();
50-
var body = new Pet(); // Pet | Pet object that needs to be added to the store
49+
var api_instance = PetApi();
50+
var pet = Pet(); // Pet | Pet object that needs to be added to the store
5151
5252
try {
53-
api_instance.addPet(body);
53+
api_instance.addPet(pet);
5454
} catch (e) {
5555
print("Exception when calling PetApi->addPet: $e\n");
5656
}
@@ -89,6 +89,8 @@ Class | Method | HTTP request | Description
8989

9090
- [ApiResponse](docs//ApiResponse.md)
9191
- [Category](docs//Category.md)
92+
- [InlineObject](docs//InlineObject.md)
93+
- [InlineObject1](docs//InlineObject1.md)
9294
- [Order](docs//Order.md)
9395
- [Pet](docs//Pet.md)
9496
- [Tag](docs//Tag.md)
@@ -104,6 +106,12 @@ Class | Method | HTTP request | Description
104106
- **API key parameter name**: api_key
105107
- **Location**: HTTP header
106108

109+
## auth_cookie
110+
111+
- **Type**: API key
112+
- **API key parameter name**: AUTH_KEY
113+
- **Location**:
114+
107115
## petstore_auth
108116

109117
- **Type**: OAuth
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+

0 commit comments

Comments
 (0)