mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
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:
1 parent
bfe2b71587
commit
e8edc3d4b1
3 files changed
+181
-3
No files matched your search
@@ -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 ────────────────────────────
|
||||
|
||||
/**
|
||||
|
||||
+7
-3
@@ -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");
|
||||
|
||||
+79
@@ -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(
|
||||
|
||||
Reference in new issue
Block a user