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 cbcf210b89..7b035a7a7e 100644 --- a/src/main/java/io/floci/aws/services/apigateway/ApiGatewayController.java +++ b/src/main/java/io/floci/aws/services/apigateway/ApiGatewayController.java @@ -281,6 +281,11 @@ public class ApiGatewayController { return Pagination.parseMaxResults(limit, "limit", "BadRequestException"); } + /** An empty query value binds as null, so an explicitly empty {@code position} is read from the URI. */ + private static String requestedPosition(String position, UriInfo uriInfo) { + return position == null && uriInfo.getQueryParameters().containsKey("position") ? "" : position; + } + private Response pageResponse(PaginatedResult page, Function toNode) { ObjectNode root = objectMapper.createObjectNode(); ArrayNode items = root.putArray("item"); @@ -294,10 +299,12 @@ public class ApiGatewayController { @GET @Path("/restapis/{apiId}/authorizers") public Response getAuthorizers(@Context HttpHeaders headers, @PathParam("apiId") String apiId, - @QueryParam("limit") String limit, @QueryParam("position") String position) { + @QueryParam("limit") String limit, @QueryParam("position") String position, + @Context UriInfo uriInfo) { String region = regionResolver.resolveRegion(headers); // Authorizer clashes with the imported API Gateway v2 model - PaginatedResult page = service.getAuthorizerPage(region, apiId, pageLimit(limit), position); + PaginatedResult page = + service.getAuthorizerPage(region, apiId, pageLimit(limit), requestedPosition(position, uriInfo)); return pageResponse(page, this::toAuthorizerNode); } @@ -438,10 +445,7 @@ public class ApiGatewayController { public Response getRestApis(@Context HttpHeaders headers, @Context UriInfo uriInfo, @QueryParam("limit") String limit, @QueryParam("position") String position) { String region = regionResolver.resolveRegion(headers); - // An empty query value binds as null, so presence is read from the URI. - String requestedPosition = position == null && uriInfo.getQueryParameters().containsKey("position") - ? "" : position; - return pageResponse(service.getRestApiPage(region, pageLimit(limit), requestedPosition), + return pageResponse(service.getRestApiPage(region, pageLimit(limit), requestedPosition(position, uriInfo)), api -> toApiNode(region, api)); } @@ -475,10 +479,11 @@ public class ApiGatewayController { @Path("/restapis/{apiId}/resources") public Response getResources(@Context HttpHeaders headers, @PathParam("apiId") String apiId, @QueryParam("embed") List embed, - @QueryParam("limit") String limit, @QueryParam("position") String position) { + @QueryParam("limit") String limit, @QueryParam("position") String position, + @Context UriInfo uriInfo) { String region = regionResolver.resolveRegion(headers); boolean embedMethods = embed != null && embed.contains("methods"); - return pageResponse(service.getResourcePage(region, apiId, pageLimit(limit), position), + return pageResponse(service.getResourcePage(region, apiId, pageLimit(limit), requestedPosition(position, uriInfo)), resource -> toResourceNode(resource, embedMethods)); } @@ -661,9 +666,10 @@ public class ApiGatewayController { @GET @Path("/restapis/{apiId}/deployments") public Response getDeployments(@Context HttpHeaders headers, @PathParam("apiId") String apiId, - @QueryParam("limit") String limit, @QueryParam("position") String position) { + @QueryParam("limit") String limit, @QueryParam("position") String position, + @Context UriInfo uriInfo) { String region = regionResolver.resolveRegion(headers); - return pageResponse(service.getDeploymentsPage(region, apiId, limit, position), this::toDeploymentNode); + return pageResponse(service.getDeploymentsPage(region, apiId, limit, requestedPosition(position, uriInfo)), this::toDeploymentNode); } @GET @@ -908,9 +914,10 @@ public class ApiGatewayController { @GET @Path("/usageplans") public Response getUsagePlans(@Context HttpHeaders headers, @QueryParam("keyId") String keyId, - @QueryParam("limit") String limit, @QueryParam("position") String position) { + @QueryParam("limit") String limit, @QueryParam("position") String position, + @Context UriInfo uriInfo) { String region = regionResolver.resolveRegion(headers); - PaginatedResult page = service.getUsagePlanPage(region, keyId, pageLimit(limit), position); + PaginatedResult page = service.getUsagePlanPage(region, keyId, pageLimit(limit), requestedPosition(position, uriInfo)); return pageResponse(page, p -> toUsagePlanNode(p, false)); } @@ -1029,10 +1036,11 @@ public class ApiGatewayController { @GET @Path("/usageplans/{usagePlanId}/keys") public Response getUsagePlanKeys(@Context HttpHeaders headers, @PathParam("usagePlanId") String usagePlanId, - @QueryParam("limit") String limit, @QueryParam("position") String position) { + @QueryParam("limit") String limit, @QueryParam("position") String position, + @Context UriInfo uriInfo) { String region = regionResolver.resolveRegion(headers); PaginatedResult page = - service.getUsagePlanKeyPage(region, usagePlanId, pageLimit(limit), position); + service.getUsagePlanKeyPage(region, usagePlanId, pageLimit(limit), requestedPosition(position, uriInfo)); return pageResponse(page, this::toUsagePlanKeyNode); } @@ -1135,9 +1143,11 @@ public class ApiGatewayController { @GET @Path("/restapis/{apiId}/requestvalidators") public Response getRequestValidators(@Context HttpHeaders headers, @PathParam("apiId") String apiId, - @QueryParam("limit") String limit, @QueryParam("position") String position) { + @QueryParam("limit") String limit, @QueryParam("position") String position, + @Context UriInfo uriInfo) { String region = regionResolver.resolveRegion(headers); - PaginatedResult page = service.getRequestValidatorPage(region, apiId, pageLimit(limit), position); + PaginatedResult page = service.getRequestValidatorPage(region, apiId, pageLimit(limit), + requestedPosition(position, uriInfo)); return pageResponse(page, this::toRequestValidatorNode); } @@ -1191,11 +1201,12 @@ public class ApiGatewayController { @GET @Path("/restapis/{apiId}/models") public Response getModels(@Context HttpHeaders headers, @PathParam("apiId") String apiId, - @QueryParam("limit") String limit, @QueryParam("position") String position) { + @QueryParam("limit") String limit, @QueryParam("position") String position, + @Context UriInfo uriInfo) { String region = regionResolver.resolveRegion(headers); // Model clashes with the imported API Gateway v2 model PaginatedResult page = - service.getModelPage(region, apiId, pageLimit(limit), position); + service.getModelPage(region, apiId, pageLimit(limit), requestedPosition(position, uriInfo)); return pageResponse(page, this::toModelNode); } @@ -1261,10 +1272,11 @@ public class ApiGatewayController { @QueryParam("type") String type, @QueryParam("name") String name, @QueryParam("path") String path, @QueryParam("locationStatus") String locationStatus, - @QueryParam("limit") String limit, @QueryParam("position") String position) { + @QueryParam("limit") String limit, @QueryParam("position") String position, + @Context UriInfo uriInfo) { String region = regionResolver.resolveRegion(headers); PaginatedResult page = service.getDocumentationParts(region, apiId, type, name, - path, locationStatus, pageLimit(limit), position); + path, locationStatus, pageLimit(limit), requestedPosition(position, uriInfo)); return pageResponse(page, part -> objectMapper.valueToTree(part)); } @@ -1373,9 +1385,11 @@ public class ApiGatewayController { @GET @Path("/domainnames/{domainName}/basepathmappings") public Response getBasePathMappings(@Context HttpHeaders headers, @PathParam("domainName") String domainName, - @QueryParam("limit") String limit, @QueryParam("position") String position) { + @QueryParam("limit") String limit, @QueryParam("position") String position, + @Context UriInfo uriInfo) { String region = regionResolver.resolveRegion(headers); - PaginatedResult page = service.getBasePathMappingsPage(region, domainName, limit, position); + PaginatedResult page = service.getBasePathMappingsPage(region, domainName, limit, + requestedPosition(position, uriInfo)); return pageResponse(page, this::toMappingNode); } @@ -1650,11 +1664,12 @@ public class ApiGatewayController { @GET @Path("/vpclinks") public Response getV1VpcLinks(@Context HttpHeaders headers, - @QueryParam("limit") String limit, @QueryParam("position") String position) { + @QueryParam("limit") String limit, @QueryParam("position") String position, + @Context UriInfo uriInfo) { String region = regionResolver.resolveRegion(headers); // VpcLink clashes with the imported API Gateway v2 model PaginatedResult page = - service.getVpcLinkPage(region, pageLimit(limit), position); + service.getVpcLinkPage(region, pageLimit(limit), requestedPosition(position, uriInfo)); return pageResponse(page, this::toV1VpcLinkNode); } 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 5fc08da3fe..ff52eeb9e1 100644 --- a/src/main/java/io/floci/aws/services/apigateway/ApiGatewayService.java +++ b/src/main/java/io/floci/aws/services/apigateway/ApiGatewayService.java @@ -527,11 +527,7 @@ public class ApiGatewayService implements ResourceProvider, Resettable, PassedRo return apiStore.scan(k -> k.startsWith(prefix)); } - /** AWS answers an explicitly empty {@code position}, and Floci a blank one, with a BadRequestException instead of the first page. */ public PaginatedResult getRestApiPage(String region, Integer limit, String position) { - if (position != null && position.isBlank()) { - throw new AwsException("BadRequestException", "Invalid position parameter", 400); - } return pageById(getRestApis(region), RestApi::getId, limit, position); } @@ -1324,14 +1320,15 @@ public class ApiGatewayService implements ResourceProvider, Resettable, PassedRo /** * 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. + * an unrecognised token is a {@code BadRequestException}, as on AWS. So is an explicitly empty + * {@code position}, which AWS refuses instead of serving the first page, and a blank one. */ private static PaginatedResult pageById(List items, Function idOf, Integer limit, String position) { List sorted = items.stream().sorted(Comparator.comparing(idOf)).toList(); int pageSize = resolvePageSize(limit); int start = 0; - if (position != null && !position.isBlank()) { + if (position != null) { while (start < sorted.size() && !position.equals(idOf.apply(sorted.get(start)))) { start++; } diff --git a/src/test/java/io/floci/aws/services/apigateway/ApiGatewayRestApiPagingIntegrationTest.java b/src/test/java/io/floci/aws/services/apigateway/ApiGatewayRestApiPagingIntegrationTest.java index dc29d2671a..3e8776e401 100644 --- a/src/test/java/io/floci/aws/services/apigateway/ApiGatewayRestApiPagingIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/apigateway/ApiGatewayRestApiPagingIntegrationTest.java @@ -65,13 +65,45 @@ class ApiGatewayRestApiPagingIntegrationTest { assertThrows(BadRequestException.class, () -> client.getRestApis(r -> r.limit(1).position("missing"))); - given().queryParam("position", "").when().get("/restapis").then().statusCode(400) - .body("__type", equalTo("BadRequestException")); - given().queryParam("position", " ").when().get("/restapis").then().statusCode(400) - .body("__type", equalTo("BadRequestException")); } finally { created.forEach(id -> client.deleteRestApi(r -> r.restApiId(id))); } } } + + @Test + void everyCollectionRefusesAnEmptyOrBlankPosition() { + try (ApiGatewayClient client = client()) { + String apiId = client.createRestApi(r -> r.name("BlankPositionApi")).id(); + String planId = client.createUsagePlan(r -> r.name("BlankPositionPlan")).id(); + String domainName = "blank-position.example.com"; + client.createDomainName(r -> r.domainName(domainName)); + try { + List collections = List.of( + "/restapis", + "/restapis/" + apiId + "/resources", + "/restapis/" + apiId + "/deployments", + "/restapis/" + apiId + "/authorizers", + "/restapis/" + apiId + "/requestvalidators", + "/restapis/" + apiId + "/models", + "/restapis/" + apiId + "/documentation/parts", + "/usageplans", + "/usageplans/" + planId + "/keys", + "/domainnames/" + domainName + "/basepathmappings", + "/vpclinks"); + for (String collection : collections) { + for (String position : List.of("", " ")) { + given().queryParam("position", position).when().get(collection).then().statusCode(400) + .body("__type", equalTo("BadRequestException")) + .body("message", equalTo("Invalid position parameter")); + } + given().when().get(collection).then().statusCode(200); + } + } finally { + client.deleteDomainName(r -> r.domainName(domainName)); + client.deleteUsagePlan(r -> r.usagePlanId(planId)); + client.deleteRestApi(r -> r.restApiId(apiId)); + } + } + } }