fix(apigateway): honor limit and position on GetRequestValidators (#9060)

This commit is contained in:
Daniël van Ziel authored and GitHub committed 2026-11-19 22:12:18 -04:00
1 parent d37698ea71
commit 41aa3ba2eb
5 files changed
+129 -45

No files matched your search

@@ -67,6 +67,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
/**
* Unified AWS API Gateway management endpoints (v1 REST and v2 HTTP).
@@ -279,6 +280,16 @@ public class ApiGatewayController {
return Pagination.parseMaxResults(limit, "limit", "BadRequestException");
}
private <T> Response pageResponse(PaginatedResult<T> page, Function<T, JsonNode> toNode) {
ObjectNode root = objectMapper.createObjectNode();
ArrayNode items = root.putArray("item");
page.items().forEach(item -> items.add(toNode.apply(item)));
if (page.nextToken() != null) {
root.put("position", page.nextToken());
}
return Response.ok(root.toString()).type(MediaType.APPLICATION_JSON).build();
}
@GET
@Path("/restapis/{apiId}/authorizers")
public Response getAuthorizers(@Context HttpHeaders headers, @PathParam("apiId") String apiId,
@@ -286,13 +297,7 @@ public class ApiGatewayController {
String region = regionResolver.resolveRegion(headers);
// Authorizer clashes with the imported API Gateway v2 model
PaginatedResult<io.floci.aws.services.apigateway.model.Authorizer> page = service.getAuthorizerPage(region, apiId, pageLimit(limit), position);
ObjectNode root = objectMapper.createObjectNode();
ArrayNode items = root.putArray("item");
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();
return pageResponse(page, this::toAuthorizerNode);
}
@GET
@@ -1136,13 +1141,11 @@ public class ApiGatewayController {
@GET
@Path("/restapis/{apiId}/requestvalidators")
public Response getRequestValidators(@Context HttpHeaders headers, @PathParam("apiId") String apiId) {
public Response getRequestValidators(@Context HttpHeaders headers, @PathParam("apiId") String apiId,
@QueryParam("limit") String limit, @QueryParam("position") String position) {
String region = regionResolver.resolveRegion(headers);
List<RequestValidator> validators = service.getRequestValidators(region, apiId);
ObjectNode root = objectMapper.createObjectNode();
ArrayNode items = root.putArray("item");
validators.forEach(v -> items.add(toRequestValidatorNode(v)));
return Response.ok(root.toString()).type(MediaType.APPLICATION_JSON).build();
PaginatedResult<RequestValidator> page = service.getRequestValidatorPage(region, apiId, pageLimit(limit), position);
return pageResponse(page, this::toRequestValidatorNode);
}
@GET
@@ -1259,13 +1262,7 @@ public class ApiGatewayController {
String region = regionResolver.resolveRegion(headers);
PaginatedResult<DocumentationPart> page = service.getDocumentationParts(region, apiId, type, name,
path, locationStatus, pageLimit(limit), position);
ObjectNode root = objectMapper.createObjectNode();
ArrayNode items = root.putArray("item");
page.items().forEach(part -> items.add(objectMapper.valueToTree(part)));
if (page.nextToken() != null) {
root.put("position", page.nextToken());
}
return Response.ok(root.toString()).type(MediaType.APPLICATION_JSON).build();
return pageResponse(page, part -> objectMapper.valueToTree(part));
}
@GET
@@ -1267,18 +1267,16 @@ public class ApiGatewayService implements ResourceProvider, Resettable {
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);
return pageById(getAuthorizers(region, apiId), Authorizer::getId, limit, position);
}
/**
* One page of a list already sorted by id. The token is the id of the last item returned, and
* One page of a list, sorted by id here. 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,
private static <T> PaginatedResult<T> pageById(List<T> items, Function<T, String> idOf, Integer limit,
String position) {
List<T> sorted = items.stream().sorted(Comparator.comparing(idOf)).toList();
int pageSize = resolvePageSize(limit);
int start = 0;
if (position != null && !position.isBlank()) {
@@ -2371,6 +2369,11 @@ public class ApiGatewayService implements ResourceProvider, Resettable {
return requestValidatorStore.scan(k -> k.startsWith(prefix));
}
public PaginatedResult<RequestValidator> getRequestValidatorPage(String region, String apiId, Integer limit,
String position) {
return pageById(getRequestValidators(region, apiId), RequestValidator::getId, limit, position);
}
public void deleteRequestValidator(String region, String apiId, String validatorId) {
getRequestValidator(region, apiId, validatorId);
requestValidatorStore.delete(requestValidatorKey(region, apiId, validatorId));
@@ -2582,7 +2585,6 @@ public class ApiGatewayService implements ResourceProvider, Resettable {
.filter(part -> name == null || name.equals(part.getLocation().get("name")))
.filter(part -> path == null || path.equals(part.getLocation().get("path")))
.filter(part -> !"UNDOCUMENTED".equals(locationStatus))
.sorted(Comparator.comparing(DocumentationPart::getId))
.toList();
return pageById(parts, DocumentationPart::getId, limit, position);
@@ -12,13 +12,11 @@ import software.amazon.awssdk.services.apigateway.model.BadRequestException;
import software.amazon.awssdk.services.apigateway.model.GetAuthorizersResponse;
import software.amazon.awssdk.services.apigateway.model.NotFoundException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -56,22 +54,10 @@ class ApiGatewayAuthorizerPagingIntegrationTest {
assertEquals(1, first.items().size());
assertNotNull(first.position());
List<String> seen = new ArrayList<>();
List<String> cursors = new ArrayList<>();
seen.add(first.items().get(0).id());
String position = first.position();
for (int page = 0; position != null && page < created.size(); page++) {
assertFalse(cursors.contains(position), "position repeated: " + position);
cursors.add(position);
String next = position;
GetAuthorizersResponse response = client.getAuthorizers(request -> request.restApiId(apiId)
.limit(1).position(next));
assertEquals(1, response.items().size());
assertFalse(seen.contains(response.items().get(0).id()), "authorizer repeated");
seen.add(response.items().get(0).id());
position = response.position();
}
assertNull(position);
List<String> seen = ApiGatewayPagingWalk.oneItemPerPageIds(
position -> client.getAuthorizers(
request -> request.restApiId(apiId).limit(1).position(position)),
GetAuthorizersResponse::items, GetAuthorizersResponse::position, Authorizer::id);
assertEquals(3, seen.size());
assertEquals(created, new HashSet<>(seen));
} finally {
@@ -0,0 +1,39 @@
package io.floci.aws.services.apigateway;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
final class ApiGatewayPagingWalk {
private ApiGatewayPagingWalk() {
}
/**
* Follows {@code position} from the first page to the last, one item per page, and returns the ids
* seen in order. Fails if an id or a position repeats.
*/
static <P, I> List<String> oneItemPerPageIds(Function<String, P> fetchPage, Function<P, List<I>> itemsOf,
Function<P, String> positionOf, Function<I, String> idOf) {
List<String> seen = new ArrayList<>();
List<String> positions = new ArrayList<>();
String position = null;
do {
P page = fetchPage.apply(position);
List<I> items = itemsOf.apply(page);
assertEquals(1, items.size());
String id = idOf.apply(items.get(0));
assertFalse(seen.contains(id), "item repeated: " + id);
seen.add(id);
position = positionOf.apply(page);
if (position != null) {
assertFalse(positions.contains(position), "position repeated: " + position);
positions.add(position);
}
} while (position != null);
return seen;
}
}
@@ -0,0 +1,60 @@
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.http.apache5.Apache5HttpClient;
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.GetRequestValidatorsResponse;
import software.amazon.awssdk.services.apigateway.model.RequestValidator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
@QuarkusTest
class ApiGatewayRequestValidatorPagingIntegrationTest {
private ApiGatewayClient client() {
return AwsTestClients.create(ApiGatewayClient.builder(), "test", Region.US_EAST_1,
Apache5HttpClient.builder());
}
@Test
void getRequestValidatorsWithLimitFollowsPositionToTerminalPage() {
try (ApiGatewayClient client = client()) {
String apiId = client.createRestApi(request -> request.name("validator-paging-sdk")).id();
try {
Set<String> created = new HashSet<>();
for (int i = 0; i < 3; i++) {
String name = "Validator" + i;
boolean body = i != 0;
created.add(client.createRequestValidator(request -> request.restApiId(apiId).name(name)
.validateRequestBody(body).validateRequestParameters(!body)).id());
}
List<String> seen = ApiGatewayPagingWalk.oneItemPerPageIds(
position -> client.getRequestValidators(
request -> request.restApiId(apiId).limit(1).position(position)),
GetRequestValidatorsResponse::items, GetRequestValidatorsResponse::position,
RequestValidator::id);
assertEquals(created, new HashSet<>(seen));
GetRequestValidatorsResponse all = client.getRequestValidators(request -> request.restApiId(apiId));
assertEquals(3, all.items().size());
assertNull(all.position());
assertThrows(BadRequestException.class, () -> client.getRequestValidators(
request -> request.restApiId(apiId).limit(1).position("missing")));
} finally {
client.deleteRestApi(request -> request.restApiId(apiId));
}
}
}
}