Skip to content

[dart-dio] Adds support for multipart form data post body #4797

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

Merged
merged 2 commits into from
Jan 2, 2020
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 @@ -197,6 +197,8 @@ public void processOpts() {
supportingFiles.add(new SupportingFile("pubspec.mustache", "", "pubspec.yaml"));
supportingFiles.add(new SupportingFile("analysis_options.mustache", "", "analysis_options.yaml"));
supportingFiles.add(new SupportingFile("apilib.mustache", libFolder, "api.dart"));
supportingFiles.add(new SupportingFile("api_util.mustache", libFolder, "api_util.dart"));

supportingFiles.add(new SupportingFile("serializers.mustache", libFolder, "serializers.dart"));

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

if (op.getHasFormParams()) {
fullImports.add("package:" + pubName + "/api_util.dart");
}

Set<String> imports = new HashSet<>();
for (String item : op.imports) {
if (!modelToIgnore.contains(item.toLowerCase(Locale.ROOT))) {
Expand All @@ -314,6 +320,7 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
}
modelImports.addAll(imports);
op.imports = imports;

}

objs.put("modelImports", modelImports);
Expand Down
39 changes: 27 additions & 12 deletions modules/openapi-generator/src/main/resources/dart-dio/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,39 @@ class {{classname}} {

String path = "{{{path}}}"{{#pathParams}}.replaceAll("{" + "{{baseName}}" + "}", {{{paramName}}}.toString()){{/pathParams}};

// query params
Map<String, dynamic> queryParams = {};
Map<String, String> headerParams = Map.from(headers ?? {});
Map<String, String> formParams = {};
Map<String, dynamic> queryParams = {};
Map<String, String> headerParams = Map.from(headers ?? {});
dynamic bodyData;

{{#headerParams}}
headerParams["{{baseName}}"] = {{paramName}};
{{/headerParams}}
{{#queryParams}}
queryParams["{{baseName}}"] = {{paramName}};
{{/queryParams}}
queryParams.removeWhere((key, value) => value == null);
headerParams.removeWhere((key, value) => value == null);
formParams.removeWhere((key, value) => value == null);
queryParams.removeWhere((key, value) => value == null);
headerParams.removeWhere((key, value) => value == null);

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

{{#hasFormParams}}
Map<String, dynamic> formData = {};
{{#formParams}}
{{#isMultipart}}
{{^isFile}}
if ({{paramName}} != null) {
formData['{{baseName}}'] = parameterToString({{paramName}});
}
{{/isFile}}
{{#isFile}}
if ({{paramName}} != null) {
formData['{{baseName}}'] = MultipartFile.fromBytes({{paramName}}, filename: "{{baseName}}");
}
{{/isFile}}
{{/isMultipart}}
{{/formParams}}
bodyData = FormData.fromMap(formData);
{{/hasFormParams}}

{{#bodyParam}}
{{#isListContainer}}
Expand All @@ -52,14 +68,13 @@ class {{classname}} {
var serializedBody = _serializers.serialize({{paramName}});
{{/isListContainer}}
var json{{paramName}} = json.encode(serializedBody);
bodyData = json{{paramName}};
{{/bodyParam}}

return _dio.request(
path,
queryParameters: queryParams,
{{#bodyParam}}
data: json{{paramName}},
{{/bodyParam}}
data: bodyData,
options: Options(
method: '{{httpMethod}}'.toUpperCase(),
headers: headerParams,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/// Format the given parameter object into string.
String parameterToString(dynamic value) {
if (value == null) {
return '';
} else if (value is DateTime) {
return value.toUtc().toIso8601String();
{{#models}}
{{#model}}
{{#isEnum}}
} else if (value is {{classname}}) {
return {{classname}}TypeTransformer().encode(value).toString();
{{/isEnum}}
{{/model}}
{{/models}}
} else {
return value.toString();
}
}