Skip to content

#1192 check null before use this.content #1193

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

Closed
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 @@ -77,6 +77,7 @@ public class ServerlessHttpServletRequest implements HttpServletRequest {

private static final BufferedReader EMPTY_BUFFERED_READER = new BufferedReader(new StringReader(""));

private static final InputStream EMPTY_INPUT_STREAM = new ByteArrayInputStream(new byte[0]);
/**
* Date formats as specified in the HTTP RFC.
*
Expand Down Expand Up @@ -283,7 +284,15 @@ public String getContentType() {

@Override
public ServletInputStream getInputStream() {
InputStream stream = new ByteArrayInputStream(this.content);

InputStream stream;
if (this.content == null) {
stream = EMPTY_INPUT_STREAM;
}
else {
stream = new ByteArrayInputStream(this.content);
}

return new ServletInputStream() {

boolean finished = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,21 @@ public void validatePostWithBody() throws Exception {
assertThat(pet.getName()).isNotEmpty();
}

@Test
public void validatePostWithoutBody() throws Exception {
ServerlessHttpServletRequest request = new ServerlessHttpServletRequest(null, "POST", "/pets/");
request.setContentType("application/json");
ServerlessHttpServletResponse response = new ServerlessHttpServletResponse();
try {
mvc.service(request, response);
}
catch (jakarta.servlet.ServletException e) {
assertThat(e.getCause()).isNotInstanceOf(NullPointerException.class);
}

assertThat(response.getStatus()).isEqualTo(400); // application fail because the pet is empty ;)
}

@Test
public void validatePostAsyncWithBody() throws Exception {
// System.setProperty("spring.main.banner-mode", "off");
Expand Down