From 5f767f65e0848330cbd8e72e466e448d29c559b6 Mon Sep 17 00:00:00 2001 From: Nicolas PETERS Date: Mon, 21 Oct 2024 23:45:54 +0200 Subject: [PATCH] check null before use this.content Resolves #1192 Resolves #1193 --- .../web/ServerlessHttpServletRequest.java | 11 ++++++++++- .../serverless/web/RequestResponseTests.java | 15 +++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/spring-cloud-function-adapters/spring-cloud-function-serverless-web/src/main/java/org/springframework/cloud/function/serverless/web/ServerlessHttpServletRequest.java b/spring-cloud-function-adapters/spring-cloud-function-serverless-web/src/main/java/org/springframework/cloud/function/serverless/web/ServerlessHttpServletRequest.java index 77b135555..b84faa11f 100644 --- a/spring-cloud-function-adapters/spring-cloud-function-serverless-web/src/main/java/org/springframework/cloud/function/serverless/web/ServerlessHttpServletRequest.java +++ b/spring-cloud-function-adapters/spring-cloud-function-serverless-web/src/main/java/org/springframework/cloud/function/serverless/web/ServerlessHttpServletRequest.java @@ -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. * @@ -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; diff --git a/spring-cloud-function-adapters/spring-cloud-function-serverless-web/src/test/java/org/springframework/cloud/function/serverless/web/RequestResponseTests.java b/spring-cloud-function-adapters/spring-cloud-function-serverless-web/src/test/java/org/springframework/cloud/function/serverless/web/RequestResponseTests.java index 1cbf27cc7..d02d36539 100644 --- a/spring-cloud-function-adapters/spring-cloud-function-serverless-web/src/test/java/org/springframework/cloud/function/serverless/web/RequestResponseTests.java +++ b/spring-cloud-function-adapters/spring-cloud-function-serverless-web/src/test/java/org/springframework/cloud/function/serverless/web/RequestResponseTests.java @@ -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");