Skip to content

Commit

Permalink
Resteasy Reactive: Handle null fields in multiparts
Browse files Browse the repository at this point in the history
When having null values in parts, these should be ignored. I confirmed that this behaviour is the same then in Resteasy Classic.

Fix #22847
  • Loading branch information
Sgitario committed Jan 13, 2022
1 parent a026614 commit d3e3ad5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
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

0 comments on commit d3e3ad5

Please sign in to comment.