Skip to content

Commit a0ac34b

Browse files
committed
[dart-dio] Fixes issues around formData
1 parent ad2dd88 commit a0ac34b

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ public void processOpts() {
197197
supportingFiles.add(new SupportingFile("pubspec.mustache", "", "pubspec.yaml"));
198198
supportingFiles.add(new SupportingFile("analysis_options.mustache", "", "analysis_options.yaml"));
199199
supportingFiles.add(new SupportingFile("apilib.mustache", libFolder, "api.dart"));
200+
supportingFiles.add(new SupportingFile("api_util.mustache", libFolder, "api_util.dart"));
201+
200202
supportingFiles.add(new SupportingFile("serializers.mustache", libFolder, "serializers.dart"));
201203

202204
supportingFiles.add(new SupportingFile("gitignore.mustache", "", ".gitignore"));
@@ -304,6 +306,10 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
304306
op.vendorExtensions.put("isForm", isForm);
305307
op.vendorExtensions.put("isMultipart", isMultipart);
306308

309+
if (op.getHasFormParams()) {
310+
fullImports.add("package:" + pubName + "/api_util.dart");
311+
}
312+
307313
Set<String> imports = new HashSet<>();
308314
for (String item : op.imports) {
309315
if (!modelToIgnore.contains(item.toLowerCase(Locale.ROOT))) {
@@ -314,6 +320,7 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
314320
}
315321
modelImports.addAll(imports);
316322
op.imports = imports;
323+
317324
}
318325

319326
objs.put("modelImports", modelImports);

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ class {{classname}} {
4040

4141
List<String> contentTypes = [{{#consumes}}"{{{mediaType}}}"{{#hasMore}},{{/hasMore}}{{/consumes}}];
4242

43-
44-
{{#formParams}}
43+
{{#hasFormParams}}
4544
Map<String, dynamic> formData = {};
45+
{{#formParams}}
4646
{{#isMultipart}}
4747
{{^isFile}}
4848
if ({{paramName}} != null) {
@@ -55,8 +55,9 @@ class {{classname}} {
5555
}
5656
{{/isFile}}
5757
{{/isMultipart}}
58-
bodyData = FormData.fromMap(formData);
5958
{{/formParams}}
59+
bodyData = FormData.fromMap(formData);
60+
{{/hasFormParams}}
6061

6162
{{#bodyParam}}
6263
{{#isListContainer}}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/// Format the given parameter object into string.
2+
String parameterToString(dynamic value) {
3+
if (value == null) {
4+
return '';
5+
} else if (value is DateTime) {
6+
return value.toUtc().toIso8601String();
7+
{{#models}}
8+
{{#model}}
9+
{{#isEnum}}
10+
} else if (value is {{classname}}) {
11+
return {{classname}}TypeTransformer().encode(value).toString();
12+
{{/isEnum}}
13+
{{/model}}
14+
{{/models}}
15+
} else {
16+
return value.toString();
17+
}
18+
}

0 commit comments

Comments
 (0)