fix(apigateway): import x-amazon-apigateway-any-method in the native image and roll back failed imports (#4473)

* fix(apigateway): import x-amazon-apigateway-any-method in the native image and roll back failed imports

The ANY pseudo-operation reaches the service as an untyped extension map, and it was
bound to io.swagger.v3.oas.models.Operation with Jackson bean deserialization. The
swagger model graph is not registered for reflection, so in the native image every
ImportRestApi or PutRestApi body with x-amazon-apigateway-any-method failed with
BadRequestException. Parse it with the swagger parser's own Operation reader, the one
that already builds the real verbs, which needs no reflection.

A failed import also left state behind: ImportRestApi kept a half-built API whose id the
caller never received, and PutRestApi left the API cleared and partly rebuilt. ImportRestApi
now deletes what it created and PutRestApi restores the API-scoped state it replaced.

* chore(apigateway): import swagger Json instead of qualifying it inline
This commit is contained in:
Abhigya authored and GitHub committed 2026-09-26 14:03:30 -04:00
1 parent f3dde75c0b
commit a5ded3e162
3 files changed
+465 -20

No files matched your search

@@ -11,6 +11,8 @@ import org.junit.jupiter.api.TestMethodOrder;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
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.assertNotNull;
@@ -171,8 +173,8 @@ class ApiGatewayOpenApiAnyMethodImportTest {
@Order(6)
void import_withMalformedAnyMethodExtension_returnsCleanBadRequest() {
// "x-amazon-apigateway-any-method" set to a plain string instead of an operation object.
// Json.mapper().convertValue(..., Operation.class) can't coerce this and must not be allowed
// to escape as an uncaught IllegalArgumentException (which would surface as a raw 500).
// It is not an Operation object and must be rejected as a BadRequestException, not escape as
// an uncaught exception (which would surface as a raw 500).
String spec = """
{
"openapi": "3.0.1",
@@ -237,4 +239,179 @@ class ApiGatewayOpenApiAnyMethodImportTest {
.statusCode(400)
.body("__type", equalTo("BadRequestException"));
}
@Test
@Order(8)
void importedAnyMethod_keepsPathParametersAndIntegrationRequestParameters() throws Exception {
String spec = """
{
"openapi": "3.0.1",
"info": {"title": "AnyMethodProxyAPI", "version": "1.0"},
"paths": {
"/p/{proxy+}": {
"x-amazon-apigateway-any-method": {
"parameters": [
{"name": "proxy", "in": "path", "required": true, "schema": {"type": "string"}}
],
"security": [],
"x-amazon-apigateway-integration": {
"type": "http_proxy",
"httpMethod": "ANY",
"uri": "http://example.com/{proxy}",
"requestParameters": {
"integration.request.path.proxy": "method.request.path.proxy"
}
}
}
}
}
}
""";
String proxyApiId = given()
.contentType(ContentType.JSON)
.queryParam("mode", "import")
.body(spec)
.when().post("/restapis")
.then()
.statusCode(201)
.extract().path("id");
try {
String resourceId = resourceIdForPath(proxyApiId, "/p/{proxy+}");
given()
.when().get("/restapis/" + proxyApiId + "/resources/" + resourceId + "/methods/ANY")
.then()
.statusCode(200)
.body("httpMethod", equalTo("ANY"))
.body("requestParameters.'method.request.path.proxy'", equalTo(true));
given()
.when().get("/restapis/" + proxyApiId + "/resources/" + resourceId + "/methods/ANY/integration")
.then()
.statusCode(200)
.body("uri", equalTo("http://example.com/{proxy}"))
.body("requestParameters.'integration.request.path.proxy'",
equalTo("method.request.path.proxy"));
} finally {
given().when().delete("/restapis/" + proxyApiId).then().statusCode(202);
}
}
@Test
@Order(9)
void failedImport_leavesNoRestApiBehind() {
String spec = """
{
"openapi": "3.0.1",
"info": {"title": "FailedImportLeavesNothingAPI", "version": "1.0"},
"paths": {
"/p/{proxy+}": {
"x-amazon-apigateway-any-method": "not-an-operation-object"
}
}
}
""";
given()
.contentType(ContentType.JSON)
.queryParam("mode", "import")
.body(spec)
.when().post("/restapis")
.then()
.statusCode(400)
.body("__type", equalTo("BadRequestException"));
given()
.when().get("/restapis")
.then()
.statusCode(200)
.body("item.name", not(hasItem("FailedImportLeavesNothingAPI")));
}
@Test
@Order(10)
void failedPutRestApi_leavesExistingApiUnchanged() throws Exception {
String original = """
{
"openapi": "3.0.1",
"info": {"title": "PutRollbackAPI", "version": "1.0"},
"paths": {
"/kept": {
"get": {
"x-amazon-apigateway-integration": {
"type": "http_proxy", "httpMethod": "GET", "uri": "http://example.com/kept"
}
}
}
}
}
""";
String broken = """
{
"openapi": "3.0.1",
"info": {"title": "PutRollbackAPIRenamed", "version": "1.0"},
"paths": {
"/p/{proxy+}": {
"x-amazon-apigateway-any-method": "not-an-operation-object"
}
}
}
""";
String putApiId = given()
.contentType(ContentType.JSON)
.queryParam("mode", "import")
.body(original)
.when().post("/restapis")
.then()
.statusCode(201)
.extract().path("id");
try {
String keptResourceId = resourceIdForPath(putApiId, "/kept");
given()
.contentType(ContentType.JSON)
.queryParam("mode", "overwrite")
.body(broken)
.when().put("/restapis/" + putApiId)
.then()
.statusCode(400)
.body("__type", equalTo("BadRequestException"));
given()
.when().get("/restapis/" + putApiId)
.then()
.statusCode(200)
.body("name", equalTo("PutRollbackAPI"));
given()
.when().get("/restapis/" + putApiId + "/resources")
.then()
.statusCode(200)
.body("item.path", not(hasItem("/p")))
.body("item.path", not(hasItem("/p/{proxy+}")));
assertEquals(keptResourceId, resourceIdForPath(putApiId, "/kept"));
given()
.when().get("/restapis/" + putApiId + "/resources/" + keptResourceId + "/methods/GET/integration")
.then()
.statusCode(200)
.body("uri", equalTo("http://example.com/kept"));
} finally {
given().when().delete("/restapis/" + putApiId).then().statusCode(202);
}
}
private static String resourceIdForPath(String restApiId, String path) throws Exception {
String body = given()
.when().get("/restapis/" + restApiId + "/resources")
.then()
.statusCode(200)
.extract().body().asString();
for (JsonNode resource : mapper.readTree(body).get("item")) {
if (path.equals(resource.get("path").asText())) {
return resource.get("id").asText();
}
}
throw new AssertionError("No resource with path " + path + " in " + body);
}
}