From f8a2f2d5c43ce67af91024b77a5e27a903a2eb1d Mon Sep 17 00:00:00 2001 From: Matt Lishman Date: Fri, 29 May 2026 05:41:44 +0100 Subject: [PATCH] fix(apigw-v2): populate pathParameters in Lambda proxy event for HTTP API integrations (#1067) * fix(apigw-v2): populate pathParameters in Lambda proxy event for HTTP API integrations buildV2ProxyEvent never called extractV2PathParams, so pathParameters was always absent from the Lambda event payload. Frameworks that rely on it for routing (e.g. the tRPC AWS Lambda adapter via ANY /{proxy+}) failed with path routing errors. Wire extractV2PathParams into buildV2ProxyEvent after the queryStringParameters block, mirroring how dispatchHttpProxyV2 already uses it. Also relax the method visibility to package-private to allow the new unit tests to call it directly. * test(ec2): scope Ec2NetworkInterfacePaginationTest to its own VPC DescribeNetworkInterfaces was called without a filter, so the tests counted ENIs created by other concurrently-running sdk-test-java tests (e.g. sdk-test-instance from Ec2DescribeNetworkInterfacesTest), causing intermittent failures when test ordering meant those ENIs existed at assertion time. Adds a vpc-id filter helper scoped to the VPC created in @BeforeAll so each test only sees the 6 ENIs it owns. --- .../Ec2NetworkInterfacePaginationTest.java | 34 +++----- .../ApiGatewayExecuteController.java | 8 +- .../BuildV2ProxyEventPathParametersTest.java | 78 +++++++++++++++++++ 3 files changed, 97 insertions(+), 23 deletions(-) create mode 100644 src/test/java/io/github/hectorvent/floci/services/apigateway/BuildV2ProxyEventPathParametersTest.java diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/Ec2NetworkInterfacePaginationTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/Ec2NetworkInterfacePaginationTest.java index 56ce93ca69..5f4071f85b 100644 --- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/Ec2NetworkInterfacePaginationTest.java +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/Ec2NetworkInterfacePaginationTest.java @@ -110,12 +110,17 @@ class Ec2NetworkInterfacePaginationTest { } } + private DescribeNetworkInterfacesRequest.Builder vpcFilter() { + return DescribeNetworkInterfacesRequest.builder() + .filters(Filter.builder().name("vpc-id").values(vpcId).build()); + } + @Test @Order(1) @DisplayName("DescribeNetworkInterfaces without pagination returns all 6 ENIs") void describeNetworkInterfacesAll() { DescribeNetworkInterfacesResponse resp = ec2.describeNetworkInterfaces( - DescribeNetworkInterfacesRequest.builder().build()); + vpcFilter().build()); assertThat(resp.networkInterfaces()).hasSize(6); assertThat(resp.nextToken()).isNull(); @@ -126,9 +131,7 @@ class Ec2NetworkInterfacePaginationTest { @DisplayName("MaxResults=5 truncates first page and returns nextToken") void describeNetworkInterfacesFirstPage() { DescribeNetworkInterfacesResponse resp = ec2.describeNetworkInterfaces( - DescribeNetworkInterfacesRequest.builder() - .maxResults(5) - .build()); + vpcFilter().maxResults(5).build()); assertThat(resp.networkInterfaces()).hasSize(5); assertThat(resp.nextToken()).isNotNull().isNotEmpty(); @@ -140,17 +143,12 @@ class Ec2NetworkInterfacePaginationTest { void describeNetworkInterfacesContinuation() { // Page 1: get nextToken DescribeNetworkInterfacesResponse page1 = ec2.describeNetworkInterfaces( - DescribeNetworkInterfacesRequest.builder() - .maxResults(5) - .build()); + vpcFilter().maxResults(5).build()); assertThat(page1.nextToken()).isNotNull(); // Page 2: use nextToken DescribeNetworkInterfacesResponse page2 = ec2.describeNetworkInterfaces( - DescribeNetworkInterfacesRequest.builder() - .maxResults(5) - .nextToken(page1.nextToken()) - .build()); + vpcFilter().maxResults(5).nextToken(page1.nextToken()).build()); assertThat(page2.networkInterfaces()).hasSize(1); assertThat(page2.nextToken()).isNull(); @@ -175,25 +173,17 @@ class Ec2NetworkInterfacePaginationTest { void describeNetworkInterfacesTokenPastEnd() { // Get a valid token from first page DescribeNetworkInterfacesResponse page1 = ec2.describeNetworkInterfaces( - DescribeNetworkInterfacesRequest.builder() - .maxResults(5) - .build()); + vpcFilter().maxResults(5).build()); String token = page1.nextToken(); // Use it to get the last page DescribeNetworkInterfacesResponse page2 = ec2.describeNetworkInterfaces( - DescribeNetworkInterfacesRequest.builder() - .maxResults(5) - .nextToken(token) - .build()); + vpcFilter().maxResults(5).nextToken(token).build()); // Using the token from page2 (which is null since it's the last page) // would be invalid, but using a valid token again should still work DescribeNetworkInterfacesResponse repeat = ec2.describeNetworkInterfaces( - DescribeNetworkInterfacesRequest.builder() - .maxResults(5) - .nextToken(token) - .build()); + vpcFilter().maxResults(5).nextToken(token).build()); assertThat(repeat.networkInterfaces()).hasSize(1); assertThat(repeat.nextToken()).isNull(); 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 340963a492..85d5bb3367 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 @@ -1734,7 +1734,7 @@ public class ApiGatewayExecuteController { }; } - private String buildV2ProxyEvent(String httpMethod, String path, String routeKey, + String buildV2ProxyEvent(String httpMethod, String path, String routeKey, String apiId, String stageName, HttpHeaders headers, UriInfo uriInfo, byte[] body, String requestId) { @@ -1759,6 +1759,12 @@ public class ApiGatewayExecuteController { } } + Map pathParams = extractV2PathParams(routeKey, path); + if (!pathParams.isEmpty()) { + ObjectNode pp = event.putObject("pathParameters"); + pathParams.forEach(pp::put); + } + ObjectNode ctx = event.putObject("requestContext"); ctx.put("accountId", regionResolver.getAccountId()); ctx.put("apiId", apiId); diff --git a/src/test/java/io/github/hectorvent/floci/services/apigateway/BuildV2ProxyEventPathParametersTest.java b/src/test/java/io/github/hectorvent/floci/services/apigateway/BuildV2ProxyEventPathParametersTest.java new file mode 100644 index 0000000000..a127992bc9 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/apigateway/BuildV2ProxyEventPathParametersTest.java @@ -0,0 +1,78 @@ +package io.github.hectorvent.floci.services.apigateway; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import io.github.hectorvent.floci.core.common.RegionResolver; +import jakarta.ws.rs.core.HttpHeaders; +import jakarta.ws.rs.core.MultivaluedHashMap; +import jakarta.ws.rs.core.UriInfo; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.net.URI; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Verifies that {@link ApiGatewayExecuteController#buildV2ProxyEvent} populates + * {@code pathParameters} in the Lambda event payload for HTTP API (V2) integrations. + */ +class BuildV2ProxyEventPathParametersTest { + + private ApiGatewayExecuteController controller; + private HttpHeaders headers; + private UriInfo uriInfo; + + @BeforeEach + void setUp() throws Exception { + RegionResolver regionResolver = mock(RegionResolver.class); + when(regionResolver.getAccountId()).thenReturn("000000000000"); + + headers = mock(HttpHeaders.class); + when(headers.getRequestHeaders()).thenReturn(new MultivaluedHashMap<>()); + when(headers.getHeaderString("User-Agent")).thenReturn(null); + + uriInfo = mock(UriInfo.class); + when(uriInfo.getQueryParameters()).thenReturn(new MultivaluedHashMap<>()); + when(uriInfo.getRequestUri()).thenReturn(new URI("http://localhost:4566/api/stage/trpc/health")); + + controller = new ApiGatewayExecuteController( + null, null, null, + regionResolver, new ObjectMapper(), null, + null, null, null + ); + } + + @Test + void greedyProxyRoutePopulatesPathParameters() throws Exception { + String json = controller.buildV2ProxyEvent( + "POST", "/trpc/health", "ANY /{proxy+}", + "abc123", "$default", headers, uriInfo, null, "req-1"); + JsonNode event = new ObjectMapper().readTree(json); + assertTrue(event.has("pathParameters"), "pathParameters must be present"); + assertEquals("trpc/health", event.get("pathParameters").get("proxy").asText()); + } + + @Test + void namedParamRoutePopulatesPathParameters() throws Exception { + when(uriInfo.getRequestUri()).thenReturn(new URI("http://localhost:4566/api/stage/users/42")); + String json = controller.buildV2ProxyEvent( + "GET", "/users/42", "GET /users/{id}", + "abc123", "$default", headers, uriInfo, null, "req-2"); + JsonNode event = new ObjectMapper().readTree(json); + assertTrue(event.has("pathParameters"), "pathParameters must be present"); + assertEquals("42", event.get("pathParameters").get("id").asText()); + } + + @Test + void defaultRouteOmitsPathParameters() throws Exception { + when(uriInfo.getRequestUri()).thenReturn(new URI("http://localhost:4566/api/stage/anything")); + String json = controller.buildV2ProxyEvent( + "GET", "/anything", "$default", + "abc123", "$default", headers, uriInfo, null, "req-3"); + JsonNode event = new ObjectMapper().readTree(json); + assertFalse(event.has("pathParameters"), "pathParameters must be absent for $default route"); + } +}