Skip to content

Commit 5f767f6

Browse files
npetersolegz
authored andcommitted
check null before use this.content
Resolves #1192 Resolves #1193
1 parent 3930cad commit 5f767f6

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

spring-cloud-function-adapters/spring-cloud-function-serverless-web/src/main/java/org/springframework/cloud/function/serverless/web/ServerlessHttpServletRequest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public class ServerlessHttpServletRequest implements HttpServletRequest {
7777

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

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

284285
@Override
285286
public ServletInputStream getInputStream() {
286-
InputStream stream = new ByteArrayInputStream(this.content);
287+
288+
InputStream stream;
289+
if (this.content == null) {
290+
stream = EMPTY_INPUT_STREAM;
291+
}
292+
else {
293+
stream = new ByteArrayInputStream(this.content);
294+
}
295+
287296
return new ServletInputStream() {
288297

289298
boolean finished = false;

spring-cloud-function-adapters/spring-cloud-function-serverless-web/src/test/java/org/springframework/cloud/function/serverless/web/RequestResponseTests.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,21 @@ public void validatePostWithBody() throws Exception {
149149
assertThat(pet.getName()).isNotEmpty();
150150
}
151151

152+
@Test
153+
public void validatePostWithoutBody() throws Exception {
154+
ServerlessHttpServletRequest request = new ServerlessHttpServletRequest(null, "POST", "/pets/");
155+
request.setContentType("application/json");
156+
ServerlessHttpServletResponse response = new ServerlessHttpServletResponse();
157+
try {
158+
mvc.service(request, response);
159+
}
160+
catch (jakarta.servlet.ServletException e) {
161+
assertThat(e.getCause()).isNotInstanceOf(NullPointerException.class);
162+
}
163+
164+
assertThat(response.getStatus()).isEqualTo(400); // application fail because the pet is empty ;)
165+
}
166+
152167
@Test
153168
public void validatePostAsyncWithBody() throws Exception {
154169
// System.setProperty("spring.main.banner-mode", "off");

0 commit comments

Comments
 (0)