From 817664191be456f53fd92e8c0a4e4b481ecae3ac Mon Sep 17 00:00:00 2001 From: Dennis Kieselhorst Date: Wed, 29 Jan 2025 14:18:04 +0100 Subject: [PATCH] fix: Request Parameters not picked up for v1 payload format with multivalue headers enabled (#1256) --- .../spring/AwsSpringHttpProcessingUtils.java | 12 +++-- .../AwsSpringHttpProcessingUtilsTests.java | 47 +++++++++++++++---- 2 files changed, 47 insertions(+), 12 deletions(-) diff --git a/aws-serverless-java-container-springboot3/src/main/java/com/amazonaws/serverless/proxy/spring/AwsSpringHttpProcessingUtils.java b/aws-serverless-java-container-springboot3/src/main/java/com/amazonaws/serverless/proxy/spring/AwsSpringHttpProcessingUtils.java index 2f547a32d..abcc56b26 100644 --- a/aws-serverless-java-container-springboot3/src/main/java/com/amazonaws/serverless/proxy/spring/AwsSpringHttpProcessingUtils.java +++ b/aws-serverless-java-container-springboot3/src/main/java/com/amazonaws/serverless/proxy/spring/AwsSpringHttpProcessingUtils.java @@ -5,10 +5,12 @@ import java.nio.charset.StandardCharsets; import java.nio.charset.UnsupportedCharsetException; import java.util.Base64; +import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; +import java.util.function.BiConsumer; import com.amazonaws.serverless.proxy.internal.HttpUtils; import org.apache.commons.io.Charsets; @@ -118,7 +120,11 @@ private static HttpServletRequest generateRequest1(String request, Context lambd ServerlessHttpServletRequest httpRequest = new ServerlessHttpServletRequest(servletContext, v1Request.getHttpMethod(), v1Request.getPath()); - populateQueryStringparameters(v1Request.getQueryStringParameters(), httpRequest); + populateQueryStringParameters(v1Request.getQueryStringParameters(), httpRequest); + if (v1Request.getMultiValueQueryStringParameters() != null) { + MultiValueMapAdapter queryStringParameters = new MultiValueMapAdapter(v1Request.getMultiValueQueryStringParameters()); + queryStringParameters.forEach((k, v) -> httpRequest.setParameter(k, StringUtils.collectionToCommaDelimitedString(v))); + } if (v1Request.getMultiValueHeaders() != null) { MultiValueMapAdapter headers = new MultiValueMapAdapter(v1Request.getMultiValueHeaders()); @@ -152,7 +158,7 @@ private static HttpServletRequest generateRequest2(String request, Context lambd ServerlessHttpServletRequest httpRequest = new ServerlessHttpServletRequest(servletContext, v2Request.getRequestContext().getHttp().getMethod(), v2Request.getRequestContext().getHttp().getPath()); - populateQueryStringparameters(v2Request.getQueryStringParameters(), httpRequest); + populateQueryStringParameters(v2Request.getQueryStringParameters(), httpRequest); v2Request.getHeaders().forEach(httpRequest::setHeader); @@ -172,7 +178,7 @@ private static HttpServletRequest generateRequest2(String request, Context lambd return httpRequest; } - private static void populateQueryStringparameters(Map requestParameters, ServerlessHttpServletRequest httpRequest) { + private static void populateQueryStringParameters(Map requestParameters, ServerlessHttpServletRequest httpRequest) { if (!CollectionUtils.isEmpty(requestParameters)) { for (Entry entry : requestParameters.entrySet()) { httpRequest.setParameter(entry.getKey(), entry.getValue()); diff --git a/aws-serverless-java-container-springboot3/src/test/java/com/amazonaws/serverless/proxy/spring/AwsSpringHttpProcessingUtilsTests.java b/aws-serverless-java-container-springboot3/src/test/java/com/amazonaws/serverless/proxy/spring/AwsSpringHttpProcessingUtilsTests.java index a04361e7e..e3c91acf0 100644 --- a/aws-serverless-java-container-springboot3/src/test/java/com/amazonaws/serverless/proxy/spring/AwsSpringHttpProcessingUtilsTests.java +++ b/aws-serverless-java-container-springboot3/src/test/java/com/amazonaws/serverless/proxy/spring/AwsSpringHttpProcessingUtilsTests.java @@ -79,15 +79,15 @@ public class AwsSpringHttpProcessingUtilsTests { + " },\n" + " \"queryStringParameters\": {\n" + " \"abc\": \"xyz\",\n" - + " \"foo\": \"baz\"\n" + + " \"parameter1\": \"value2\"\n" + " },\n" + " \"multiValueQueryStringParameters\": {\n" + " \"abc\": [\n" + " \"xyz\"\n" + " ],\n" - + " \"foo\": [\n" - + " \"bar\",\n" - + " \"baz\"\n" + + " \"parameter1\": [\n" + + " \"value1\",\n" + + " \"value2\"\n" + " ]\n" + " },\n" + " \"requestContext\": {\n" @@ -184,10 +184,35 @@ public class AwsSpringHttpProcessingUtilsTests { " }\n" + "}"; + private static final String ALB_EVENT = "{\n" + + " \"requestContext\": {\n" + + " \"elb\": {\n" + + " \"targetGroupArn\": \"arn:aws:elasticloadbalancing:region:123456789012:targetgroup/my-target-group/6d0ecf831eec9f09\"\n" + + " }\n" + + " },\n" + + " \"httpMethod\": \"POST\",\n" + + " \"path\": \"/async\",\n" + + " \"multiValueQueryStringParameters\": { \"parameter1\": [\"value1\", \"value2\"]},\n" + + " \"multiValueHeaders\": {\n" + + " \"accept\": [\"text/html,application/xhtml+xml\"],\n" + + " \"accept-language\": [\"en-US,en;q=0.8\"],\n" + + " \"content-type\": [\"text/plain\"],\n" + + " \"cookie\": [\"cookies\"],\n" + + " \"host\": [\"lambda-846800462-us-east-2.elb.amazonaws.com\"],\n" + + " \"User-Agent\": [\"curl/7.79.1\"],\n" + + " \"x-amzn-trace-id\": [\"Root=1-5bdb40ca-556d8b0c50dc66f0511bf520\"],\n" + + " \"x-forwarded-for\": [\"72.21.198.66\"],\n" + + " \"x-forwarded-port\": [\"443\"],\n" + + " \"x-forwarded-proto\": [\"https\"]\n" + + " },\n" + + " \"isBase64Encoded\": false,\n" + + " \"body\": \"request_body\"\n" + + "}"; + private final ObjectMapper mapper = new ObjectMapper(); public static Collection data() { - return Arrays.asList(new String[]{API_GATEWAY_EVENT, API_GATEWAY_EVENT_V2}); + return Arrays.asList(new String[]{API_GATEWAY_EVENT, API_GATEWAY_EVENT_V2, ALB_EVENT}); } @MethodSource("data") @@ -197,10 +222,17 @@ public void validateHttpServletRequestGenerationWithInputStream(String jsonEvent ServerlessServletContext servletContext = new ServerlessServletContext(); HttpServletRequest request = AwsSpringHttpProcessingUtils.generateHttpServletRequest(inputStream, null, servletContext, mapper); // spot check some headers + assertRequest(request); + } + + private static void assertRequest(HttpServletRequest request) { assertEquals("curl/7.79.1", request.getHeader("User-Agent")); assertEquals("443", request.getHeader("X-Forwarded-Port")); assertEquals("POST", request.getMethod()); assertEquals("/async", request.getRequestURI()); + assertEquals("value1,value2", request.getParameter("parameter1")); + // to be fixed: assertArrayEquals(new String[]{"value1","value2"}, request.getParameterValues("parameter1")); + } @MethodSource("data") @@ -209,10 +241,7 @@ public void validateHttpServletRequestGenerationWithJson(String jsonEvent) { ServerlessServletContext servletContext = new ServerlessServletContext(); HttpServletRequest request = AwsSpringHttpProcessingUtils.generateHttpServletRequest(jsonEvent, null, servletContext, mapper); // spot check some headers - assertEquals("curl/7.79.1", request.getHeader("User-Agent")); - assertEquals("443", request.getHeader("X-Forwarded-Port")); - assertEquals("POST", request.getMethod()); - assertEquals("/async", request.getRequestURI()); + assertRequest(request); } @MethodSource("data")