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

[dart][dart-dio-next] Improve support for file uploads #9542

Merged
merged 6 commits into from
May 25, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 0 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ jobs:
- ~/.bundle
- ~/.go_workspace
- ~/.gradle
- ~/.pub-cache
- ~/.cache/bower
- ".git"
- ~/.stack
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class DartDioNextClientCodegen extends AbstractDartCodegen {
public static final String SERIALIZATION_LIBRARY_BUILT_VALUE = "built_value";
public static final String SERIALIZATION_LIBRARY_DEFAULT = SERIALIZATION_LIBRARY_BUILT_VALUE;

private static final String DIO_IMPORT = "package:dio/dio.dart";
kuhnroyal marked this conversation as resolved.
Show resolved Hide resolved
private static final String CLIENT_NAME = "clientName";

private String dateLibrary;
Expand Down Expand Up @@ -182,16 +183,16 @@ private void configureSerializationLibraryBuiltValue(String srcFolder) {
typeMapping.put("List", "BuiltList");
typeMapping.put("set", "BuiltSet");
typeMapping.put("map", "BuiltMap");
typeMapping.put("file", "Uint8List");
typeMapping.put("binary", "Uint8List");
typeMapping.put("file", "MultipartFile");
kuhnroyal marked this conversation as resolved.
Show resolved Hide resolved
typeMapping.put("binary", "MultipartFile");
typeMapping.put("object", "JsonObject");
typeMapping.put("AnyType", "JsonObject");

imports.put("BuiltList", "package:built_collection/built_collection.dart");
imports.put("BuiltSet", "package:built_collection/built_collection.dart");
imports.put("BuiltMap", "package:built_collection/built_collection.dart");
imports.put("JsonObject", "package:built_value/json_object.dart");
imports.put("Uint8List", "dart:typed_data");
imports.put("MultipartFile", DIO_IMPORT);
}

private void configureDateLibrary(String srcFolder) {
Expand Down Expand Up @@ -257,7 +258,7 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
for (Object _mo : models) {
Map<String, Object> mo = (Map<String, Object>) _mo;
CodegenModel cm = (CodegenModel) mo.get("model");
cm.imports = rewriteImports(cm.imports);
cm.imports = rewriteImports(cm.imports, true);
cm.vendorExtensions.put("x-has-vars", !cm.vars.isEmpty());
}
return objs;
Expand Down Expand Up @@ -302,7 +303,6 @@ private void appendBuiltValueCollection(StringBuilder sb, CodegenProperty proper
sb.append(")]");
}


@Override
public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> objs, List<Object> allModels) {
objs = super.postProcessOperationsWithModels(objs, allModels);
Expand Down Expand Up @@ -330,10 +330,6 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
}

for (CodegenParameter param : op.bodyParams) {
if (param.baseType != null && param.baseType.equalsIgnoreCase("Uint8List") && isMultipart) {
param.baseType = "MultipartFile";
param.dataType = "MultipartFile";
}
if (param.isContainer) {
final Map<String, Object> serializer = new HashMap<>();
serializer.put("isArray", param.isArray);
Expand All @@ -348,7 +344,7 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
op.vendorExtensions.put("x-is-form", isForm);
op.vendorExtensions.put("x-is-multipart", isMultipart);

resultImports.addAll(rewriteImports(op.imports));
resultImports.addAll(rewriteImports(op.imports, false));
if (op.getHasFormParams()) {
resultImports.add("package:" + pubName + "/src/api_util.dart");
}
Expand All @@ -369,11 +365,16 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
return objs;
}

private Set<String> rewriteImports(Set<String> originalImports) {
private Set<String> rewriteImports(Set<String> originalImports, boolean isModel) {
Set<String> resultImports = Sets.newHashSet();
for (String modelImport : originalImports) {
if (imports.containsKey(modelImport)) {
resultImports.add(imports.get(modelImport));
String i = imports.get(modelImport);
if (Objects.equals(i, DIO_IMPORT) && !isModel) {
// Don't add imports to operations that are already imported
continue;
}
resultImports.add(i);
} else {
resultImports.add("package:" + pubName + "/src/model/" + underscore(modelImport) + ".dart");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{{#hasFormParams}}
_bodyData = {{#isMultipart}}FormData.fromMap({{/isMultipart}}<String, dynamic>{
{{#formParams}}
{{^required}}{{^isNullable}}if ({{{paramName}}} != null) {{/isNullable}}{{/required}}r'{{{baseName}}}': {{#isFile}}MultipartFile.fromBytes({{{paramName}}}, filename: r'{{{baseName}}}'){{/isFile}}{{^isFile}}encodeFormParameter(_serializers, {{{paramName}}}, const FullType({{^isContainer}}{{{baseType}}}{{/isContainer}}{{#isContainer}}Built{{#isMap}}Map{{/isMap}}{{#isArray}}{{#uniqueItems}}Set{{/uniqueItems}}{{^uniqueItems}}List{{/uniqueItems}}{{/isArray}}, [{{#isMap}}FullType(String), {{/isMap}}FullType({{{baseType}}})]{{/isContainer}})){{/isFile}},
{{^required}}{{^isNullable}}if ({{{paramName}}} != null) {{/isNullable}}{{/required}}r'{{{baseName}}}': {{#isFile}}{{{paramName}}}{{/isFile}}{{^isFile}}encodeFormParameter(_serializers, {{{paramName}}}, const FullType({{^isContainer}}{{{baseType}}}{{/isContainer}}{{#isContainer}}Built{{#isMap}}Map{{/isMap}}{{#isArray}}{{#uniqueItems}}Set{{/uniqueItems}}{{^uniqueItems}}List{{/uniqueItems}}{{/isArray}}, [{{#isMap}}FullType(String), {{/isMap}}FullType({{{baseType}}})]{{/isContainer}})){{/isFile}},
{{/formParams}}
}{{#isMultipart}}){{/isMultipart}};
{{/hasFormParams}}
{{#bodyParam}}
{{#isPrimitiveType}}
_bodyData = {{paramName}};
_bodyData = {{paramName}}{{#isFile}}.finalize(){{/isFile}};
{{/isPrimitiveType}}
{{^isPrimitiveType}}
{{#isContainer}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ var int32 = 56; // int | None
var int64 = 789; // int | None
var float = 3.4; // double | None
var string = string_example; // String | None
var binary = BINARY_DATA_HERE; // Uint8List | None
var binary = BINARY_DATA_HERE; // MultipartFile | None
var date = 2013-10-20; // Date | None
var dateTime = 2013-10-20T19:20:30+01:00; // DateTime | None
var password = password_example; // String | None
Expand All @@ -503,7 +503,7 @@ Name | Type | Description | Notes
**int64** | **int**| None | [optional]
**float** | **double**| None | [optional]
**string** | **String**| None | [optional]
**binary** | **Uint8List**| None | [optional]
**binary** | **MultipartFile**| None | [optional]
**date** | **Date**| None | [optional]
**dateTime** | **DateTime**| None | [optional]
**password** | **String**| None | [optional]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Name | Type | Description | Notes
**decimal** | **double** | | [optional]
**string** | **String** | | [optional]
**byte** | **String** | |
**binary** | [**Uint8List**](Uint8List.md) | | [optional]
**binary** | [**MultipartFile**](MultipartFile.md) | | [optional]
**date** | [**Date**](Date.md) | |
**dateTime** | [**DateTime**](DateTime.md) | | [optional]
**uuid** | **String** | | [optional]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ import 'package:openapi/api.dart';
var api_instance = new PetApi();
var petId = 789; // int | ID of pet to update
var additionalMetadata = additionalMetadata_example; // String | Additional data to pass to server
var file = BINARY_DATA_HERE; // Uint8List | file to upload
var file = BINARY_DATA_HERE; // MultipartFile | file to upload
try {
var result = api_instance.uploadFile(petId, additionalMetadata, file);
Expand All @@ -361,7 +361,7 @@ Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**petId** | **int**| ID of pet to update |
**additionalMetadata** | **String**| Additional data to pass to server | [optional]
**file** | **Uint8List**| file to upload | [optional]
kuhnroyal marked this conversation as resolved.
Show resolved Hide resolved
**file** | **MultipartFile**| file to upload | [optional]

### Return type

Expand Down Expand Up @@ -391,7 +391,7 @@ import 'package:openapi/api.dart';
var api_instance = new PetApi();
var petId = 789; // int | ID of pet to update
var requiredFile = BINARY_DATA_HERE; // Uint8List | file to upload
var requiredFile = BINARY_DATA_HERE; // MultipartFile | file to upload
var additionalMetadata = additionalMetadata_example; // String | Additional data to pass to server
try {
Expand All @@ -407,7 +407,7 @@ try {
Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**petId** | **int**| ID of pet to update |
**requiredFile** | **Uint8List**| file to upload |
**requiredFile** | **MultipartFile**| file to upload |
**additionalMetadata** | **String**| Additional data to pass to server | [optional]

### Return type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import 'dart:async';
import 'package:built_value/serializer.dart';
import 'package:dio/dio.dart';

import 'dart:typed_data';
import 'package:built_collection/built_collection.dart';
import 'package:openapi/src/api_util.dart';
import 'package:openapi/src/model/date.dart';
Expand Down Expand Up @@ -826,7 +825,7 @@ class FakeApi {
int? int64,
double? float,
String? string,
Uint8List? binary,
MultipartFile? binary,
Date? date,
DateTime? dateTime,
String? password,
Expand Down Expand Up @@ -875,7 +874,7 @@ class FakeApi {
if (string != null) r'string': encodeFormParameter(_serializers, string, const FullType(String)),
r'pattern_without_delimiter': encodeFormParameter(_serializers, patternWithoutDelimiter, const FullType(String)),
r'byte': encodeFormParameter(_serializers, byte, const FullType(String)),
if (binary != null) r'binary': MultipartFile.fromBytes(binary, filename: r'binary'),
if (binary != null) r'binary': binary,
if (date != null) r'date': encodeFormParameter(_serializers, date, const FullType(Date)),
if (dateTime != null) r'dateTime': encodeFormParameter(_serializers, dateTime, const FullType(DateTime)),
if (password != null) r'password': encodeFormParameter(_serializers, password, const FullType(String)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import 'dart:async';
import 'package:built_value/serializer.dart';
import 'package:dio/dio.dart';

import 'dart:typed_data';
import 'package:built_collection/built_collection.dart';
import 'package:openapi/src/api_util.dart';
import 'package:openapi/src/model/api_response.dart';
Expand Down Expand Up @@ -513,7 +512,7 @@ class PetApi {
Future<Response<ApiResponse>> uploadFile({
required int petId,
String? additionalMetadata,
Uint8List? file,
MultipartFile? file,
CancelToken? cancelToken,
Map<String, dynamic>? headers,
Map<String, dynamic>? extra,
Expand Down Expand Up @@ -550,7 +549,7 @@ class PetApi {
try {
_bodyData = FormData.fromMap(<String, dynamic>{
if (additionalMetadata != null) r'additionalMetadata': encodeFormParameter(_serializers, additionalMetadata, const FullType(String)),
if (file != null) r'file': MultipartFile.fromBytes(file, filename: r'file'),
if (file != null) r'file': file,
});

} catch(error) {
Expand Down Expand Up @@ -610,7 +609,7 @@ class PetApi {
///
Future<Response<ApiResponse>> uploadFileWithRequiredFile({
required int petId,
required Uint8List requiredFile,
required MultipartFile requiredFile,
String? additionalMetadata,
CancelToken? cancelToken,
Map<String, dynamic>? headers,
Expand Down Expand Up @@ -648,7 +647,7 @@ class PetApi {
try {
_bodyData = FormData.fromMap(<String, dynamic>{
if (additionalMetadata != null) r'additionalMetadata': encodeFormParameter(_serializers, additionalMetadata, const FullType(String)),
r'requiredFile': MultipartFile.fromBytes(requiredFile, filename: r'requiredFile'),
r'requiredFile': requiredFile,
});

} catch(error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// AUTO-GENERATED FILE, DO NOT MODIFY!
//

import 'dart:typed_data';
import 'package:openapi/src/model/date.dart';
import 'package:dio/dio.dart';
import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';

Expand Down Expand Up @@ -40,7 +40,7 @@ abstract class FormatTest implements Built<FormatTest, FormatTestBuilder> {
String get byte;

@BuiltValueField(wireName: r'binary')
Uint8List? get binary;
MultipartFile? get binary;

@BuiltValueField(wireName: r'date')
Date get date;
Expand Down Expand Up @@ -137,7 +137,7 @@ class _$FormatTestSerializer implements StructuredSerializer<FormatTest> {
result
..add(r'binary')
..add(serializers.serialize(object.binary,
specifiedType: const FullType(Uint8List)));
specifiedType: const FullType(MultipartFile)));
}
result
..add(r'date')
Expand Down Expand Up @@ -223,7 +223,7 @@ class _$FormatTestSerializer implements StructuredSerializer<FormatTest> {
break;
case r'binary':
result.binary = serializers.deserialize(value,
specifiedType: const FullType(Uint8List)) as Uint8List;
specifiedType: const FullType(MultipartFile)) as MultipartFile;
break;
case r'date':
result.date = serializers.deserialize(value,
Expand Down