[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.
This commit is contained in:
Alex Wood 2023-09-08 03:07:49 -04:00 committed by GitHub
parent 005566c6e3
commit 6ea8ff3a17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 4 deletions

View File

@ -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<MultipartFormDataOutput> genericEntity = new GenericEntity<MultipartFormDataOutput>(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<String, Object> param: formParams.entrySet()) {

View File

@ -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<MultipartFormDataOutput> genericEntity = new GenericEntity<MultipartFormDataOutput>(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<String, Object> param: formParams.entrySet()) {