Skip to content

Commit bfa7ddb

Browse files
committed
added reserved word and tweaked templates
1 parent 2bcc8d2 commit bfa7ddb

File tree

5 files changed

+80
-20
lines changed

5 files changed

+80
-20
lines changed

src/main/java/io/swagger/codegen/v3/generators/dart/DartClientCodegen.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
import io.swagger.v3.oas.models.media.ArraySchema;
1111
import io.swagger.v3.oas.models.media.MapSchema;
1212
import io.swagger.v3.oas.models.media.Schema;
13+
import io.swagger.v3.oas.models.media.StringSchema;
14+
import org.apache.commons.lang3.StringUtils;
15+
import org.slf4j.Logger;
16+
import org.slf4j.LoggerFactory;
1317

1418
import java.io.File;
1519
import java.util.ArrayList;
@@ -24,6 +28,8 @@
2428

2529
public class DartClientCodegen extends DefaultCodegenConfig {
2630

31+
protected static final Logger LOGGER = LoggerFactory.getLogger(DartClientCodegen.class);
32+
2733
public static final String BROWSER_CLIENT = "browserClient";
2834
public static final String PUB_NAME = "pubName";
2935
public static final String PUB_VERSION = "pubVersion";
@@ -48,9 +54,11 @@ public DartClientCodegen() {
4854
outputFolder = "generated-code/dart";
4955
modelTemplateFiles.put("model.mustache", ".dart");
5056
apiTemplateFiles.put("api.mustache", ".dart");
57+
apiTestTemplateFiles.put("api_test.mustache", ".dart");
5158
embeddedTemplateDir = templateDir = "dart";
5259
apiPackage = "lib.api";
5360
modelPackage = "lib.model";
61+
testPackage = "tests";
5462
modelDocTemplateFiles.put("object_doc.mustache", ".md");
5563
apiDocTemplateFiles.put("api_doc.mustache", ".md");
5664

@@ -67,7 +75,7 @@ public DartClientCodegen() {
6775
"is", "library", "new", "null", "operator", "part", "rethrow",
6876
"return", "set", "static", "super", "switch", "sync*", "this",
6977
"throw", "true", "try", "typedef", "var", "void", "while",
70-
"with", "yield", "yield*" )
78+
"int", "double", "with", "yield", "yield*" )
7179
);
7280

7381
languageSpecificPrimitives = new HashSet<String>(
@@ -94,6 +102,7 @@ public DartClientCodegen() {
94102
typeMapping.put("number", "num");
95103
typeMapping.put("float", "double");
96104
typeMapping.put("double", "double");
105+
typeMapping.put("BigDecimal", "double");
97106
typeMapping.put("object", "Object");
98107
typeMapping.put("integer", "int");
99108
typeMapping.put("Date", "DateTime");
@@ -208,6 +217,11 @@ public String modelFileFolder() {
208217
return outputFolder + "/" + sourceFolder + "/" + modelPackage().replace('.', File.separatorChar);
209218
}
210219

220+
@Override
221+
public String apiTestFileFolder() {
222+
return outputFolder + "/" + sourceFolder + "/" + testPackage().replace('/', File.separatorChar);
223+
}
224+
211225
@Override
212226
public String apiDocFileFolder() {
213227
return (outputFolder + "/" + apiDocPath).replace('/', File.separatorChar);
@@ -256,6 +270,10 @@ public String toModelName(String name) {
256270
LOGGER.warn(name + " (reserved word) cannot be used as model filename. Renamed to " + camelize("model_" + name));
257271
name = "model_" + name; // e.g. return => ModelReturn (after camelize)
258272
}
273+
if (Character.isDigit(name.charAt(0))) {
274+
LOGGER.warn(name + " start with number. Renamed to " + camelize("model_" + name));
275+
name = "model_" + name; // e.g. return => ModelReturn (after camelize)
276+
}
259277

260278
// camelize the model name
261279
// phone_number => PhoneNumber
@@ -272,12 +290,27 @@ public String toApiFilename(String name) {
272290
return underscore(toApiName(name));
273291
}
274292

293+
@Override
294+
public String toApiTestFilename(String name) {
295+
return underscore(toApiName(name)) + "_test";
296+
}
297+
275298
@Override
276299
public String toDefaultValue(Schema schema) {
277300
if (schema instanceof MapSchema) {
278301
return "{}";
279302
} else if (schema instanceof ArraySchema) {
280303
return "[]";
304+
} else if (schema instanceof StringSchema) {
305+
if (schema.getDefault() != null) {
306+
String _default = schema.getDefault().toString();
307+
if (schema.getEnum() == null) {
308+
return String.format("\"%s\"", escapeText(_default));
309+
} else {
310+
// convert to enum var name later in postProcessModels
311+
return _default;
312+
}
313+
}
281314
}
282315
return super.toDefaultValue(schema);
283316
}
@@ -311,6 +344,9 @@ public String getSchemaType(Schema schema) {
311344
} else {
312345
type = swaggerType;
313346
}
347+
if (type == null) {
348+
type = "Object";
349+
}
314350
return toModelName(type);
315351
}
316352

src/main/resources/handlebars/dart/api.mustache

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,25 @@ class {{classname}} {
99
{{classname}}([ApiClient apiClient]) : apiClient = apiClient ?? defaultApiClient;
1010

1111
{{#operation}}
12+
{{#contents}}
13+
{{#@first}}
1214
/// {{summary}}
1315
///
1416
/// {{notes}}
1517
{{#isDeprecated}}
1618
@deprecated
1719
{{/isDeprecated}}
18-
{{#returnType}}Future<{{{returnType}}}> {{/returnType}}{{^returnType}}Future {{/returnType}}{{nickname}}({{#allParams}}{{#required}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/required}}{{/allParams}}{{#hasOptionalParams}}{ {{#allParams}}{{^required}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/required}}{{/allParams}} }{{/hasOptionalParams}}) async {
20+
{{#returnType}}Future<{{{returnType}}}> {{/returnType}}{{^returnType}}Future {{/returnType}}{{nickname}}({{#parameters}}{{#required}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/required}}{{/parameters}}{{#hasOptionalParams}}{ {{#parameters}}{{^required}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/required}}{{/parameters}} }{{/hasOptionalParams}}) async {
1921
Object postBody = {{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}};
2022

2123
// verify required params are set
22-
{{#allParams}}
24+
{{#parameters}}
2325
{{#required}}
2426
if({{paramName}} == null) {
2527
throw new ApiException(400, "Missing required param: {{paramName}}");
2628
}
2729
{{/required}}
28-
{{/allParams}}
30+
{{/parameters}}
2931

3032
// create path and map variables
3133
String path = "{{{path}}}".replaceAll("{format}","json"){{#pathParams}}.replaceAll("{" + "{{baseName}}" + "}", {{{paramName}}}.toString()){{/pathParams}};
@@ -103,6 +105,8 @@ class {{classname}} {
103105
return {{#returnType}}null{{/returnType}};
104106
}
105107
}
108+
{{/@first}}
109+
{{/contents}}
106110
{{/operation}}
107111
}
108112
{{/operations}}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import 'package:{{pubName}}/api.dart';
2+
import 'package:test/test.dart';
3+
4+
{{#operations}}
5+
6+
/// tests for {{classname}}
7+
void main() {
8+
var instance = new {{classname}}();
9+
10+
group('tests for {{classname}}', () {
11+
{{#operation}}
12+
{{#summary}}
13+
// {{{.}}}
14+
//
15+
{{/summary}}
16+
{{#notes}}
17+
// {{{.}}}
18+
//
19+
{{/notes}}
20+
//{{#returnType}}Future<{{{returnType}}}> {{/returnType}}{{^returnType}}Future {{/returnType}}{{operationId}}({{#allParams}}{{#required}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/required}}{{/allParams}}{{#hasOptionalParams}}{ {{#allParams}}{{^required}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/required}}{{/allParams}} }{{/hasOptionalParams}}) async
21+
test('test {{operationId}}', () async {
22+
// TODO
23+
});
24+
25+
{{/operation}}
26+
});
27+
}
28+
{{/operations}}

src/main/resources/handlebars/dart/class.mustache

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
class {{classname}} {
22
{{#vars}}{{#description}}/* {{{description}}} */{{/description}}
33
{{{datatype}}} {{name}} = {{{defaultValue}}};
4-
{{#allowableValues}}{{#min}} // range from {{min}} to {{max}}{{/min}}//{{^min}}enum {{name}}Enum { {{#values}} {{.}}, {{/values}} };{{/min}}{{/allowableValues}}
4+
{{#allowableValues}}
5+
{{#min}} // range from {{min}} to {{max}}{{/min}}//{{^min}}enum {{name}}Enum { {{#values}} {{.}}, {{/values}} };{{/min}}
6+
{{/allowableValues}}
57
{{/vars}}
8+
69
{{classname}}();
710

811
@override
@@ -17,20 +20,7 @@ class {{classname}} {
1720
{{name}} = json['{{baseName}}'] == null ? null : DateTime.parse(json['{{baseName}}']);
1821
{{/isDateTime}}
1922
{{^isDateTime}}
20-
{{name}} =
21-
{{#complexType}}
22-
{{#isListContainer}}{{complexType}}.listFromJson(json['{{baseName}}']){{/isListContainer}}{{^isListContainer}}
23-
{{#isMapContainer}}{{complexType}}.mapFromJson(json['{{baseName}}']){{/isMapContainer}}
24-
{{^isMapContainer}}new {{complexType}}.fromJson(json['{{baseName}}']){{/isMapContainer}}{{/isListContainer}}
25-
{{/complexType}}
26-
{{^complexType}}
27-
{{#isListContainer}}
28-
(json['{{baseName}}'] as List).map((item) => item as {{items.datatype}}).toList()
29-
{{/isListContainer}}
30-
{{^isListContainer}}
31-
json['{{baseName}}']
32-
{{/isListContainer}}
33-
{{/complexType}};
23+
{{name}} = {{#complexType}}{{#isListContainer}}{{complexType}}.listFromJson(json['{{baseName}}']){{/isListContainer}}{{^isListContainer}}{{#isMapContainer}}{{complexType}}.mapFromJson(json['{{baseName}}']){{/isMapContainer}}{{^isMapContainer}}new {{complexType}}.fromJson(json['{{baseName}}']){{/isMapContainer}}{{/isListContainer}}{{/complexType}}{{^complexType}}{{#isListContainer}}(json['{{baseName}}'] as List).map((item) => item as {{items.datatype}}).toList(){{/isListContainer}}{{^isListContainer}}json['{{baseName}}']{{/isListContainer}}{{/complexType}};
3424
{{/isDateTime}}
3525
{{/vars}}
3626
}

src/main/resources/handlebars/dart/pubspec.mustache

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ name: {{pubName}}
22
version: {{pubVersion}}
33
description: {{pubDescription}}
44
dependencies:
5-
http: '>=0.11.1 <0.12.0'
5+
http: '>=0.11.1 <0.13.0'
6+
dev_dependencies:
7+
test: ^1.3.0

0 commit comments

Comments
 (0)