fix(apigateway): take the last value of a repeated query string parameter (#3548)

A REST proxy event built queryStringParameters from the FIRST value of a
repeated parameter, so ?x=1&x=2&x=3 reached the integration as "1".

Captured against a real REST API in us-west-2 with a Lambda proxy
integration: AWS collapses a repeated parameter to the LAST value,
yielding {"x":"3"}, while multiValueQueryStringParameters keeps every
value in order as ["1","2","3"]. A trailing empty value still wins, so
?x=1&x= yields "".

This is the same rule putSingleValueHeaders already applies to duplicate
request headers one method above, where the comment notes that taking the
first value diverged from AWS. Query strings were left on the old
behaviour; they now match.

Also pins two properties that were already correct, so they stay that
way: a bracketed name such as filter[status]=open reaches the integration
unchanged, and an absent query string nulls both maps.

putQueryStringParameters and putMultiValueQueryStringParameters become
package-private for the unit test, matching putSingleValueHeaders and
putMultiValueHeaders next to them.

HTTP APIs are untouched. Their payload formats join duplicates with a
comma rather than taking one value, which is a different rule and is not
measured here.
This commit is contained in:
Ovidiu (Ovi) authored and GitHub committed 2026-09-13 18:41:22 -04:00
1 parent bfe2b71587
commit e8edc3d4b1
3 files changed
+181 -3

No files matched your search

@@ -11,6 +11,7 @@ import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MultivaluedHashMap;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.UriInfo;
import org.junit.jupiter.api.Test;
import java.nio.charset.StandardCharsets;
@@ -137,6 +138,84 @@ class ApiGatewayExecuteControllerTest {
event.path("multiValueHeaders").path("X-Dup"));
}
@Test
void duplicateQueryParamUsesLastSingleValueAndPreservesAllMultiValues() {
// Measured against a real REST API (us-west-2, Lambda proxy integration):
// ?x=1&x=2&x=3 yields queryStringParameters {"x":"3"} and
// multiValueQueryStringParameters {"x":["1","2","3"]}, matching how AWS
// collapses duplicate request headers.
MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();
queryParams.add("x", "1");
queryParams.add("x", "2");
queryParams.add("x", "3");
UriInfo uriInfo = mock(UriInfo.class);
when(uriInfo.getQueryParameters()).thenReturn(queryParams);
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode event = objectMapper.createObjectNode();
ApiGatewayExecuteController controller = controller(objectMapper);
controller.putQueryStringParameters(event, uriInfo);
controller.putMultiValueQueryStringParameters(event, uriInfo);
assertEquals("3", event.path("queryStringParameters").path("x").asText());
assertEquals(
objectMapper.valueToTree(List.of("1", "2", "3")),
event.path("multiValueQueryStringParameters").path("x"));
}
@Test
void repeatedQueryParamEndingEmptyKeepsTheTrailingEmptyValue() {
// ?x=1&x= yields {"x":""}: the last value wins even when it is empty.
MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();
queryParams.add("x", "1");
queryParams.add("x", "");
UriInfo uriInfo = mock(UriInfo.class);
when(uriInfo.getQueryParameters()).thenReturn(queryParams);
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode event = objectMapper.createObjectNode();
ApiGatewayExecuteController controller = controller(objectMapper);
controller.putQueryStringParameters(event, uriInfo);
assertEquals("", event.path("queryStringParameters").path("x").asText());
}
@Test
void bracketedQueryParamNameSurvivesUnchanged() {
// JSON:API style filter[status]=open reaches the integration with the brackets intact;
// AWS does not rewrite or drop the name. Pinned so the parameter map stays a passthrough.
MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();
queryParams.add("filter[status]", "open");
UriInfo uriInfo = mock(UriInfo.class);
when(uriInfo.getQueryParameters()).thenReturn(queryParams);
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode event = objectMapper.createObjectNode();
ApiGatewayExecuteController controller = controller(objectMapper);
controller.putQueryStringParameters(event, uriInfo);
controller.putMultiValueQueryStringParameters(event, uriInfo);
assertEquals("open", event.path("queryStringParameters").path("filter[status]").asText());
assertEquals(
objectMapper.valueToTree(List.of("open")),
event.path("multiValueQueryStringParameters").path("filter[status]"));
}
@Test
void absentQueryStringYieldsExplicitNulls() {
UriInfo uriInfo = mock(UriInfo.class);
when(uriInfo.getQueryParameters()).thenReturn(new MultivaluedHashMap<>());
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode event = objectMapper.createObjectNode();
ApiGatewayExecuteController controller = controller(objectMapper);
controller.putQueryStringParameters(event, uriInfo);
controller.putMultiValueQueryStringParameters(event, uriInfo);
assertTrue(event.path("queryStringParameters").isNull());
assertTrue(event.path("multiValueQueryStringParameters").isNull());
}
@Test
void convertsAuthorizerValuesToAwsVtlStringShape() {
Map<String, Object> result = ApiGatewayExecuteController.vtlAuthorizerContext(