Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dart2 custom templates #3656

Merged
merged 4 commits into from
Aug 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,10 @@ public void processOpts() {
} else {
// dart 2.x
LOGGER.info("Dart version: 2.x");
embeddedTemplateDir = templateDir = "dart2";
// check to not overwrite a custom templateDir
if (templateDir == null) {
embeddedTemplateDir = templateDir = "dart2";
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this may be causing issues. I ran ./bin/dart2-petstore.sh and got the following diff (partial):

--- a/samples/client/petstore/dart2/flutter_petstore/openapi/.travis.yml
+++ b/samples/client/petstore/dart2/flutter_petstore/openapi/.travis.yml
@@ -3,7 +3,7 @@
 language: dart
 dart:
 # Install a specific stable release
-- "2.2.0"
+- "1.24.3"
 install:
 - pub get
 
diff --git a/samples/client/petstore/dart2/flutter_petstore/openapi/README.md b/samples/client/petstore/dart2/flutter_petstore/openapi/README.md
index e78e0e3e69..8520a219f8 100644
--- a/samples/client/petstore/dart2/flutter_petstore/openapi/README.md
+++ b/samples/client/petstore/dart2/flutter_petstore/openapi/README.md
@@ -44,10 +44,10 @@ Please follow the [installation procedure](#installation--usage) and then run th
 import 'package:openapi/api.dart';
 
 // TODO Configure OAuth2 access token for authorization: petstore_auth
-//defaultApiClient.getAuthentication<OAuth>('petstore_auth').accessToken = 'YOUR_ACCESS_TOKEN';
+//openapi.api.Configuration.accessToken = 'YOUR_ACCESS_TOKEN';
 
-var api_instance = PetApi();
-var body = Pet(); // Pet | Pet object that needs to be added to the store
+var api_instance = new PetApi();
+var body = new Pet(); // Pet | Pet object that needs to be added to the store
 
 try {

so dart 2 seems to be using dart 1.x template instead.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and undo these changes (line 205 to 208) seem to have fixed the issue.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest you remove these changes so that we can merge the remaining changes into master to give us more time reviewing this particular issue.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I know what the problem is and will file another PR to fix it after merging your PR

}

final String libFolder = sourceFolder + File.separator + "lib";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,7 @@ class ApiClient {
{{#model}}
case '{{classname}}':
{{#isEnum}}
// Enclose the value in a list so that Dartson can use a transformer
// to decode it.
final listValue = [value];
final List<dynamic> listResult = dson.map(listValue, []);
return listResult[0];
return new {{classname}}TypeTransformer().decode(value);
alexnavratil marked this conversation as resolved.
Show resolved Hide resolved
{{/isEnum}}
{{^isEnum}}
return {{classname}}.fromJson(value);
Expand Down
18 changes: 18 additions & 0 deletions modules/openapi-generator/src/main/resources/dart2/class.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,16 @@ class {{classname}} {
{{/isListContainer}}
{{^isListContainer}}
{{#isMapContainer}}
{{#items.isListContainer}}
{{name}} = (json['{{baseName}}'] == null) ?
null :
{{items.complexType}}.mapListFromJson(json['{{baseName}}']);
{{/items.isListContainer}}
{{^items.isListContainer}}
{{name}} = (json['{{baseName}}'] == null) ?
null :
{{complexType}}.mapFromJson(json['{{baseName}}']);
{{/items.isListContainer}}
{{/isMapContainer}}
{{^isMapContainer}}
{{name}} = (json['{{baseName}}'] == null) ?
Expand Down Expand Up @@ -108,4 +115,15 @@ class {{classname}} {
}
return map;
}

alexnavratil marked this conversation as resolved.
Show resolved Hide resolved
// maps a json object with a list of {{classname}}-objects as value to a dart map
static Map<String, List<{{classname}}>> mapListFromJson(Map<String, dynamic> json) {
var map = Map<String, List<{{classname}}>>();
if (json != null && json.isNotEmpty) {
json.forEach((String key, dynamic value) {
map[key] = {{classname}}.listFromJson(value);
});
}
return map;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
@Entity()
class {{classname}} {
/// The underlying value of this enum member.
final {{dataType}} value;
Expand All @@ -13,16 +12,18 @@ class {{classname}} {
static const {{classname}} {{{name}}} = const {{classname}}._internal({{{value}}});
{{/enumVars}}
{{/allowableValues}}

static {{classname}} fromJson(String value) {
return new {{classname}}TypeTransformer().decode(value);
}
}

class {{classname}}TypeTransformer extends TypeTransformer<{{classname}}> {
class {{classname}}TypeTransformer {

@override
dynamic encode({{classname}} data) {
return data.value;
}

@override
{{classname}} decode(dynamic data) {
switch (data) {
{{#allowableValues}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import 'package:test/test.dart';

// tests for {{classname}}
void main() {
var instance = {{classname}}();
{{^isEnum}}
var instance = new {{classname}}();
{{/isEnum}}

group('test {{classname}}', () {
{{#vars}}
Expand Down