Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

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

Ovidiu (Ovi)e8edc3d4b177
diff --git a/compatibility-tests/sdk-test-node/tests/apigateway.test.ts b/compatibility-tests/sdk-test-node/tests/apigateway.test.ts
index fb215942ab..8779901547 100644
--- a/compatibility-tests/sdk-test-node/tests/apigateway.test.ts
+++ b/compatibility-tests/sdk-test-node/tests/apigateway.test.ts
@@ -746,6 +746,101 @@ describe('API Gateway v1 — execute-api data plane', () => {
});
});
+// ──────────────────────────── Query string parameter parsing ────────────────────────────
+
+/**
+ * How a REST API fills queryStringParameters / multiValueQueryStringParameters.
+ *
+ * Captured against a real REST API in us-west-2 with a Lambda proxy integration:
+ * ?x=1&x=2&x=3 yields {"x":"3"} and {"x":["1","2","3"]} — the LAST value wins in the
+ * single-value map, as it does for duplicate headers. A valueless parameter arrives as "",
+ * an absent query string makes both maps null, and a JSON:API style filter[status]=open
+ * keeps its brackets.
+ */
+describe('API Gateway v1 — query string parameters', () => {
+ let gw: APIGatewayClient;
+ let lambda: LambdaClient;
+ let apiId: string;
+ const echoFn = uniqueName('qs-echo');
+
+ beforeAll(async () => {
+ gw = makeClient(APIGatewayClient);
+ lambda = makeClient(LambdaClient);
+
+ await createLambda(lambda, echoFn, `
+ exports.handler = async (event) => ({
+ statusCode: 200,
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({
+ qsp: event.queryStringParameters,
+ mvqsp: event.multiValueQueryStringParameters
+ })
+ });
+ `);
+
+ const api = await gw.send(new CreateRestApiCommand({ name: uniqueName('qs-test') }));
+ apiId = api.id!;
+
+ const resources = await gw.send(new GetResourcesCommand({ restApiId: apiId }));
+ const rootId = resources.items![0].id!;
+
+ const echoRes = await apigwFetch(`/restapis/${apiId}/resources/${rootId}`, 'POST', { pathPart: 'echo' });
+ const echoId = (await echoRes.json() as { id: string }).id;
+
+ await apigwFetch(`/restapis/${apiId}/resources/${echoId}/methods/GET`, 'PUT', {
+ authorizationType: 'NONE',
+ });
+ await apigwFetch(`/restapis/${apiId}/resources/${echoId}/methods/GET/integration`, 'PUT', {
+ type: 'AWS_PROXY',
+ httpMethod: 'POST',
+ uri: `arn:aws:apigateway:${REGION}:lambda:path/2015-03-31/functions/arn:aws:lambda:${REGION}:${ACCOUNT}:function:${echoFn}/invocations`,
+ });
+
+ const depRes = await apigwFetch(`/restapis/${apiId}/deployments`, 'POST', { description: 'compat-test' });
+ const depId = (await depRes.json() as { id: string }).id;
+ await apigwFetch(`/restapis/${apiId}/stages`, 'POST', { stageName: 'prod', deploymentId: depId });
+ });
+
+ afterAll(async () => {
+ try { if (apiId) await gw.send(new DeleteRestApiCommand({ restApiId: apiId })); } catch { /* ignore */ }
+ await deleteLambda(lambda, echoFn);
+ });
+
+ async function echo(query: string): Promise<{ qsp: Record<string, string> | null; mvqsp: Record<string, string[]> | null }> {
+ const res = await executeApi(apiId, 'prod', '/echo', { query });
+ expect(res.status).toBe(200);
+ return res.json() as Promise<{ qsp: Record<string, string> | null; mvqsp: Record<string, string[]> | null }>;
+ }
+
+ it('should take the last value of a repeated parameter', async () => {
+ const { qsp, mvqsp } = await echo('x=1&x=2&x=3');
+ expect(qsp).toEqual({ x: '3' });
+ expect(mvqsp).toEqual({ x: ['1', '2', '3'] });
+ });
+
+ it('should keep a trailing empty value as the winner', async () => {
+ const { qsp } = await echo('x=1&x=');
+ expect(qsp).toEqual({ x: '' });
+ });
+
+ it('should represent a valueless parameter as an empty string', async () => {
+ const { qsp } = await echo('a&b=1');
+ expect(qsp).toEqual({ a: '', b: '1' });
+ });
+
+ it('should preserve a bracketed parameter name', async () => {
+ const { qsp, mvqsp } = await echo('filter%5Bstatus%5D=open');
+ expect(qsp).toEqual({ 'filter[status]': 'open' });
+ expect(mvqsp).toEqual({ 'filter[status]': ['open'] });
+ });
+
+ it('should null both maps when there is no query string', async () => {
+ const { qsp, mvqsp } = await echo('');
+ expect(qsp).toBeNull();
+ expect(mvqsp).toBeNull();
+ });
+});
+
// ──────────────────────────── Resource/method resolution precedence ────────────────────────────
/**
diff --git a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteController.java b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteController.java
index ac02838d1d..14e3c3dcea 100644
--- a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteController.java
+++ b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteController.java
@@ -1133,19 +1133,23 @@ public class ApiGatewayExecuteController {
});
}
- private void putQueryStringParameters(ObjectNode event, UriInfo uriInfo) {
+ void putQueryStringParameters(ObjectNode event, UriInfo uriInfo) {
MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters();
if (!queryParams.isEmpty()) {
ObjectNode qsp = event.putObject("queryStringParameters");
queryParams.forEach((name, values) -> {
- if (!values.isEmpty()) qsp.put(name, values.get(0));
+ // AWS collapses a repeated query string parameter to the LAST value in the
+ // single-value `queryStringParameters` map, exactly as it does for duplicate
+ // headers above; multiValueQueryStringParameters keeps every value in order.
+ // Taking the first value diverged from AWS: ?x=1&x=2&x=3 yields "3", not "1".
+ if (!values.isEmpty()) qsp.put(name, values.get(values.size() - 1));
});
} else {
event.putNull("queryStringParameters");
}
}
- private void putMultiValueQueryStringParameters(ObjectNode event, UriInfo uriInfo) {
+ void putMultiValueQueryStringParameters(ObjectNode event, UriInfo uriInfo) {
MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters();
if (!queryParams.isEmpty()) {
ObjectNode mqsp = event.putObject("multiValueQueryStringParameters");
diff --git a/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteControllerTest.java b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteControllerTest.java
index 34351de491..f031ed323d 100644
--- a/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteControllerTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteControllerTest.java
@@ -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(