fix(apigateway): page GetAuthorizers with limit and position (#8495)

API Gateway's GetAuthorizers now pages with limit and position instead
of returning every authorizer at once: authorizers come back sorted by
id, at most limit of them (25 by default, capped at 500, refused below
1), with position set to the id the next page starts after, and an
unknown position is refused with BadRequestException as GetUsage and
GetDocumentationParts refuse one. GetAuthorizers and
GetDocumentationParts share one id-cursor page helper returning
PaginatedResult, and the page-size rule all three paged calls use is
named resolvePageSize.

Closes #8439
This commit is contained in:
Mohith authored and GitHub committed 2026-11-15 19:21:34 -04:00
1 parent bca36ce5b5
commit 722472e76b
4 files changed
+200 -44

No files matched your search

@@ -277,12 +277,17 @@ public class ApiGatewayController {
@GET
@Path("/restapis/{apiId}/authorizers")
public Response getAuthorizers(@Context HttpHeaders headers, @PathParam("apiId") String apiId) {
public Response getAuthorizers(@Context HttpHeaders headers, @PathParam("apiId") String apiId,
@QueryParam("limit") Integer limit, @QueryParam("position") String position) {
String region = regionResolver.resolveRegion(headers);
List<io.floci.aws.services.apigateway.model.Authorizer> auths = service.getAuthorizers(region, apiId);
// Authorizer clashes with the imported API Gateway v2 model
PaginatedResult<io.floci.aws.services.apigateway.model.Authorizer> page = service.getAuthorizerPage(region, apiId, limit, position);
ObjectNode root = objectMapper.createObjectNode();
ArrayNode items = root.putArray("item");
auths.forEach(a -> items.add(toAuthorizerNode(a)));
page.items().forEach(a -> items.add(toAuthorizerNode(a)));
if (page.nextToken() != null) {
root.put("position", page.nextToken());
}
return Response.ok(root.toString()).type(MediaType.APPLICATION_JSON).build();
}
@@ -1248,13 +1253,13 @@ public class ApiGatewayController {
@QueryParam("locationStatus") String locationStatus,
@QueryParam("limit") Integer limit, @QueryParam("position") String position) {
String region = regionResolver.resolveRegion(headers);
ApiGatewayService.DocumentationPartPage page = service.getDocumentationParts(region, apiId, type, name,
PaginatedResult<DocumentationPart> page = service.getDocumentationParts(region, apiId, type, name,
path, locationStatus, limit, position);
ObjectNode root = objectMapper.createObjectNode();
ArrayNode items = root.putArray("item");
page.items().forEach(part -> items.add(objectMapper.valueToTree(part)));
if (page.position() != null) {
root.put("position", page.position());
if (page.nextToken() != null) {
root.put("position", page.nextToken());
}
return Response.ok(root.toString()).type(MediaType.APPLICATION_JSON).build();
}
@@ -15,6 +15,7 @@ import io.floci.aws.core.common.AwsPartitions;
import io.floci.aws.core.common.AwsRegionFacts;
import io.floci.aws.core.common.AwsRegions;
import io.floci.aws.core.common.CloudFrontEdgeDomain;
import io.floci.aws.core.common.PaginatedResult;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.core.common.ReservedTags;
import io.floci.aws.core.common.Resettable;
@@ -110,13 +111,13 @@ public class ApiGatewayService implements ResourceProvider, Resettable {
private static final ObjectMapper JSON = new ObjectMapper();
/** Documented default page size for GetUsage. */
private static final int DEFAULT_USAGE_LIMIT = 25;
/** Documented default page size for the paged list calls. */
private static final int DEFAULT_PAGE_SIZE = 25;
/**
* Documented maximum results per page. A larger {@code limit} is accepted, as real API Gateway
* accepts one, but the page returned is still capped here.
*/
private static final int MAX_USAGE_PAGE_SIZE = 500;
private static final int MAX_PAGE_SIZE = 500;
private static final Logger LOG = Logger.getLogger(ApiGatewayService.class);
@@ -1251,6 +1252,36 @@ public class ApiGatewayService implements ResourceProvider, Resettable {
return authorizerStore.scan(k -> k.startsWith(prefix));
}
public PaginatedResult<Authorizer> getAuthorizerPage(String region, String apiId, Integer limit,
String position) {
List<Authorizer> authorizers = getAuthorizers(region, apiId).stream()
.sorted(Comparator.comparing(Authorizer::getId))
.toList();
return pageById(authorizers, Authorizer::getId, limit, position);
}
/**
* One page of a list already sorted by id. The token is the id of the last item returned, and
* an unrecognised token is a {@code BadRequestException}, as on AWS.
*/
private static <T> PaginatedResult<T> pageById(List<T> sorted, Function<T, String> idOf, Integer limit,
String position) {
int pageSize = resolvePageSize(limit);
int start = 0;
if (position != null && !position.isBlank()) {
while (start < sorted.size() && !position.equals(idOf.apply(sorted.get(start)))) {
start++;
}
if (start == sorted.size()) {
throw new AwsException("BadRequestException", "Invalid position parameter", 400);
}
start++;
}
int end = Math.min(start + pageSize, sorted.size());
String nextPosition = end < sorted.size() ? idOf.apply(sorted.get(end - 1)) : null;
return new PaginatedResult<>(List.copyOf(sorted.subList(start, end)), nextPosition);
}
public void deleteAuthorizer(String region, String apiId, String authorizerId) {
getAuthorizer(region, apiId, authorizerId);
authorizerStore.delete(authorizerKey(region, apiId, authorizerId));
@@ -1790,7 +1821,7 @@ public class ApiGatewayService implements ResourceProvider, Resettable {
if (end.isBefore(start)) {
throw new AwsException("BadRequestException", "Usage end date must be after start date", 400);
}
int pageSize = resolveUsageLimit(limit);
int pageSize = resolvePageSize(limit);
List<UsagePlanKey> keys = getUsagePlanKeys(region, usagePlanId).stream()
.filter(key -> keyId == null || keyId.isBlank() || keyId.equals(key.getId()))
@@ -1848,9 +1879,11 @@ public class ApiGatewayService implements ResourceProvider, Resettable {
}
/**
* Resolves the effective page size, which is not the same thing as accepting the request.
* Resolves the effective page size of a paged list call ({@code GetUsage}, {@code GetAuthorizers},
* {@code GetDocumentationParts}), which is not the same thing as accepting the request.
*
* <p>Request acceptance and response page size are separate. Probed against real API Gateway,
* <p>Request acceptance and response page size are separate. The probe below was made against
* {@code GetUsage} on real API Gateway:
* every {@code limit} from 500 up to {@link Integer#MAX_VALUE} is accepted without error, so
* none is rejected here either. What that probe does <em>not</em> establish is that the service
* ever returns more than 500 entries in one page, and the documented contract says 500 is the
@@ -1861,14 +1894,14 @@ public class ApiGatewayService implements ResourceProvider, Resettable {
* {@code limit=-1} with an {@code InternalFailure}, which is a fault rather than a contract, so
* a page size below one is rejected as a bad request instead of reproducing a 500.
*/
static int resolveUsageLimit(Integer limit) {
static int resolvePageSize(Integer limit) {
if (limit == null) {
return DEFAULT_USAGE_LIMIT;
return DEFAULT_PAGE_SIZE;
}
if (limit < 1) {
throw new AwsException("BadRequestException", "Invalid limit parameter", 400);
}
return Math.min(limit, MAX_USAGE_PAGE_SIZE);
return Math.min(limit, MAX_PAGE_SIZE);
}
/**
@@ -2443,9 +2476,7 @@ public class ApiGatewayService implements ResourceProvider, Resettable {
.orElseThrow(() -> new AwsException("NotFoundException", "Invalid documentation part id specified", 404));
}
public record DocumentationPartPage(List<DocumentationPart> items, String position) {}
public DocumentationPartPage getDocumentationParts(String region, String apiId, String type, String name,
public PaginatedResult<DocumentationPart> getDocumentationParts(String region, String apiId, String type, String name,
String path, String locationStatus, Integer limit,
String position) {
getRestApi(region, apiId);
@@ -2469,10 +2500,6 @@ public class ApiGatewayService implements ResourceProvider, Resettable {
modeledRequest.put("position", position);
}
ApiGatewayDocumentationRequestShapes.check("GetDocumentationParts", modeledRequest);
if (limit != null && limit < 1) {
throw new AwsException("BadRequestException", "Invalid limit parameter", 400);
}
int pageSize = limit == null ? 25 : Math.min(limit, 500);
String prefix = region + "::" + apiId + "::";
List<DocumentationPart> parts = documentationPartStore.scan(key -> key.startsWith(prefix)).stream()
.filter(part -> type == null || type.equals(part.getLocation().get("type")))
@@ -2482,19 +2509,7 @@ public class ApiGatewayService implements ResourceProvider, Resettable {
.sorted(Comparator.comparing(DocumentationPart::getId))
.toList();
int start = 0;
if (position != null && !position.isBlank()) {
while (start < parts.size() && !position.equals(parts.get(start).getId())) {
start++;
}
if (start == parts.size()) {
throw new AwsException("BadRequestException", "Invalid position parameter", 400);
}
start++;
}
int end = Math.min(start + pageSize, parts.size());
String nextPosition = end < parts.size() ? parts.get(end - 1).getId() : null;
return new DocumentationPartPage(List.copyOf(parts.subList(start, end)), nextPosition);
return pageById(parts, DocumentationPart::getId, limit, position);
}
public synchronized DocumentationPart updateDocumentationPart(String region, String apiId, String partId,