fix(apigateway): honor import warnings and strict rollback (#4531)

This commit is contained in:
Aniket Shukla authored and GitHub committed 2026-09-27 18:22:48 -04:00
1 parent 98e1cca8a5
commit b9efda8606
6 files changed
+257 -8

No files matched your search

@@ -0,0 +1,79 @@
"""OpenAPI warnings and failOnWarnings through the API Gateway v1 SDK."""
import json
import uuid
import boto3
import pytest
from botocore.exceptions import ClientError
@pytest.fixture
def client(aws_config):
api = boto3.client("apigateway", **aws_config)
try:
yield api
finally:
api.close()
def definition(title, warning=True, any_method=False):
operation = {"responses": {"200": {"description": "ok"}}}
if warning:
operation["security"] = "not-an-array"
return json.dumps({
"openapi": "3.0.1", "info": {"title": title, "version": "1.0"},
"paths": {"/p": {"x-amazon-apigateway-any-method" if any_method else "get": operation}},
}).encode()
@pytest.mark.parametrize("any_method", [False, True])
def test_lenient_import_returns_warnings(client, any_method):
imported = client.import_rest_api(
body=definition(f"warn-{uuid.uuid4().hex}", any_method=any_method))
try:
assert any("security" in warning for warning in imported["warnings"])
readback = client.get_rest_api(restApiId=imported["id"])
assert readback["warnings"] == imported["warnings"]
finally:
client.delete_rest_api(restApiId=imported["id"])
@pytest.mark.parametrize("any_method", [False, True])
def test_strict_import_creates_nothing(client, any_method):
title = f"reject-{uuid.uuid4().hex}"
with pytest.raises(ClientError) as error:
client.import_rest_api(body=definition(title, any_method=any_method), failOnWarnings=True)
assert error.value.response["Error"]["Code"] == "BadRequestException"
assert "security" in error.value.response["Error"]["Message"]
assert title not in [api["name"] for api in client.get_rest_apis(limit=500)["items"]]
def test_strict_overwrite_preserves_resources_and_metadata(client):
title = f"kept-{uuid.uuid4().hex}"
imported = client.import_rest_api(body=definition(title, warning=False), failOnWarnings=True)
api_id = imported["id"]
try:
before = client.get_resources(restApiId=api_id)["items"]
with pytest.raises(ClientError) as error:
client.put_rest_api(restApiId=api_id, mode="overwrite", failOnWarnings=True,
body=definition("rejected"))
assert error.value.response["Error"]["Code"] == "BadRequestException"
assert client.get_rest_api(restApiId=api_id)["name"] == title
assert client.get_resources(restApiId=api_id)["items"] == before
finally:
client.delete_rest_api(restApiId=api_id)
def test_lenient_overwrite_returns_warnings_then_clean_import_clears_them(client):
imported = client.import_rest_api(body=definition(f"clean-{uuid.uuid4().hex}", warning=False))
api_id = imported["id"]
try:
updated = client.put_rest_api(restApiId=api_id, mode="overwrite", failOnWarnings=False,
body=definition("warning"))
assert any("security" in warning for warning in updated["warnings"])
client.put_rest_api(restApiId=api_id, mode="overwrite", failOnWarnings=True,
body=definition("clean", warning=False))
assert not client.get_rest_api(restApiId=api_id).get("warnings")
finally:
client.delete_rest_api(restApiId=api_id)
+6
View File
@@ -52,6 +52,12 @@ duplicate override IDs.
**Protocol:** REST JSON
**Endpoint:** `http://localhost:4566/restapis/...`
`ImportRestApi` and `PutRestApi` return parser warnings in the REST API's `warnings` array.
Set the lowercase query parameter `failonwarnings=true` (SDK: `failOnWarnings`) to reject
a warning-bearing definition with `BadRequestException` before creating or overwriting
the API. The default is `false`. A successful import with no warnings clears any prior
warnings; malformed definitions and fatal import errors remain errors in either mode.
### Supported Operations
| Category | Operations |
@@ -321,10 +321,11 @@ public class ApiGatewayController {
@Consumes(MediaType.WILDCARD)
public Response createRestApi(@Context HttpHeaders headers,
@QueryParam("mode") String mode,
@QueryParam("failonwarnings") boolean failOnWarnings,
String body) {
String region = regionResolver.resolveRegion(headers);
if ("import".equals(mode)) {
RestApi api = service.importRestApi(region, body);
RestApi api = service.importRestApi(region, body, failOnWarnings);
return Response.status(201).entity(toApiNode(region, api).toString()).type(MediaType.APPLICATION_JSON).build();
}
try {
@@ -343,9 +344,10 @@ public class ApiGatewayController {
public Response putRestApi(@Context HttpHeaders headers,
@PathParam("apiId") String apiId,
@QueryParam("mode") String mode,
@QueryParam("failonwarnings") boolean failOnWarnings,
String body) {
String region = regionResolver.resolveRegion(headers);
RestApi api = service.putRestApi(region, apiId, mode, body);
RestApi api = service.putRestApi(region, apiId, mode, body, failOnWarnings);
return Response.ok(toApiNode(region, api).toString()).type(MediaType.APPLICATION_JSON).build();
}
@@ -2104,6 +2106,10 @@ public class ApiGatewayController {
if (api.getDescription() != null) node.put("description", api.getDescription());
node.put("createdDate", api.getCreatedDate());
node.put("apiStatus", "AVAILABLE");
if (api.getWarnings() != null && !api.getWarnings().isEmpty()) {
ArrayNode warnings = node.putArray("warnings");
api.getWarnings().forEach(warnings::add);
}
if (api.getTags() != null && !api.getTags().isEmpty()) {
ObjectNode tagsNode = objectMapper.createObjectNode();
api.getTags().forEach(tagsNode::put);
@@ -2638,7 +2638,12 @@ public class ApiGatewayService {
// ──────────────────────────── OpenAPI Import ────────────────────────────
public RestApi importRestApi(String region, String specBody) {
OpenAPI openAPI = parseOpenApiSpec(specBody);
return importRestApi(region, specBody, false);
}
public RestApi importRestApi(String region, String specBody, boolean failOnWarnings) {
ParsedOpenApi parsed = parseOpenApiSpec(specBody, failOnWarnings);
OpenAPI openAPI = parsed.openAPI();
String name = openAPI.getInfo() != null ? openAPI.getInfo().getTitle() : "Imported API";
String description = openAPI.getInfo() != null ? openAPI.getInfo().getDescription() : null;
@@ -2650,6 +2655,8 @@ public class ApiGatewayService {
try {
applyOpenApiSpec(region, api.getId(), openAPI);
api.setWarnings(parsed.warnings());
apiStore.put(apiKey(region, api.getId()), api);
} catch (RuntimeException e) {
// AWS creates nothing when an import fails, and the error carries no id to clean up with.
restoreRestApi(region, api.getId(), RestApiSnapshot.empty());
@@ -2660,12 +2667,19 @@ public class ApiGatewayService {
}
public RestApi putRestApi(String region, String apiId, String mode, String specBody) {
return putRestApi(region, apiId, mode, specBody, false);
}
public RestApi putRestApi(String region, String apiId, String mode, String specBody, boolean failOnWarnings) {
// Note: mode=merge is accepted but treated as overwrite (merge semantics not yet implemented)
getRestApi(region, apiId);
OpenAPI openAPI = parseOpenApiSpec(specBody);
ParsedOpenApi parsed = parseOpenApiSpec(specBody, failOnWarnings);
RestApiSnapshot snapshot = snapshotRestApi(region, apiId);
try {
return overwriteRestApi(region, apiId, openAPI);
RestApi api = overwriteRestApi(region, apiId, parsed.openAPI());
api.setWarnings(parsed.warnings());
apiStore.put(apiKey(region, apiId), api);
return api;
} catch (RuntimeException e) {
// A failed PutRestApi leaves the API as it was; the overwrite has already cleared it by now.
restoreRestApi(region, apiId, snapshot);
@@ -2766,7 +2780,9 @@ public class ApiGatewayService {
entries.forEach(store::put);
}
private OpenAPI parseOpenApiSpec(String specBody) {
private record ParsedOpenApi(OpenAPI openAPI, List<String> warnings) {}
private ParsedOpenApi parseOpenApiSpec(String specBody, boolean failOnWarnings) {
SwaggerParseResult result = new io.swagger.parser.OpenAPIParser().readContents(specBody, null, null);
if (result.getOpenAPI() == null) {
String errors = result.getMessages() != null ? String.join(", ", result.getMessages()) : "unknown error";
@@ -2774,7 +2790,23 @@ public class ApiGatewayService {
}
OpenAPI openAPI = result.getOpenAPI();
validateImportedAuthorizers(openAPI);
return openAPI;
List<String> warnings = result.getMessages() != null ? new ArrayList<>(result.getMessages()) : new ArrayList<>();
if (openAPI.getPaths() != null) {
for (Map.Entry<String, PathItem> entry : openAPI.getPaths().entrySet()) {
PathItem pathItem = entry.getValue();
Object anyMethod = pathItem.getExtensions() != null
? pathItem.getExtensions().get("x-amazon-apigateway-any-method") : null;
if (anyMethod != null) {
OpenAPIDeserializer.ParseResult anyResult = new OpenAPIDeserializer.ParseResult();
parseAnyMethodOperation(entry.getKey(), anyMethod, anyResult);
warnings.addAll(anyResult.getMessages());
}
}
}
if (failOnWarnings && !warnings.isEmpty()) {
throw new AwsException("BadRequestException", String.join("; ", warnings), 400);
}
return new ParsedOpenApi(openAPI, List.copyOf(warnings));
}
private void validateImportedAuthorizers(OpenAPI openAPI) {
@@ -3115,6 +3147,11 @@ public class ApiGatewayService {
* swagger model graph, which the native image does not have.
*/
private Operation parseAnyMethodOperation(String path, Object anyMethodExt) {
return parseAnyMethodOperation(path, anyMethodExt, new OpenAPIDeserializer.ParseResult());
}
private Operation parseAnyMethodOperation(String path, Object anyMethodExt,
OpenAPIDeserializer.ParseResult result) {
JsonNode node = Json.mapper().valueToTree(anyMethodExt);
if (!(node instanceof ObjectNode operationNode)) {
throw new AwsException("BadRequestException",
@@ -3123,7 +3160,7 @@ public class ApiGatewayService {
400);
}
return new OpenAPIDeserializer().getOperation(operationNode,
"paths.'" + path + "'.x-amazon-apigateway-any-method", new OpenAPIDeserializer.ParseResult());
"paths.'" + path + "'.x-amazon-apigateway-any-method", result);
}
private void applyOperation(String region, String apiId, String resourceId, String httpMethod,
@@ -24,6 +24,15 @@ public class RestApi {
private List<String> binaryMediaTypes = new ArrayList<>();
/** Resource policy document, stored exactly as the caller sent it. */
private String policy;
private List<String> warnings = new ArrayList<>();
public List<String> getWarnings() {
return warnings;
}
public void setWarnings(List<String> warnings) {
this.warnings = warnings != null ? new ArrayList<>(warnings) : new ArrayList<>();
}
public String getPolicy() {
return policy;
@@ -0,0 +1,112 @@
package io.github.hectorvent.floci.services.apigateway;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.Test;
import java.util.List;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
@QuarkusTest
class ApiGatewayOpenApiWarningsIntegrationTest {
private static final String SPEC = """
{"openapi":"3.0.1","info":{"title":"WarningsAPI","version":"1.0"},
"paths":{"/p":{"get":{"security":"not-an-array",
"responses":{"200":{"description":"ok"}}}}}}
""";
private static final String CLEAN_SPEC = SPEC.replace("\"security\":\"not-an-array\",", "");
@Test
void lenientImportReturnsAndPersistsParserWarnings() {
String id = given().queryParam("mode", "import").body(SPEC).post("/restapis")
.then().statusCode(201).body("warnings", hasItem(containsString("security")))
.extract().path("id");
try {
given().get("/restapis/" + id).then().statusCode(200)
.body("warnings", hasItem(containsString("security")));
} finally {
given().delete("/restapis/" + id).then().statusCode(202);
}
}
@Test
void strictImportDoesNotCreateAnApi() {
List<String> before = apiIds();
given().queryParam("mode", "import").queryParam("failonwarnings", true)
.body(SPEC).post("/restapis").then().statusCode(400)
.body("__type", containsString("BadRequestException"))
.body("message", containsString("security"));
assertEquals(before, apiIds());
}
@Test
void strictOverwritePreservesApiMetadataAndResources() {
String id = importCleanApi();
try {
String resourcesBefore = given().get("/restapis/" + id + "/resources")
.then().statusCode(200).extract().asString();
given().queryParam("mode", "overwrite").queryParam("failonwarnings", true)
.body(SPEC.replace("WarningsAPI", "RejectedAPI"))
.put("/restapis/" + id).then().statusCode(400);
given().get("/restapis/" + id).then().statusCode(200)
.body("name", not("RejectedAPI"));
assertEquals(resourcesBefore, given().get("/restapis/" + id + "/resources")
.then().statusCode(200).extract().asString());
} finally {
given().delete("/restapis/" + id).then().statusCode(202);
}
}
@Test
void lenientOverwriteReturnsWarningsAndCleanOverwriteClearsThem() {
String id = importCleanApi();
try {
given().queryParam("mode", "overwrite").queryParam("failonwarnings", false)
.body(SPEC).put("/restapis/" + id).then().statusCode(200)
.body("warnings", hasItem(containsString("security")));
given().queryParam("mode", "overwrite").queryParam("failonwarnings", true)
.body(CLEAN_SPEC).put("/restapis/" + id).then().statusCode(200);
List<String> warnings = given().get("/restapis/" + id).then().statusCode(200)
.extract().path("warnings");
assertFalse(warnings != null && !warnings.isEmpty());
} finally {
given().delete("/restapis/" + id).then().statusCode(202);
}
}
private static String importCleanApi() {
return given().queryParam("mode", "import").queryParam("failonwarnings", true)
.body(CLEAN_SPEC).post("/restapis").then().statusCode(201)
.body("id", notNullValue()).extract().path("id");
}
private static List<String> apiIds() {
return given().get("/restapis").then().statusCode(200).extract().path("item.id");
}
@Test
void anyMethodWarningsAreAlsoReturned() {
String id = given().queryParam("mode", "import")
.body(SPEC.replace("\"get\"", "\"x-amazon-apigateway-any-method\""))
.post("/restapis").then().statusCode(201)
.body("warnings", hasItem(containsString("security"))).extract().path("id");
given().delete("/restapis/" + id).then().statusCode(202);
}
@Test
void strictAnyMethodImportDoesNotCreateAnApi() {
List<String> before = apiIds();
given().queryParam("mode", "import").queryParam("failonwarnings", true)
.body(SPEC.replace("\"get\"", "\"x-amazon-apigateway-any-method\""))
.post("/restapis").then().statusCode(400)
.body("message", containsString("security"));
assertEquals(before, apiIds());
}
}