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

Support passing filename to multipart form data output #30648

Merged
merged 1 commit into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
public class MultipartOutputResource {

public static final String RESPONSE_NAME = "a name";
public static final String RESPONSE_FILENAME = "a filename";
public static final String RESPONSE_SURNAME = "a surname";
public static final Status RESPONSE_STATUS = Status.WORKING;
public static final List<String> RESPONSE_VALUES = List.of("one", "two");
Expand Down Expand Up @@ -58,6 +59,7 @@ public RestResponse<MultipartOutputResponse> restResponse() {
public RestResponse<MultipartFormDataOutput> withFormDataOutput() {
MultipartFormDataOutput form = new MultipartFormDataOutput();
form.addFormData("name", RESPONSE_NAME, MediaType.TEXT_PLAIN_TYPE);
form.addFormData("part-with-filename", RESPONSE_FILENAME, MediaType.TEXT_PLAIN_TYPE, "file.txt");
form.addFormData("custom-surname", RESPONSE_SURNAME, MediaType.TEXT_PLAIN_TYPE);
form.addFormData("custom-status", RESPONSE_STATUS, MediaType.TEXT_PLAIN_TYPE)
.getHeaders().putSingle("extra-header", "extra-value");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public void testWithFormData() {

String body = extractable.asString();
assertContainsValue(body, "name", MediaType.TEXT_PLAIN, MultipartOutputResource.RESPONSE_NAME);
assertContainsValue(body, "part-with-filename", MediaType.TEXT_PLAIN, "filename=\"file.txt\"");
assertContainsValue(body, "custom-surname", MediaType.TEXT_PLAIN, MultipartOutputResource.RESPONSE_SURNAME);
assertContainsValue(body, "custom-status", MediaType.TEXT_PLAIN, MultipartOutputResource.RESPONSE_STATUS);
assertContainsValue(body, "active", MediaType.TEXT_PLAIN, MultipartOutputResource.RESPONSE_ACTIVE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ private String getFileNameIfFile(Object value, String partFileName) {
} else if (value instanceof FileDownload) {
return "; filename=\"" + ((FileDownload) value).fileName() + "\"";
} else if (partFileName != null) {
piu130 marked this conversation as resolved.
Show resolved Hide resolved
return partFileName;
return "; filename=\"" + partFileName + "\"";
}

return "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ public PartItem addFormData(String key, Object entity, MediaType mediaType) {

public PartItem addFormData(String key, Object entity, String genericType, MediaType mediaType) {
PartItem part = new PartItem(entity, genericType, mediaType);
return addFormData(key, part);
}

public PartItem addFormData(String key, Object entity, MediaType mediaType, String filename) {
PartItem part = new PartItem(entity, null, mediaType, filename);
return addFormData(key, part);
}

private PartItem addFormData(String key, PartItem part) {
parts.put(key, part);
return part;
}
Expand Down