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

[Java][retrofit2] file upload sets filename as baseName instead of a dynamic filename #7446

Closed
acabarbaye opened this issue Jan 18, 2018 · 5 comments

Comments

@acabarbaye
Copy link
Contributor

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.mustache

@wing328
Copy link
Contributor

wing328 commented Jan 28, 2018

@acabarbaye Please file a PR so that we can review the fix more easily.

@canadaduane
Copy link
Contributor

@wing328 wrong github person, I think

@wing328
Copy link
Contributor

wing328 commented Jan 29, 2018

@canadaduane I'm sorry. Please kindly ignore.

acabarbaye added a commit to acabarbaye/swagger-codegen that referenced this issue Feb 14, 2018
…lename as baseName instead of a dynamic filename. The solution is to use okhttp3.MultipartBody.Part instead of RequestBody in formParams template.
acabarbaye added a commit to acabarbaye/swagger-codegen that referenced this issue Feb 14, 2018
…lename as baseName instead of a dynamic filename. The solution is to use okhttp3.MultipartBody.Part instead of RequestBody in formParams template.
@acabarbaye
Copy link
Contributor Author

I created the following PR for requested change: #7659

wing328 pushed a commit that referenced this issue Feb 22, 2018
* [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
@steffen-harbich-cognitum

Using swagger-codegen-cli 3.0.27 my generated API looks like

@retrofit2.http.Multipart
  @PUT("employee/{code}/photo")
  Call<Object> updatePhoto(
                        @retrofit2.http.Part("file\"; filename=\"file") RequestBody file,         @retrofit2.http.Path("code") String code            ,     @retrofit2.http.Query("lang") String lang                
  );

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants