Skip to content

Commit 033d985

Browse files
authored
[Dart-dio] Add basic integration testing and fix compile errors (#8025)
* [dart-dio] Add basic integration test POM This basically just fetches dependencies, runs the built_value generator and empty test cases. Even running empty test cases is more than is currently possible and at least finds compile errors. There are compile errors atm which need to be fixed. * [dart-dio] Fix missing BuiltSet import in models that use enums * [dart-dio] Fix compile error when the return type is a Map * the compile error was `serializerForType(Map<String, int>)` in `StoreApi` which needs to be `serializerForType(Map)` * use final instead of var in response handling * [dart-dio] Generate docs after changes * [dart-dio] Add integration test to CI execution list
1 parent 82c5021 commit 033d985

File tree

26 files changed

+603
-39
lines changed

26 files changed

+603
-39
lines changed

docs/generators/dart-dio.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
3232
| ---------- | ------- |
3333
|BuiltList|package:built_collection/built_collection.dart|
3434
|BuiltMap|package:built_collection/built_collection.dart|
35+
|BuiltSet|package:built_collection/built_collection.dart|
3536
|JsonObject|package:built_value/json_object.dart|
3637
|Uint8List|dart:typed_data|
3738

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/DartClientCodegen.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ public void postProcessFile(File file, String fileType) {
617617
return; // skip if DART_POST_PROCESS_FILE env variable is not defined
618618
}
619619

620-
// only procees the following type (or we can simply rely on the file extension to check if it's a Dart file)
620+
// only process the following type (or we can simply rely on the file extension to check if it's a Dart file)
621621
Set<String> supportedFileType = Sets.newHashSet(
622622
"supporting-mustache",
623623
"model-test",

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/DartDioClientCodegen.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public class DartDioClientCodegen extends DartClientCodegen {
5151

5252
private static final String IS_FORMAT_JSON = "jsonFormat";
5353
private static final String CLIENT_NAME = "clientName";
54-
private static Set<String> modelToIgnore = new HashSet<>();
54+
private static final Set<String> modelToIgnore = new HashSet<>();
5555

5656
static {
5757
modelToIgnore.add("datetime");
@@ -71,10 +71,6 @@ public DartDioClientCodegen() {
7171
embeddedTemplateDir = "dart-dio";
7272
this.setTemplateDir(embeddedTemplateDir);
7373

74-
//no tests at this time
75-
modelTestTemplateFiles.clear();
76-
apiTestTemplateFiles.clear();
77-
7874
cliOptions.add(new CliOption(NULLABLE_FIELDS, "Is the null fields should be in the JSON payload"));
7975
CliOption dateLibrary = new CliOption(DATE_LIBRARY, "Option. Date library to use").defaultValue(this.getDateLibrary());
8076
Map<String, String> dateOptions = new HashMap<>();
@@ -88,6 +84,7 @@ public DartDioClientCodegen() {
8884
typeMapping.put("AnyType", "Object");
8985

9086
importMapping.put("BuiltList", "package:built_collection/built_collection.dart");
87+
importMapping.put("BuiltSet", "package:built_collection/built_collection.dart");
9188
importMapping.put("BuiltMap", "package:built_collection/built_collection.dart");
9289
importMapping.put("JsonObject", "package:built_value/json_object.dart");
9390
importMapping.put("Uint8List", "dart:typed_data");
@@ -109,7 +106,6 @@ public void setNullableFields(boolean nullableFields) {
109106
this.nullableFields = nullableFields;
110107
}
111108

112-
113109
@Override
114110
public String getName() {
115111
return "dart-dio";
@@ -316,6 +312,11 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert
316312
if (property.dataType.contains("JsonObject")) {
317313
model.imports.add("JsonObject");
318314
}
315+
316+
if (property.isEnum) {
317+
// enums are generated with built_value and make use of BuiltSet
318+
model.imports.add("BuiltSet");
319+
}
319320
}
320321

321322
@Override
@@ -378,5 +379,4 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
378379
return objs;
379380
}
380381

381-
382382
}

modules/openapi-generator/src/main/resources/dart-dio/api.mustache

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,18 +95,23 @@ class {{classname}} {
9595
){{#returnType}}.then((response) {
9696
9797
{{#isResponseFile}}
98-
var data = response.data;
98+
final data = response.data;
9999
{{/isResponseFile}}
100100
{{^isResponseFile}}
101-
{{#isArray}}
102-
final FullType type = const FullType(BuiltList, const [const FullType({{returnBaseType}})]);
103-
BuiltList<{{returnBaseType}}> dataList = _serializers.deserialize(response.data is String ? jsonDecode(response.data) : response.data, specifiedType: type);
104-
var data = dataList.toList();
105-
{{/isArray}}
106-
{{^isArray}}
107-
var serializer = _serializers.serializerForType({{{returnType}}});
108-
var data = _serializers.deserializeWith<{{{returnType}}}>(serializer, response.data is String ? jsonDecode(response.data) : response.data);
109-
{{/isArray}}
101+
{{#isArray}}
102+
final FullType type = const FullType(BuiltList, const [const FullType({{returnBaseType}})]);
103+
final BuiltList<{{returnBaseType}}> dataList = _serializers.deserialize(response.data is String ? jsonDecode(response.data) : response.data, specifiedType: type);
104+
final data = dataList.toList();
105+
{{/isArray}}
106+
{{^isArray}}
107+
{{#isMap}}
108+
final serializer = _serializers.serializerForType(Map);
109+
{{/isMap}}
110+
{{^isMap}}
111+
final serializer = _serializers.serializerForType({{{returnType}}});
112+
{{/isMap}}
113+
final data = _serializers.deserializeWith<{{{returnType}}}>(serializer, response.data is String ? jsonDecode(response.data) : response.data);
114+
{{/isArray}}
110115
{{/isResponseFile}}
111116

112117
return Response<{{{returnType}}}>(
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import 'package:{{pubName}}/api.dart';
2+
import 'package:{{pubName}}/api/{{classFilename}}.dart';
3+
import 'package:test/test.dart';
4+
5+
{{#operations}}
6+
7+
/// tests for {{{classname}}}
8+
void main() {
9+
final instance = {{{clientName}}}().get{{{classname}}}();
10+
11+
group({{{classname}}}, () {
12+
{{#operation}}
13+
{{#summary}}
14+
// {{{.}}}
15+
//
16+
{{/summary}}
17+
{{#notes}}
18+
// {{{.}}}
19+
//
20+
{{/notes}}
21+
//{{#returnType}}Future<{{{returnType}}}> {{/returnType}}{{^returnType}}Future {{/returnType}}{{{operationId}}}({{#allParams}}{{#required}}{{{dataType}}} {{{paramName}}}{{^-last}}, {{/-last}}{{/required}}{{/allParams}}{{#hasOptionalParams}}{ {{#allParams}}{{^required}}{{{dataType}}} {{{paramName}}}{{^-last}}, {{/-last}}{{/required}}{{/allParams}} }{{/hasOptionalParams}}) async
22+
test('test {{{operationId}}}', () async {
23+
// TODO
24+
});
25+
26+
{{/operation}}
27+
});
28+
}
29+
{{/operations}}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{{#models}}
2+
{{#model}}
3+
import 'package:{{pubName}}/model/{{classFilename}}.dart';
4+
import 'package:test/test.dart';
5+
6+
// tests for {{{classname}}}
7+
void main() {
8+
{{^isEnum}}
9+
final instance = {{{classname}}}();
10+
{{/isEnum}}
11+
12+
group({{{classname}}}, () {
13+
{{#vars}}
14+
{{#description}}
15+
// {{{description}}}
16+
{{/description}}
17+
// {{{dataType}}} {{{name}}}{{#defaultValue}} (default value: {{{.}}}){{/defaultValue}}
18+
test('to test the property `{{{name}}}`', () async {
19+
// TODO
20+
});
21+
22+
{{/vars}}
23+
24+
});
25+
26+
}
27+
{{/model}}
28+
{{/models}}

modules/openapi-generator/src/main/resources/dart-dio/pubspec.mustache

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ dependencies:
1313
dev_dependencies:
1414
built_value_generator: ^7.1.0
1515
build_runner: ^1.7.1
16+
test: ^1.3.0
1617

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,6 +1350,7 @@
13501350
<module>samples/server/petstore/go-api-server</module>
13511351
<module>samples/server/petstore/go-gin-api-server</module>
13521352
<module>samples/client/petstore/dart2/petstore</module>
1353+
<module>samples/client/petstore/dart-dio</module>
13531354
<module>samples/client/petstore/dart-jaguar/openapi</module>
13541355
<module>samples/client/petstore/dart-jaguar/flutter_petstore/openapi</module>
13551356
</modules>

samples/client/petstore/dart-dio/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ doc/api/
2727
# (Library packages only! Remove pattern if developing an application package)
2828
pubspec.lock
2929

30+
# Don't commit *.g.dart files which are genrated during integration tests
31+
lib/**/*.g.dart
32+
3033
# Don’t commit files and directories created by other development environments.
3134
# For example, if your development environment creates any of the following files,
3235
# consider putting them in a global ignore file:

samples/client/petstore/dart-dio/.openapi-generator-ignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@
2121
#docs/*.md
2222
# Then explicitly reverse the ignore rule for a single file:
2323
#!docs/README.md
24+
25+
# Don't regenerate .gitignore as it contains customizations for generated dart files
26+
.gitignore

0 commit comments

Comments
 (0)