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

Resteasy Reactive: Handle null fields in multiparts #22850

Merged
merged 1 commit into from
Jan 13, 2022
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 @@ -49,4 +49,14 @@ public MultipartOutputFileResponse complex() {
return response;
}

@GET
@Path("/with-null-fields")
@Produces(MediaType.MULTIPART_FORM_DATA)
public MultipartOutputFileResponse nullFields() {
MultipartOutputFileResponse response = new MultipartOutputFileResponse();
response.name = null;
response.file = null;
return response;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ public void testWithFiles() {
assertContainsFile(response, "file", MediaType.APPLICATION_OCTET_STREAM, "lorem.txt");
}

@Test
public void testWithNullFields() {
RestAssured
.given()
.get("/multipart/output/with-null-fields")
.then()
.contentType(ContentType.MULTIPART)
.log().all()
.statusCode(200); // should return 200 with no parts
}

private void assertContainsFile(String response, String name, String contentType, String fileName) {
String[] lines = response.split("--");
assertThat(lines).anyMatch(line -> line.contains(String.format(EXPECTED_CONTENT_DISPOSITION_FILE_PART, name, fileName))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,22 @@ private void write(List<PartItem> parts, String boundary, OutputStream outputStr
Charset charset = requestContext.getDeployment().getRuntimeConfiguration().body().defaultCharset();
String boundaryLine = "--" + boundary;
for (PartItem part : parts) {
// write boundary: --...
writeLine(outputStream, boundaryLine, charset);
// write content disposition header
writeLine(outputStream, HttpHeaders.CONTENT_DISPOSITION + ": form-data; name=\"" + part.getName() + "\""
+ getFileNameIfFile(part.getValue()), charset);
// write content content type
writeLine(outputStream, HttpHeaders.CONTENT_TYPE + ": " + part.getType(), charset);
// extra line
writeLine(outputStream, charset);

// write content
write(outputStream, serialiseEntity(part.getValue(), part.getType(), requestContext));
// extra line
writeLine(outputStream, charset);
if (part.getValue() != null) {
// write boundary: --...
writeLine(outputStream, boundaryLine, charset);
// write content disposition header
writeLine(outputStream, HttpHeaders.CONTENT_DISPOSITION + ": form-data; name=\"" + part.getName() + "\""
+ getFileNameIfFile(part.getValue()), charset);
// write content content type
writeLine(outputStream, HttpHeaders.CONTENT_TYPE + ": " + part.getType(), charset);
// extra line
writeLine(outputStream, charset);

// write content
write(outputStream, serialiseEntity(part.getValue(), part.getType(), requestContext));
// extra line
writeLine(outputStream, charset);
}
}

// write boundary: -- ... --
Expand Down