Skip to content

Commit

Permalink
fix null handling of MultipartFile (#422)
Browse files Browse the repository at this point in the history
  • Loading branch information
mj-hd authored Nov 20, 2021
1 parent 3c8a66e commit 1f51dd0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
8 changes: 7 additions & 1 deletion generator/lib/src/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1466,14 +1466,20 @@ You should create a new class to encapsulate the response.
]).statement);
} else if (innerType != null &&
_typeChecker(MultipartFile).isExactlyType(innerType)) {
if (p.type.isNullable) {
blocks.add(Code("if (${p.displayName} != null) {"));
}
blocks
.add(refer(_dataVar).property('files').property("addAll").call([
refer('''
${p.displayName}?.map((i) => MapEntry(
${p.displayName}.map((i) => MapEntry(
'${fieldName}',
i))
''')
]).statement);
if (p.type.isNullable) {
blocks.add(Code("}"));
}
} else if (innerType?.element is ClassElement) {
final ele = innerType!.element as ClassElement;
if (_missingToJson(ele)) {
Expand Down
28 changes: 28 additions & 0 deletions generator/test/src/generator_test_src.dart
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,34 @@ abstract class FilePartWithCustomNameTest {
@Part(name: 'image', fileName: 'my_profile_image.jpg') File image);
}

@ShouldGenerate(
r'''
final _data = FormData();
_data.files.addAll(images.map((i) => MapEntry('images', i)));
''',
contains: true,
)
@RestApi(baseUrl: "https://httpbin.org/")
abstract class FilePartWithMultipartListTest {
@POST("/profile")
Future<String> setProfile(@Part() List<MultipartFile> images);
}

@ShouldGenerate(
r'''
final _data = FormData();
if (images != null) {
_data.files.addAll(images.map((i) => MapEntry('images', i)));
}
''',
contains: true,
)
@RestApi(baseUrl: "https://httpbin.org/")
abstract class FilePartWithNullableMultipartListTest {
@POST("/profile")
Future<String> setProfile(@Part() List<MultipartFile>? images);
}

@ShouldGenerate(
r'''
final _data = FormData();
Expand Down

0 comments on commit 1f51dd0

Please sign in to comment.