-
Notifications
You must be signed in to change notification settings - Fork 124
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
[BUG] fileName cannot be provided with List<int> partFile #605
Comments
No, it's not. FileName cannot be provided with The The generated code does not include a parameter to send the Looking at the code in chopper's /// Convert this [Request] to a [http.MultipartRequest]
@visibleForTesting
Future<http.MultipartRequest> toMultipartRequest() async {
final http.MultipartRequest request = http.MultipartRequest(method, url)
..headers.addAll(headers);
for (final PartValue part in parts) {
if (part.value == null) continue;
if (part.value is http.MultipartFile) {
request.files.add(part.value);
} else if (part.value is Iterable<http.MultipartFile>) {
request.files.addAll(part.value);
} else if (part is PartValueFile) {
if (part.value is List<int>) {
request.files.add(
http.MultipartFile.fromBytes(part.name, part.value),
);
} else if (part.value is String) {
request.files.add(
await http.MultipartFile.fromPath(part.name, part.value),
);
} else {
throw ArgumentError(
'Type ${part.value.runtimeType} is not a supported type for PartFile'
'Please use one of the following types'
' - List<int>'
' - String (path of your file) '
' - MultipartFile (from package:http)',
);
}
} else {
request.fields[part.name] = part.value.toString();
}
}
return request;
} When the type is request.files.add(
http.MultipartFile.fromBytes(part.name, part.value),
); Note that request.files.add(
http.MultipartFile.fromBytes(
part.name,
part.value,
fileName: "FileName.png",
contentType: "image/png",
),
); I manually changed the generated code to use |
I ran into this issue as well. Based on lejard-h/chopper#160 (comment), if the generated chopper client code uses Without these, our backend .Net Core web API was receiving In the meantime, I wrote an extension method that manually uses MultipartFile, to avoid overwriting the generated code. Something like this // Represents a file with byte data
class FileReference {
FileReference({
required this.value,
required this.contentType,
required this.filename,
});
final List<int> value;
final String contentType;
final String filename;
}
extension MyApiExtensions on MyApi {
Future<Response<dynamic>> apiV1UploadFixed({
FileReference? file,
String? description,
}) {
final Uri $url = Uri.parse('/api/v1/upload');
final List<PartValue> $parts = <PartValue>[
PartValue<String?>(
'description',
description,
),
PartValue<MultipartFile?>(
'file',
file != null
? MultipartFile.fromBytes(
'file',
file.value,
filename: file.filename,
contentType: MediaType.parse(file.contentType),
)
: null,
),
];
final Request $request = Request(
'POST',
$url,
client.baseUrl,
parts: $parts,
multipart: true,
);
return client.send<dynamic, dynamic>($request);
}
} Would be nice to have this fixed tho. It's a breaking change if all |
Hello, Then, by creating a build.yml file as explained in the documentation with the following content:
The generated client is correct and I'm able to assign a file
The file is correctly sent to the API, including the filename. I hope it helps! 🤞 Have a nice day! |
Describe the bug
For more information: lejard-h/chopper#408
I have a .NET Core WebApi with an action to upload files:
This action expects a
IFormFile
and requires the FileName and ContentType of the file to work.But the generator uses
List<int>?
parameter instead ofMultipartFile
so I can't send theFileName
during the request.How can I configure the generator to use
MultipartFile
instead ofList<int>
?To Reproduce
swagger.yaml
lib/generated_code/swagger_api.swagger.chopper.dart
Expected behavior
I want the generated code to be like this:
Library version used:
The text was updated successfully, but these errors were encountered: