From 208b07b54af69899dc2bf7443430b2a83a654250 Mon Sep 17 00:00:00 2001 From: galsuchetzky-act Date: Wed, 25 Nov 2026 20:44:35 +0200 Subject: [PATCH] fix(apigateway): paginate REST deployments (#9437) Use the shared ID pager and response envelope for GetDeployments. Preserve unpaged internal reads, missing-parent responses and deployment metadata. Closes #8603 --- docs/services/api-gateway.md | 8 + .../apigateway/ApiGatewayController.java | 10 +- .../apigateway/ApiGatewayService.java | 8 + ...yDeploymentsPaginationIntegrationTest.java | 139 ++++++++++++++++++ 4 files changed, 158 insertions(+), 7 deletions(-) create mode 100644 src/test/java/io/floci/aws/services/apigateway/ApiGatewayDeploymentsPaginationIntegrationTest.java diff --git a/docs/services/api-gateway.md b/docs/services/api-gateway.md index 6e30cfa568..14f7800d2e 100644 --- a/docs/services/api-gateway.md +++ b/docs/services/api-gateway.md @@ -204,6 +204,14 @@ uses each record's stored base path, so legacy root records that report the same `basePath` remain distinct. Looking up a missing domain still returns `NotFoundException`. +### Deployment Pagination + +`GetDeployments` accepts `limit` and `position`, returns 25 deployments by default, +and caps pages at 500. Use the returned `position` to continue a partial collection; +the final page omits it. Positions belong to the caller's account, Region and REST +API. A missing parent API returns `NotFoundException`. + + ### API Key Behaviour Notes #### `CreateApiKey` and `ImportApiKeys` share one route diff --git a/src/main/java/io/floci/aws/services/apigateway/ApiGatewayController.java b/src/main/java/io/floci/aws/services/apigateway/ApiGatewayController.java index 680dcd8609..af0d651ff3 100644 --- a/src/main/java/io/floci/aws/services/apigateway/ApiGatewayController.java +++ b/src/main/java/io/floci/aws/services/apigateway/ApiGatewayController.java @@ -660,14 +660,10 @@ public class ApiGatewayController { @GET @Path("/restapis/{apiId}/deployments") - public Response getDeployments(@Context HttpHeaders headers, - @PathParam("apiId") String apiId) { + public Response getDeployments(@Context HttpHeaders headers, @PathParam("apiId") String apiId, + @QueryParam("limit") String limit, @QueryParam("position") String position) { String region = regionResolver.resolveRegion(headers); - List deployments = service.getDeployments(region, apiId); - ObjectNode root = objectMapper.createObjectNode(); - ArrayNode items = root.putArray("item"); - deployments.forEach(d -> items.add(toDeploymentNode(d))); - return Response.ok(root.toString()).type(MediaType.APPLICATION_JSON).build(); + return pageResponse(service.getDeploymentsPage(region, apiId, limit, position), this::toDeploymentNode); } @GET diff --git a/src/main/java/io/floci/aws/services/apigateway/ApiGatewayService.java b/src/main/java/io/floci/aws/services/apigateway/ApiGatewayService.java index dfcdc13d24..405255d45a 100644 --- a/src/main/java/io/floci/aws/services/apigateway/ApiGatewayService.java +++ b/src/main/java/io/floci/aws/services/apigateway/ApiGatewayService.java @@ -939,6 +939,14 @@ public class ApiGatewayService implements ResourceProvider, Resettable { return deploymentStore.scan(k -> k.startsWith(prefix)); } + public PaginatedResult getDeploymentsPage(String region, String apiId, + String limit, String position) { + List deployments = getDeployments(region, apiId); + String scope = regionResolver.getAccountId() + "::" + region + "::" + apiId + "::"; + return pageById(deployments, deployment -> scope + deployment.id(), + Pagination.parseMaxResults(limit, "limit", "BadRequestException"), position); + } + public Deployment getDeployment(String region, String apiId, String deploymentId) { return deploymentStore.get(deploymentKey(region, apiId, deploymentId)) .orElseThrow(() -> new AwsException("NotFoundException", "Deployment not found", 404)); diff --git a/src/test/java/io/floci/aws/services/apigateway/ApiGatewayDeploymentsPaginationIntegrationTest.java b/src/test/java/io/floci/aws/services/apigateway/ApiGatewayDeploymentsPaginationIntegrationTest.java new file mode 100644 index 0000000000..8ff6b99288 --- /dev/null +++ b/src/test/java/io/floci/aws/services/apigateway/ApiGatewayDeploymentsPaginationIntegrationTest.java @@ -0,0 +1,139 @@ +package io.floci.aws.services.apigateway; + +import io.floci.aws.testing.AwsTestClients; +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.api.Test; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.apigateway.ApiGatewayClient; +import software.amazon.awssdk.services.apigateway.model.BadRequestException; +import software.amazon.awssdk.services.apigateway.model.GetDeploymentsResponse; +import software.amazon.awssdk.services.apigateway.model.IntegrationType; +import software.amazon.awssdk.services.apigateway.model.NotFoundException; + +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.UUID; +import java.util.stream.Collectors; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@QuarkusTest +class ApiGatewayDeploymentsPaginationIntegrationTest { + + @Test + void explicitLimitAndSdkTraversalPreserveDeploymentFields() { + try (Fixture fixture = new Fixture(3)) { + GetDeploymentsResponse first = fixture.client.getDeployments(r -> + r.restApiId(fixture.apiId).limit(1)); + assertEquals(1, first.items().size()); + assertNotNull(first.position()); + Set seen = new HashSet<>(); + fixture.client.getDeploymentsPaginator(r -> r.restApiId(fixture.apiId).limit(1)) + .forEach(page -> { + assertEquals(1, page.items().size()); + page.items().forEach(deployment -> { + assertTrue(seen.add(deployment.id())); + assertNotNull(deployment.createdDate()); + assertTrue(deployment.description().startsWith("deployment-")); + }); + }); + assertEquals(fixture.ids, seen); + GetDeploymentsResponse complete = fixture.client.getDeployments(r -> + r.restApiId(fixture.apiId).limit(500)); + assertEquals(fixture.ids, complete.items().stream().map(deployment -> deployment.id()) + .collect(Collectors.toSet())); + assertNull(complete.position()); + fixture.ids.forEach(id -> assertEquals(id, fixture.client.getDeployment(r -> + r.restApiId(fixture.apiId).deploymentId(id)).id())); + } + } + + @Test + void omittedLimitUsesTwentyFiveAndTheLastPageOmitsPosition() { + try (Fixture fixture = new Fixture(26)) { + GetDeploymentsResponse first = fixture.client.getDeployments(r -> r.restApiId(fixture.apiId)); + assertEquals(25, first.items().size()); + assertNotNull(first.position()); + GetDeploymentsResponse last = fixture.client.getDeployments(r -> + r.restApiId(fixture.apiId).position(first.position())); + assertEquals(1, last.items().size()); + assertNull(last.position()); + } + } + + @Test + void emptyApiAndMissingParentKeepTheirExistingResponses() { + try (Fixture fixture = new Fixture(0)) { + GetDeploymentsResponse empty = fixture.client.getDeployments(r -> + r.restApiId(fixture.apiId).limit(1)); + assertTrue(empty.items().isEmpty()); + assertNull(empty.position()); + assertThrows(NotFoundException.class, () -> fixture.client.getDeployments(r -> + r.restApiId("missing-api").limit(1))); + } + } + + @Test + void positionStaysWithItsApiAndDoesNotExposeForeignParents() { + String account = account(); + try (Fixture first = new Fixture(account, 2); + Fixture otherApi = new Fixture(account, 2); + ApiGatewayClient otherAccount = AwsTestClients.create( + ApiGatewayClient.builder(), account(), Region.US_EAST_1); + ApiGatewayClient otherRegion = AwsTestClients.create( + ApiGatewayClient.builder(), account, Region.US_WEST_2)) { + String position = first.client.getDeployments(r -> + r.restApiId(first.apiId).limit(1)).position(); + assertNotNull(position); + assertThrows(BadRequestException.class, () -> otherApi.client.getDeployments(r -> + r.restApiId(otherApi.apiId).limit(1).position(position))); + assertThrows(NotFoundException.class, () -> otherAccount.getDeployments(r -> + r.restApiId(first.apiId).limit(1).position(position))); + assertThrows(NotFoundException.class, () -> otherRegion.getDeployments(r -> + r.restApiId(first.apiId).limit(1).position(position))); + } + } + + private static String account() { + return String.format("%012d", Math.floorMod(System.nanoTime(), 1_000_000_000_000L)); + } + + private static final class Fixture implements AutoCloseable { + private final ApiGatewayClient client; + private final String apiId; + private final Set ids = new HashSet<>(); + + private Fixture(int count) { + this(account(), count); + } + + private Fixture(String account, int count) { + client = AwsTestClients.create(ApiGatewayClient.builder(), account, Region.US_EAST_1); + apiId = client.createRestApi(r -> r.name("deployment-page-" + UUID.randomUUID())).id(); + String root = client.getResources(r -> r.restApiId(apiId)).items().stream() + .filter(resource -> "/".equals(resource.path())).findFirst().orElseThrow().id(); + client.putMethod(r -> r.restApiId(apiId).resourceId(root).httpMethod("GET") + .authorizationType("NONE")); + client.putIntegration(r -> r.restApiId(apiId).resourceId(root).httpMethod("GET") + .type(IntegrationType.MOCK).requestTemplates(Map.of("application/json", "{\"statusCode\":200}"))); + for (int i = 0; i < count; i++) { + String description = "deployment-" + i; + ids.add(client.createDeployment(r -> r.restApiId(apiId).description(description)).id()); + } + } + + @Override + public void close() { + try { + client.deleteRestApi(r -> r.restApiId(apiId)); + } finally { + client.close(); + } + } + } +}