From 6ea8ff3a17ca0efdc6fe50a4aa0213ae016ceb9c Mon Sep 17 00:00:00 2001 From: Alex Wood Date: Fri, 8 Sep 2023 03:07:49 -0400 Subject: [PATCH] [java-client][resteasy] fix multipart requests (#16517) This patch fixes two issues with the Resteasy generated client code. The first is the usage of a deprecated method, getFormData. The fix for this issue was originally conceived by @peter-seitz. The second issue was a problem in how the Content-Disposition header was being constructed. If we had a file named "test.txt" and were uploading it to a field named "myFile", the Content-Disposition header should look like Content-Disposition: form-data; name="myFile" Instead, the code was using the file's name (rather than the field name) in the name directive and the header looked like Content-Disposition: form-data; name="test.txt" The Content-Disposition header can take an optional directive, filename, but I have not included that here as that directive is mostly useful for file downloads and not uploads. --- .../resources/Java/libraries/resteasy/ApiClient.mustache | 8 +++++--- .../src/main/java/org/openapitools/client/ApiClient.java | 4 +++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/resteasy/ApiClient.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/resteasy/ApiClient.mustache index 45a49cdf819..07c079e8922 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/resteasy/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/resteasy/ApiClient.mustache @@ -33,6 +33,7 @@ import {{javaxPackage}}.ws.rs.client.Entity; import {{javaxPackage}}.ws.rs.client.Invocation; import {{javaxPackage}}.ws.rs.client.WebTarget; import {{javaxPackage}}.ws.rs.core.Form; +import {{javaxPackage}}.ws.rs.core.GenericEntity; import {{javaxPackage}}.ws.rs.core.GenericType; import {{javaxPackage}}.ws.rs.core.MediaType; import {{javaxPackage}}.ws.rs.core.Response; @@ -497,15 +498,16 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { if (param.getValue() instanceof File) { File file = (File) param.getValue(); try { - multipart.addFormData(param.getValue().toString(),new FileInputStream(file),MediaType.APPLICATION_OCTET_STREAM_TYPE); + multipart.addFormData(param.getKey(),new FileInputStream(file),MediaType.APPLICATION_OCTET_STREAM_TYPE); } catch (FileNotFoundException e) { throw new ApiException("Could not serialize multipart/form-data "+e.getMessage()); } } else { - multipart.addFormData(param.getValue().toString(),param.getValue().toString(),MediaType.APPLICATION_OCTET_STREAM_TYPE); + multipart.addFormData(param.getKey(),param.getValue().toString(),MediaType.APPLICATION_OCTET_STREAM_TYPE); } } - entity = Entity.entity(multipart.getFormData(), MediaType.MULTIPART_FORM_DATA_TYPE); + GenericEntity genericEntity = new GenericEntity(multipart) { }; + entity = Entity.entity(genericEntity, MediaType.MULTIPART_FORM_DATA_TYPE); } else if (contentType.startsWith("application/x-www-form-urlencoded")) { Form form = new Form(); for (Entry param: formParams.entrySet()) { diff --git a/samples/client/petstore/java/resteasy/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/resteasy/src/main/java/org/openapitools/client/ApiClient.java index 718adc4771c..2f1b1630926 100644 --- a/samples/client/petstore/java/resteasy/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/resteasy/src/main/java/org/openapitools/client/ApiClient.java @@ -31,6 +31,7 @@ import javax.ws.rs.client.Entity; import javax.ws.rs.client.Invocation; import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.Form; +import javax.ws.rs.core.GenericEntity; import javax.ws.rs.core.GenericType; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; @@ -500,7 +501,8 @@ public class ApiClient extends JavaTimeFormatter { multipart.addFormData(param.getValue().toString(),param.getValue().toString(),MediaType.APPLICATION_OCTET_STREAM_TYPE); } } - entity = Entity.entity(multipart.getFormData(), MediaType.MULTIPART_FORM_DATA_TYPE); + GenericEntity genericEntity = new GenericEntity(multipart) { }; + entity = Entity.entity(genericEntity, MediaType.MULTIPART_FORM_DATA_TYPE); } else if (contentType.startsWith("application/x-www-form-urlencoded")) { Form form = new Form(); for (Entry param: formParams.entrySet()) {