Skip to content

Commit

Permalink
[Java][retrofit2] Fix for Issue #7446 (#7659)
Browse files Browse the repository at this point in the history
* [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
  • Loading branch information
acabarbaye authored and wing328 committed Feb 22, 2018
1 parent b4827be commit d4543a9
Show file tree
Hide file tree
Showing 73 changed files with 130 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import retrofit2.http.*;

import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import okhttp3.MultipartBody;

{{#imports}}import {{import}};
{{/imports}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{{#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}}("{{baseName}}\"; filename=\"{{baseName}}") RequestBody {{/usePlayWS}}{{paramName}}{{/isFile}}{{/isFormParam}}
{{#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}} MultipartBody.Part {{paramName}}{{/isFile}}{{/isFormParam}}
4 changes: 2 additions & 2 deletions samples/client/petstore/java/retrofit2-play24/docs/UserApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ Get user by user name


UserApi apiInstance = new UserApi();
String username = "username_example"; // String | The name that needs to be fetched. Use user1 for testing.
String username = "username_example"; // String | The name that needs to be fetched. Use user1 for testing.
try {
User result = apiInstance.getUserByName(username);
System.out.println(result);
Expand All @@ -220,7 +220,7 @@ try {

Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**username** | **String**| The name that needs to be fetched. Use user1 for testing. |
**username** | **String**| The name that needs to be fetched. Use user1 for testing. |

### Return type

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ F.Promise<Response<Void>> updatePetWithForm(
@retrofit2.http.Multipart
@POST("pet/{petId}/uploadImage")
F.Promise<Response<ModelApiResponse>> uploadFile(
@retrofit2.http.Path("petId") Long petId, @retrofit2.http.Part("additionalMetadata") String additionalMetadata, @retrofit2.http.Part okhttp3.MultipartBody.Part file
@retrofit2.http.Path("petId") Long petId, @retrofit2.http.Part("additionalMetadata") String additionalMetadata, @retrofit2.http.Part MultipartBody.Part file
);

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ F.Promise<Response<Void>> deleteUser(
/**
* Get user by user name
*
* @param username The name that needs to be fetched. Use user1 for testing. (required)
* @param username The name that needs to be fetched. Use user1 for testing. (required)
* @return Call&lt;User&gt;
*/
@GET("user/{username}")
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.3.1-SNAPSHOT
2.4.0-SNAPSHOT
2 changes: 1 addition & 1 deletion samples/client/petstore/java/retrofit2-play25/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ ext {
retrofit_version = "2.3.0"
jackson_version = "2.7.8"
play_version = "2.5.14"
swagger_annotations_version = "1.5.15"
swagger_annotations_version = "1.5.17"
junit_version = "4.12"
threetenbp_version = "1.3.5"
json_fire_version = "1.8.0"
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/java/retrofit2-play25/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ lazy val root = (project in file(".")).
"com.fasterxml.jackson.core" % "jackson-annotations" % "2.7.8" % "compile",
"com.fasterxml.jackson.core" % "jackson-databind" % "2.7.8" % "compile",
"com.squareup.retrofit2" % "converter-jackson" % "2.3.0" % "compile",
"io.swagger" % "swagger-annotations" % "1.5.15" % "compile",
"io.swagger" % "swagger-annotations" % "1.5.17" % "compile",
"org.apache.oltu.oauth2" % "org.apache.oltu.oauth2.client" % "1.0.1" % "compile",
"org.threeten" % "threetenbp" % "1.3.5" % "compile",
"io.gsonfire" % "gson-fire" % "1.8.0" % "compile",
Expand Down
28 changes: 12 additions & 16 deletions samples/client/petstore/java/retrofit2-play25/docs/FakeApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ No authorization required

<a name="testEndpointParameters"></a>
# **testEndpointParameters**
> Void testEndpointParameters(number, _double, patternWithoutDelimiter, _byte, integer, int32, int64, _float, string, binary, date, dateTime, password, paramCallback)
> testEndpointParameters(number, _double, patternWithoutDelimiter, _byte, integer, int32, int64, _float, string, binary, date, dateTime, password, paramCallback)
Fake endpoint for testing various parameters 假端點 偽のエンドポイント 가짜 엔드 포인트

Expand Down Expand Up @@ -280,8 +280,7 @@ OffsetDateTime dateTime = new OffsetDateTime(); // OffsetDateTime | None
String password = "password_example"; // String | None
String paramCallback = "paramCallback_example"; // String | None
try {
Void result = apiInstance.testEndpointParameters(number, _double, patternWithoutDelimiter, _byte, integer, int32, int64, _float, string, binary, date, dateTime, password, paramCallback);
System.out.println(result);
apiInstance.testEndpointParameters(number, _double, patternWithoutDelimiter, _byte, integer, int32, int64, _float, string, binary, date, dateTime, password, paramCallback);
} catch (ApiException e) {
System.err.println("Exception when calling FakeApi#testEndpointParameters");
e.printStackTrace();
Expand Down Expand Up @@ -309,7 +308,7 @@ Name | Type | Description | Notes

### Return type

[**Void**](.md)
null (empty response body)

### Authorization

Expand All @@ -322,7 +321,7 @@ Name | Type | Description | Notes

<a name="testEnumParameters"></a>
# **testEnumParameters**
> Void testEnumParameters(enumFormStringArray, enumFormString, enumHeaderStringArray, enumHeaderString, enumQueryStringArray, enumQueryString, enumQueryInteger, enumQueryDouble)
> testEnumParameters(enumFormStringArray, enumFormString, enumHeaderStringArray, enumHeaderString, enumQueryStringArray, enumQueryString, enumQueryInteger, enumQueryDouble)
To test enum parameters

Expand All @@ -345,8 +344,7 @@ String enumQueryString = "-efg"; // String | Query parameter enum test (string)
Integer enumQueryInteger = 56; // Integer | Query parameter enum test (double)
Double enumQueryDouble = 3.4D; // Double | Query parameter enum test (double)
try {
Void result = apiInstance.testEnumParameters(enumFormStringArray, enumFormString, enumHeaderStringArray, enumHeaderString, enumQueryStringArray, enumQueryString, enumQueryInteger, enumQueryDouble);
System.out.println(result);
apiInstance.testEnumParameters(enumFormStringArray, enumFormString, enumHeaderStringArray, enumHeaderString, enumQueryStringArray, enumQueryString, enumQueryInteger, enumQueryDouble);
} catch (ApiException e) {
System.err.println("Exception when calling FakeApi#testEnumParameters");
e.printStackTrace();
Expand All @@ -368,7 +366,7 @@ Name | Type | Description | Notes

### Return type

[**Void**](.md)
null (empty response body)

### Authorization

Expand All @@ -381,7 +379,7 @@ No authorization required

<a name="testInlineAdditionalProperties"></a>
# **testInlineAdditionalProperties**
> Void testInlineAdditionalProperties(param)
> testInlineAdditionalProperties(param)
test inline additionalProperties

Expand All @@ -397,8 +395,7 @@ test inline additionalProperties
FakeApi apiInstance = new FakeApi();
Object param = null; // Object | request body
try {
Void result = apiInstance.testInlineAdditionalProperties(param);
System.out.println(result);
apiInstance.testInlineAdditionalProperties(param);
} catch (ApiException e) {
System.err.println("Exception when calling FakeApi#testInlineAdditionalProperties");
e.printStackTrace();
Expand All @@ -413,7 +410,7 @@ Name | Type | Description | Notes

### Return type

[**Void**](.md)
null (empty response body)

### Authorization

Expand All @@ -426,7 +423,7 @@ No authorization required

<a name="testJsonFormData"></a>
# **testJsonFormData**
> Void testJsonFormData(param, param2)
> testJsonFormData(param, param2)
test json serialization of form data

Expand All @@ -443,8 +440,7 @@ FakeApi apiInstance = new FakeApi();
String param = "param_example"; // String | field1
String param2 = "param2_example"; // String | field2
try {
Void result = apiInstance.testJsonFormData(param, param2);
System.out.println(result);
apiInstance.testJsonFormData(param, param2);
} catch (ApiException e) {
System.err.println("Exception when calling FakeApi#testJsonFormData");
e.printStackTrace();
Expand All @@ -460,7 +456,7 @@ Name | Type | Description | Notes

### Return type

[**Void**](.md)
null (empty response body)

### Authorization

Expand Down
28 changes: 12 additions & 16 deletions samples/client/petstore/java/retrofit2-play25/docs/PetApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Method | HTTP request | Description

<a name="addPet"></a>
# **addPet**
> Void addPet(body)
> addPet(body)
Add a new pet to the store

Expand All @@ -40,8 +40,7 @@ petstore_auth.setAccessToken("YOUR ACCESS TOKEN");
PetApi apiInstance = new PetApi();
Pet body = new Pet(); // Pet | Pet object that needs to be added to the store
try {
Void result = apiInstance.addPet(body);
System.out.println(result);
apiInstance.addPet(body);
} catch (ApiException e) {
System.err.println("Exception when calling PetApi#addPet");
e.printStackTrace();
Expand All @@ -56,7 +55,7 @@ Name | Type | Description | Notes

### Return type

[**Void**](.md)
null (empty response body)

### Authorization

Expand All @@ -69,7 +68,7 @@ Name | Type | Description | Notes

<a name="deletePet"></a>
# **deletePet**
> Void deletePet(petId, apiKey)
> deletePet(petId, apiKey)
Deletes a pet

Expand All @@ -94,8 +93,7 @@ PetApi apiInstance = new PetApi();
Long petId = 789L; // Long | Pet id to delete
String apiKey = "apiKey_example"; // String |
try {
Void result = apiInstance.deletePet(petId, apiKey);
System.out.println(result);
apiInstance.deletePet(petId, apiKey);
} catch (ApiException e) {
System.err.println("Exception when calling PetApi#deletePet");
e.printStackTrace();
Expand All @@ -111,7 +109,7 @@ Name | Type | Description | Notes

### Return type

[**Void**](.md)
null (empty response body)

### Authorization

Expand Down Expand Up @@ -285,7 +283,7 @@ Name | Type | Description | Notes

<a name="updatePet"></a>
# **updatePet**
> Void updatePet(body)
> updatePet(body)
Update an existing pet

Expand All @@ -309,8 +307,7 @@ petstore_auth.setAccessToken("YOUR ACCESS TOKEN");
PetApi apiInstance = new PetApi();
Pet body = new Pet(); // Pet | Pet object that needs to be added to the store
try {
Void result = apiInstance.updatePet(body);
System.out.println(result);
apiInstance.updatePet(body);
} catch (ApiException e) {
System.err.println("Exception when calling PetApi#updatePet");
e.printStackTrace();
Expand All @@ -325,7 +322,7 @@ Name | Type | Description | Notes

### Return type

[**Void**](.md)
null (empty response body)

### Authorization

Expand All @@ -338,7 +335,7 @@ Name | Type | Description | Notes

<a name="updatePetWithForm"></a>
# **updatePetWithForm**
> Void updatePetWithForm(petId, name, status)
> updatePetWithForm(petId, name, status)
Updates a pet in the store with form data

Expand All @@ -364,8 +361,7 @@ Long petId = 789L; // Long | ID of pet that needs to be updated
String name = "name_example"; // String | Updated name of the pet
String status = "status_example"; // String | Updated status of the pet
try {
Void result = apiInstance.updatePetWithForm(petId, name, status);
System.out.println(result);
apiInstance.updatePetWithForm(petId, name, status);
} catch (ApiException e) {
System.err.println("Exception when calling PetApi#updatePetWithForm");
e.printStackTrace();
Expand All @@ -382,7 +378,7 @@ Name | Type | Description | Notes

### Return type

[**Void**](.md)
null (empty response body)

### Authorization

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Method | HTTP request | Description

<a name="deleteOrder"></a>
# **deleteOrder**
> Void deleteOrder(orderId)
> deleteOrder(orderId)
Delete purchase order by ID

Expand All @@ -28,8 +28,7 @@ For valid response try integer IDs with value &lt; 1000. Anything above 1000 or
StoreApi apiInstance = new StoreApi();
String orderId = "orderId_example"; // String | ID of the order that needs to be deleted
try {
Void result = apiInstance.deleteOrder(orderId);
System.out.println(result);
apiInstance.deleteOrder(orderId);
} catch (ApiException e) {
System.err.println("Exception when calling StoreApi#deleteOrder");
e.printStackTrace();
Expand All @@ -44,7 +43,7 @@ Name | Type | Description | Notes

### Return type

[**Void**](.md)
null (empty response body)

### Authorization

Expand Down
Loading

0 comments on commit d4543a9

Please sign in to comment.