-
Notifications
You must be signed in to change notification settings - Fork 6k
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
[Java][retrofit2] file upload sets filename as baseName instead of a dynamic filename #7446
Comments
@acabarbaye Please file a PR so that we can review the fix more easily. |
@wing328 wrong github person, I think |
@canadaduane I'm sorry. Please kindly ignore. |
…lename as baseName instead of a dynamic filename. The solution is to use okhttp3.MultipartBody.Part instead of RequestBody in formParams template.
…lename as baseName instead of a dynamic filename. The solution is to use okhttp3.MultipartBody.Part instead of RequestBody in formParams template.
I created the following PR for requested change: #7659 |
* [Java][retrofit2] Fix for issue #7446: file upload sets filename as baseName instead of a dynamic filename. The solution is to use okhttp3.MultipartBody.Part instead of RequestBody in formParams template. * Changes corresponding to review comments * Petstore Samples * Fixed tests
Using swagger-codegen-cli 3.0.27 my generated API looks like
The "filename=" is still present. The templates in "handlebars" still contain the corresponding text for retrofit2. How could this be? Or doesn't the fix apply to version 3? |
Description
This is more a request to change formParams.mustache so that the filename can be set by the developer instead of defaulting to baseName.
In current template, the filename is set as follows:
okhttp3.MultipartBody.Part {{/usePlayWS}}{{^usePlayWS}}("{{baseName}}\"; filename=\"{{baseName}}") RequestBody
This way the filename is hardcoded and is the same for any file upload no matter what file is actually used. The reason for this is that the parameter is of RequestBody type. The solution to be able to make the filename in the request more dynamic is to use okhttp3.MultipartBody.Part instead as discussed there square/retrofit#1188. It is also described in the official documentation of retrofit2 https://futurestud.io/tutorials/retrofit-2-how-to-upload-files-to-server when it comes to file upload.
With such a change, when calling the endpoint the user would just need to do the following so that the filename is taken into account:
File dataFile;
String contentType;
final RequestBody body = RequestBody.create(contentType, dataFile.getFile());
return MultipartBody.Part.createFormData("datafile", dataFile.getFileName(), body);
Swagger-codegen version
Latest master: 2.3.1.
Suggest a fix/enhancement
I would thus propose the following change to formParams.mustache
{{#isFormParam}}{{#notFile}}{{#isMultipart}}@retrofit2.http.Part{{/isMultipart}}{{^isMultipart}}@retrofit2.http.Field{{/isMultipart}}("{{baseName}}") {{{dataType}}} {{paramName}}{{/notFile}}{{#isFile}}{{#isMultipart}}@retrofit2.http.Part{{/isMultipart}}{{^isMultipart}}@retrofit2.http.Field{{/isMultipart}}{{#usePlayWS}} okhttp3.MultipartBody.Part {{/usePlayWS}}{{^usePlayWS}}() MultipartBody.Part {{/usePlayWS}}{{paramName}}{{/isFile}}{{/isFormParam}}
This would also require
import okhttp3.MultipartBody;
to be added to api.mustacheThe text was updated successfully, but these errors were encountered: