fix(apigateway): validate imported models in their deployment context (#5645)

* fix(apigateway): resolve imported request model references

Signed-off-by: Andre Kurait <akurait@amazon.com>

* fix(apigateway): snapshot request model schemas for deployments

Signed-off-by: Andre Kurait <akurait@amazon.com>

* fix(apigateway): match the default body validation error

Signed-off-by: Andre Kurait <akurait@amazon.com>

* test(apigateway): expect the AWS default validation message

Signed-off-by: Andre Kurait <akurait@amazon.com>

* fix(apigateway): address model snapshot review findings

Signed-off-by: Andre Kurait <akurait@amazon.com>

* fix(apigateway): resolve model URLs from deployment snapshots

Signed-off-by: Andre Kurait <akurait@amazon.com>

* test(apigateway): make model reference cases collectable

Signed-off-by: Andre Kurait <akurait@amazon.com>

* fix(apigateway): accept canonical model URLs in every partition

Signed-off-by: Andre Kurait <akurait@amazon.com>

---------

Signed-off-by: Andre Kurait <akurait@amazon.com>
This commit is contained in:
Andre Kurait authored and GitHub committed 2026-10-15 06:50:46 -04:00
1 parent 6f78083080
commit 5a72b4f561
6 files changed
+508 -17

No files matched your search

@@ -0,0 +1,98 @@
"""Request validation for imported, recursive sibling models."""
import http.client
import json
import time
import urllib.parse
import uuid
from contextlib import closing
import boto3
import pytest
from botocore.config import Config
@pytest.mark.parametrize("version", ["openapi", "swagger"])
@pytest.mark.parametrize("model_reference", ["local", "url"])
def test_imported_model_references(aws_config, version, model_reference):
prefix = "#/components/schemas/" if version == "openapi" else "#/definitions/"
schemas = {
"Status": {"type": "string", "enum": ["READY"]},
"Request": {"type": "object", "required": ["status"], "properties": {
"status": {"$ref": prefix + "Status"}, "child": {"$ref": prefix + "Request"}}},
}
operation = {
"responses": {"200": {"description": "accepted"}},
"x-amazon-apigateway-request-validator": "body",
"x-amazon-apigateway-integration": {
"type": "mock", "requestTemplates": {"application/json": '{"statusCode":200}'},
"responses": {"default": {"statusCode": "200",
"responseTemplates": {"application/json": '{"accepted":true}'}}}},
}
document = {
"info": {"title": "model-references-" + uuid.uuid4().hex, "version": "1"},
"paths": {"/status": {"post": operation}},
"x-amazon-apigateway-request-validators": {
"body": {"validateRequestBody": True, "validateRequestParameters": False}},
}
if version == "openapi":
document["openapi"] = "3.0.1"
document["components"] = {"schemas": schemas}
operation["requestBody"] = {
"content": {"application/json": {"schema": {"$ref": prefix + "Request"}}}}
else:
document["swagger"] = "2.0"
document["consumes"] = ["application/json"]
document["produces"] = ["application/json"]
document["definitions"] = schemas
operation["parameters"] = [{"name": "body", "in": "body", "required": True,
"schema": {"$ref": prefix + "Request"}}]
with closing(boto3.client("apigateway", config=Config(retries={"max_attempts": 10}),
**aws_config)) as api:
api_id = api.import_rest_api(body=json.dumps(document), failOnWarnings=True,
parameters={"endpointConfigurationTypes": "REGIONAL"})["id"]
try:
if model_reference == "url":
reference = f"https://apigateway.amazonaws.com/restapis/{api_id}/models/"
request_schema = json.dumps(schemas["Request"]).replace(prefix, reference)
api.update_model(restApiId=api_id, modelName="Request", patchOperations=[
{"op": "replace", "path": "/schema", "value": request_schema}])
hostname = f"{api_id}.execute-api.{api.meta.region_name}.amazonaws.com"
target = urllib.parse.urlsplit(aws_config.get("endpoint_url") or "https://" + hostname)
connection_type = (http.client.HTTPSConnection if target.scheme == "https"
else http.client.HTTPConnection)
def invoke(body):
with closing(connection_type(target.hostname, target.port, timeout=30)) as connection:
connection.request("POST", "/probe/status", body=json.dumps(body),
headers={"Host": hostname, "Content-Type": "application/json"})
response = connection.getresponse()
return response.status, json.loads(response.read())
def await_response(body, expected):
deadline = time.monotonic() + 60
while True:
actual = invoke(body)
if actual == expected or time.monotonic() >= deadline:
assert actual == expected
return
time.sleep(1)
api.create_deployment(restApiId=api_id, stageName="probe")
valid = {"status": "READY"}
await_response(valid, (200, {"accepted": True}))
assert invoke({"status": "READY", "child": valid}) == (200, {"accepted": True})
for invalid in [{}, {"status": "INVALID"}, {"status": "READY", "child": {"status": "INVALID"}}]:
assert invoke(invalid) == (400, {"message": "Invalid request body"})
api.update_model(restApiId=api_id, modelName="Status", patchOperations=[
{"op": "replace", "path": "/schema", "value": '{"type":"string","enum":["DONE"]}'}])
assert invoke(valid) == (200, {"accepted": True})
assert invoke({"status": "DONE"}) == (400, {"message": "Invalid request body"})
# AWS applies model changes to the data plane after redeployment and propagation.
api.create_deployment(restApiId=api_id, stageName="probe")
await_response({"status": "DONE"}, (200, {"accepted": True}))
await_response(valid, (400, {"message": "Invalid request body"}))
finally:
api.delete_rest_api(restApiId=api_id)
+15
View File
@@ -270,6 +270,21 @@ handling; a hand-rolled signer must include it.
> signed, known caller is let through. `principalOrgId` and `cognitoIdentity` are always null -
> Organizations membership and identity-pool federation are not modelled.
### Request Model Validation
Imported OpenAPI and Swagger request models can reference sibling models, including recursive
references. Body validation resolves these schemas within the same API while preserving inline
definitions. API Gateway model URLs (`https://apigateway.amazonaws.com/restapis/{api}/models/{name}`)
also resolve from that deployment's snapshot in every partition, including China.
Other schema URLs are not fetched.
Rejected bodies return `Invalid request body` by default. Custom gateway responses can access
the schema validation detail through `$context.error.validationErrorString`.
Each deployment snapshots its model schemas. Updating or deleting a model does not change
validation for an existing deployment; deploy again to apply those changes. Deployments saved
before model snapshots were introduced retain their previous behavior until redeployed.
### Gateway Responses
A REST API's gateway responses customise what the gateway itself answers when it, rather than
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.apigateway;
// REST and v2 both define Authorizer; the less-used REST type is qualified below.
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -12,6 +13,9 @@ import com.jayway.jsonpath.InvalidPathException;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.spi.json.JacksonJsonNodeJsonProvider;
import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider;
import com.networknt.schema.JsonSchema;
import com.networknt.schema.JsonSchemaFactory;
import com.networknt.schema.SpecVersion;
import com.networknt.schema.ValidationMessage;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsEndpoints;
@@ -54,6 +58,7 @@ import jakarta.ws.rs.*;
import jakarta.ws.rs.core.*;
import org.jboss.logging.Logger;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URLDecoder;
@@ -1163,31 +1168,43 @@ public class ApiGatewayExecuteController {
if (modelName != null) {
try {
io.github.hectorvent.floci.services.apigateway.model.Model model =
apiGatewayService.getModel(region, apiId, modelName);
String schemaStr = model.getSchema();
Map<String, String> models = apiGatewayService.getDeploymentModelSchemas(region, apiId,
scope.stage() != null ? scope.stage().getDeploymentId() : null);
String schemaStr = models.get(modelName);
if (schemaStr != null && !schemaStr.isBlank()) {
String bodyStr = body != null ? new String(body, StandardCharsets.UTF_8) : "";
if (bodyStr.isBlank()) {
return gatewayResponse(scope, GatewayResponseType.BAD_REQUEST_BODY, 400,
"Invalid request body");
}
JsonNode schemaNode = objectMapper.readTree(schemaStr);
JsonNode schemaNode = modelValidationSchema(models, schemaStr);
JsonNode bodyNode = objectMapper.readTree(bodyStr);
com.networknt.schema.JsonSchemaFactory factory =
com.networknt.schema.JsonSchemaFactory.getInstance(
com.networknt.schema.SpecVersion.VersionFlag.V4);
com.networknt.schema.JsonSchema schema = factory.getSchema(schemaNode);
JsonSchemaFactory factory =
JsonSchemaFactory.getInstance(
SpecVersion.VersionFlag.V4, builder -> builder.schemaLoaders(
loaders -> loaders.add(iri -> {
String prefix = "https://apigateway.amazonaws.com/restapis/" // partition-literal: documented model URI uses this host in every partition
+ apiId + "/models/";
String reference = iri.toString();
String referencedSchema = reference.startsWith(prefix)
? models.get(reference.substring(prefix.length())) : null;
if (referencedSchema == null) {
throw new IllegalArgumentException(
"Model reference is not in this deployment");
}
return () -> new ByteArrayInputStream(
modelValidationSchema(models, referencedSchema)
.toString().getBytes(StandardCharsets.UTF_8));
})));
JsonSchema schema = factory.getSchema(schemaNode);
Set<ValidationMessage> errors = schema.validate(bodyNode);
if (!errors.isEmpty()) {
String errorMsg = errors.iterator().next().getMessage();
return gatewayResponse(scope, GatewayResponseType.BAD_REQUEST_BODY, 400,
"Invalid request body: " + errorMsg, errorMsg);
"Invalid request body", errorMsg);
}
}
} catch (AwsException e) {
// Model not found — skip body validation
} catch (Exception e) {
return gatewayResponse(scope, GatewayResponseType.BAD_REQUEST_BODY, 400,
"Invalid request body");
@@ -1199,6 +1216,30 @@ public class ApiGatewayExecuteController {
return null;
}
private JsonNode modelValidationSchema(Map<String, String> models, String schema) throws JsonProcessingException {
JsonNode root = objectMapper.readTree(schema);
if (!(root instanceof ObjectNode document)) {
return root;
}
if ((document.has("components") && !document.get("components").isObject())
|| (document.path("components").has("schemas")
&& !document.path("components").get("schemas").isObject())) {
return root;
}
ObjectNode definitions = document.withObject("/components/schemas");
for (Map.Entry<String, String> model : models.entrySet()) {
if (!definitions.has(model.getKey())) {
try {
definitions.set(model.getKey(), objectMapper.readTree(model.getValue()));
} catch (JsonProcessingException ignored) {
// An unrelated malformed model must not invalidate this request. If referenced,
// its absent definition is rejected by the schema validator itself.
}
}
}
return root;
}
private String toAuthorizerEvent(io.github.hectorvent.floci.services.apigateway.model.Authorizer auth,
HttpHeaders headers, String region, String apiId, String stageName,
String httpMethod, String requestPath,
@@ -109,6 +109,7 @@ public class ApiGatewayService implements ResourceProvider {
private final StorageBackend<String, RestApi> apiStore;
private final StorageBackend<String, ApiGatewayResource> resourceStore;
private final StorageBackend<String, Deployment> deploymentStore;
private final StorageBackend<String, Map<String, String>> deploymentModelStore;
private final StorageBackend<String, Stage> stageStore;
private final StorageBackend<String, Authorizer> authorizerStore;
private final StorageBackend<String, ApiKey> apiKeyStore;
@@ -185,6 +186,9 @@ public class ApiGatewayService implements ResourceProvider {
this.deploymentStore = storageFactory.create("apigateway", "apigateway-deployments.json",
new TypeReference<>() {
});
this.deploymentModelStore = storageFactory.create("apigateway", "apigateway-deployment-models.json",
new TypeReference<>() {
});
this.stageStore = storageFactory.create("apigateway", "apigateway-stages.json",
new TypeReference<>() {
});
@@ -415,6 +419,7 @@ public class ApiGatewayService implements ResourceProvider {
String prefix = region + "::" + apiId + "::";
resourceStore.keys().stream().filter(k -> k.startsWith(prefix)).forEach(resourceStore::delete);
deploymentStore.keys().stream().filter(k -> k.startsWith(prefix)).forEach(deploymentStore::delete);
deploymentModelStore.keys().stream().filter(k -> k.startsWith(prefix)).forEach(deploymentModelStore::delete);
stageStore.keys().stream().filter(k -> k.startsWith(prefix)).forEach(stageStore::delete);
modelStore.keys().stream().filter(k -> k.startsWith(prefix)).forEach(modelStore::delete);
requestValidatorStore.keys().stream().filter(k -> k.startsWith(prefix)).forEach(requestValidatorStore::delete);
@@ -787,15 +792,18 @@ public class ApiGatewayService implements ResourceProvider {
getRestApi(region, apiId);
String description = (String) request.getOrDefault("description", "");
String snapshot;
Map<String, String> schemas;
try {
snapshot = JSON.writeValueAsString(new DeploymentExportSnapshot(getRestApi(region, apiId),
getResources(region, apiId), getModels(region, apiId), getAuthorizers(region, apiId),
getRequestValidators(region, apiId), getGatewayResponses(region, apiId).stream()
.filter(response -> !response.isDefaultResponse()).toList()));
schemas = modelSchemas(JSON.readValue(snapshot, DeploymentExportSnapshot.class).models());
} catch (JsonProcessingException e) {
throw new IllegalStateException("Cannot capture REST API deployment", e);
}
Deployment deployment = new Deployment(shortId(10), description, System.currentTimeMillis() / 1000L, snapshot);
deploymentModelStore.put(deploymentKey(region, apiId, deployment.id()), schemas);
deploymentStore.put(deploymentKey(region, apiId, deployment.id()), deployment);
LOG.infov("Created deployment {0} for API {1}", deployment.id(), apiId);
@@ -850,6 +858,32 @@ public class ApiGatewayService implements ResourceProvider {
public void deleteDeployment(String region, String apiId, String deploymentId) {
getDeployment(region, apiId, deploymentId);
deploymentStore.delete(deploymentKey(region, apiId, deploymentId));
deploymentModelStore.delete(deploymentKey(region, apiId, deploymentId));
}
Map<String, String> getDeploymentModelSchemas(String region, String apiId, String deploymentId) {
if (deploymentId != null) {
Optional<Map<String, String>> schemas = deploymentModelStore.get(deploymentKey(region, apiId, deploymentId));
if (schemas.isPresent()) {
return schemas.get();
}
}
// Deployments persisted before model snapshots were introduced retain their existing behavior.
return currentModelSchemas(region, apiId);
}
private Map<String, String> currentModelSchemas(String region, String apiId) {
return modelSchemas(getModels(region, apiId));
}
private Map<String, String> modelSchemas(List<Model> models) {
Map<String, String> schemas = new HashMap<>();
for (Model model : models) {
if (model.getSchema() != null) {
schemas.put(model.getName(), model.getSchema());
}
}
return Map.copyOf(schemas);
}
public Deployment updateDeployment(String region, String apiId, String deploymentId, List<Map<String, String>> patchOperations) {
@@ -0,0 +1,110 @@
package io.github.hectorvent.floci.services.apigateway;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.config.TlsCertificateManager;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.common.ServiceConfigAccess;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.apigateway.model.Deployment;
import io.github.hectorvent.floci.services.apigateway.model.Model;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
class ApiGatewayDeploymentModelTest {
private static final String REGION = "us-east-1";
private static final String ORIGINAL = "{\"type\":\"string\",\"enum\":[\"READY\"]}";
private static final String UPDATED = "{\"type\":\"string\",\"enum\":[\"DONE\"]}";
private static final String SNAPSHOTS = "apigateway-deployment-models.json";
@Test
void deploymentValidationUsesTheSchemaCapturedForExport(@TempDir Path directory) throws Exception {
ApiGatewayService service = spy(service(directory));
String apiId = service.createRestApi(REGION, Map.of("name", "concurrent-model-update")).getId();
Model original = new Model();
original.setName("Status");
original.setSchema(ORIGINAL);
Model updated = new Model();
updated.setName("Status");
updated.setSchema(UPDATED);
doReturn(List.of(original), List.of(updated)).when(service).getModels(REGION, apiId);
Deployment deployment = service.createDeployment(REGION, apiId, Map.of());
assertEquals(ORIGINAL, new ObjectMapper().readTree(deployment.exportSnapshot())
.path("models").get(0).path("schema").asText());
assertEquals(Map.of("Status", ORIGINAL),
service.getDeploymentModelSchemas(REGION, apiId, deployment.id()));
}
@Test
void deploymentModelsSurviveUpdatesDeletionAndRestart(@TempDir Path directory) {
ApiGatewayService service = service(directory);
String apiId = service.createRestApi(REGION, Map.of("name", "snapshot")).getId();
service.createModel(REGION, apiId, Map.of("name", "Status", "schema", ORIGINAL));
String original = service.createDeployment(REGION, apiId, Map.of()).id();
service.updateModel(REGION, apiId, "Status",
List.of(Map.of("op", "replace", "path", "/schema", "value", UPDATED)));
ApiGatewayService restarted = service(directory);
assertEquals(UPDATED, restarted.getModel(REGION, apiId, "Status").getSchema());
assertEquals(Map.of("Status", ORIGINAL), restarted.getDeploymentModelSchemas(REGION, apiId, original));
String updated = restarted.createDeployment(REGION, apiId, Map.of()).id();
restarted.deleteModel(REGION, apiId, "Status");
assertEquals(Map.of("Status", ORIGINAL), restarted.getDeploymentModelSchemas(REGION, apiId, original));
assertEquals(Map.of("Status", UPDATED), restarted.getDeploymentModelSchemas(REGION, apiId, updated));
}
@Test
void emptySnapshotsDifferFromLegacyDeploymentsWithoutSnapshots(@TempDir Path directory) throws Exception {
ApiGatewayService service = service(directory);
String apiId = service.createRestApi(REGION, Map.of("name", "empty")).getId();
String deploymentId = service.createDeployment(REGION, apiId, Map.of()).id();
service.createModel(REGION, apiId, Map.of("name", "Status", "schema", ORIGINAL));
assertTrue(service.getDeploymentModelSchemas(REGION, apiId, deploymentId).isEmpty());
Files.delete(directory.resolve(SNAPSHOTS));
ApiGatewayService legacy = service(directory);
assertEquals(deploymentId, legacy.getDeployment(REGION, apiId, deploymentId).id());
assertEquals(Map.of("Status", ORIGINAL), legacy.getDeploymentModelSchemas(REGION, apiId, deploymentId));
}
@Test
void deletingDeploymentsAndApisRemovesTheirSnapshots(@TempDir Path directory) throws Exception {
ApiGatewayService service = service(directory);
String firstApi = service.createRestApi(REGION, Map.of("name", "first")).getId();
String secondApi = service.createRestApi(REGION, Map.of("name", "second")).getId();
String firstDeployment = service.createDeployment(REGION, firstApi, Map.of()).id();
service.createDeployment(REGION, secondApi, Map.of());
service.deleteDeployment(REGION, firstApi, firstDeployment);
assertEquals(1, new ObjectMapper().readTree(directory.resolve(SNAPSHOTS).toFile()).size());
service.deleteRestApi(REGION, secondApi);
assertTrue(new ObjectMapper().readTree(directory.resolve(SNAPSHOTS).toFile()).isEmpty());
}
private ApiGatewayService service(Path directory) {
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
when(config.storage().persistentPath()).thenReturn(directory.toString());
when(config.defaultAccountId()).thenReturn("000000000000");
ServiceConfigAccess access = mock(ServiceConfigAccess.class);
when(access.storageMode(anyString())).thenReturn("persistent");
return new ApiGatewayService(new StorageFactory(config, access), config,
mock(TlsCertificateManager.class), new RegionResolver(REGION, "000000000000"));
}
}
@@ -2,13 +2,21 @@ package io.github.hectorvent.floci.services.apigateway;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.sun.net.httpserver.HttpServer;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.http.ContentType;
import io.restassured.specification.RequestSpecification;
import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;
import static io.restassured.RestAssured.given;
@@ -1083,6 +1091,191 @@ class ApiGatewayOpenApiImportTest {
// ──────────────────────────── Request Body Validation ────────────────────────────
@ParameterizedTest
@ValueSource(strings = {"us-east-1", "cn-north-1", "us-gov-west-1"})
@Order(100)
void importedModelReferencesValidateDeployedSiblingModelsAndRecursiveBodies(String region) throws Exception {
RequestSpecification request = new RequestSpecBuilder().addHeader("Authorization",
"AWS4-HMAC-SHA256 Credential=test/20261015/" + region
+ "/apigateway/aws4_request, SignedHeaders=host, Signature=test").build();
String spec = """
{
"openapi":"3.0.1", "info":{"title":"ReferencedWorkflowModels","version":"1"},
"x-amazon-apigateway-request-validators":{
"full":{"validateRequestBody":true,"validateRequestParameters":true}},
"x-amazon-apigateway-request-validator":"full",
"paths":{"/list-workflows":{"post":{
"requestBody":{"content":{"application/json":{
"schema":{"$ref":"#/components/schemas/Request"}}}},
"x-amazon-apigateway-integration":{"type":"MOCK",
"requestTemplates":{"application/json":"{\\"statusCode\\":200}"},
"responses":{"default":{"statusCode":"200",
"responseTemplates":{"application/json":"{\\"accepted\\":true}"}}}}}}},
"components":{"schemas":{
"Category":{"type":"string","enum":["DMS"]},
"Status":{"type":"string","enum":["RUNNING"]},
"Request":{"type":"object","required":["workflowCategory","status"],
"properties":{
"workflowCategory":{"$ref":"#/components/schemas/Category"},
"status":{"$ref":"#/components/schemas/Status"},
"child":{"$ref":"#/components/schemas/Request"}}}}}
}
""";
String apiId = given().spec(request).contentType(ContentType.JSON).queryParam("mode", "import")
.body(spec).post("/restapis").then().statusCode(201).extract().path("id");
AtomicInteger integrationCalls = new AtomicInteger();
HttpServer integration = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
try {
integration.createContext("/accepted", exchange -> {
integrationCalls.incrementAndGet();
byte[] bytes = "{\"accepted\":true}".getBytes(StandardCharsets.UTF_8);
exchange.getResponseHeaders().set("Content-Type", "application/json");
exchange.sendResponseHeaders(200, bytes.length);
exchange.getResponseBody().write(bytes);
exchange.close();
});
integration.start();
String resourceId = given().spec(request).get("/restapis/" + apiId + "/resources")
.then().statusCode(200).extract().path("item.find { it.path == '/list-workflows' }.id");
given().spec(request).contentType(ContentType.JSON)
.body("{\"type\":\"HTTP_PROXY\",\"httpMethod\":\"POST\",\"uri\":\"http://127.0.0.1:"
+ integration.getAddress().getPort() + "/accepted\"}")
.put("/restapis/" + apiId + "/resources/" + resourceId + "/methods/POST/integration")
.then().statusCode(201);
String deployment = given().spec(request).contentType(ContentType.JSON).body("{}")
.post("/restapis/" + apiId + "/deployments")
.then().statusCode(201).extract().path("id");
given().spec(request).contentType(ContentType.JSON)
.body("{\"stageName\":\"test\",\"deploymentId\":\"" + deployment + "\"}")
.post("/restapis/" + apiId + "/stages").then().statusCode(201);
String endpoint = "/execute-api/" + apiId + "/test/list-workflows";
String valid = "{\"workflowCategory\":\"DMS\",\"status\":\"RUNNING\"}";
given().spec(request).contentType(ContentType.JSON).body(valid).post(endpoint)
.then().statusCode(200).body(containsString("accepted"));
given().spec(request).contentType(ContentType.JSON)
.body("{\"workflowCategory\":\"DMS\",\"status\":\"RUNNING\",\"child\":" + valid + "}")
.post(endpoint).then().statusCode(200);
assertEquals(2, integrationCalls.get(), "Valid requests must reach the integration");
assertRejectedWithoutIntegration(request, endpoint, "{\"status\":\"RUNNING\"}", integrationCalls);
assertRejectedWithoutIntegration(request, endpoint, valid.replace("RUNNING", "INVALID"), integrationCalls);
assertRejectedWithoutIntegration(request, endpoint,
"{\"workflowCategory\":\"DMS\",\"status\":\"RUNNING\",\"child\":"
+ valid.replace("RUNNING", "INVALID") + "}", integrationCalls);
String schema = "{\"type\":\"string\",\"enum\":[\"DONE\"]}";
String patch = "{\"patchOperations\":[{\"op\":\"replace\",\"path\":\"/schema\",\"value\":"
+ mapper.writeValueAsString(schema) + "}]}";
given().spec(request).contentType(ContentType.JSON).body(patch)
.patch("/restapis/" + apiId + "/models/Status").then().statusCode(200);
given().spec(request).contentType(ContentType.JSON).body(valid).post(endpoint).then().statusCode(200);
assertRejectedWithoutIntegration(request, endpoint, valid.replace("RUNNING", "DONE"), integrationCalls);
given().spec(request).contentType(ContentType.JSON).body("{\"stageName\":\"test\"}")
.post("/restapis/" + apiId + "/deployments").then().statusCode(201);
assertRejectedWithoutIntegration(request, endpoint, valid, integrationCalls);
given().spec(request).contentType(ContentType.JSON).body(valid.replace("RUNNING", "DONE"))
.post(endpoint).then().statusCode(200);
String storedSchema = given().spec(request).get("/restapis/" + apiId + "/models/Request")
.then().statusCode(200).extract().path("schema");
assertFalse(mapper.readTree(storedSchema).has("components"), "Validation must not rewrite stored models");
String urlSchema = storedSchema.replace("#/components/schemas/",
"https://apigateway.amazonaws.com/restapis/" + apiId + "/models/");
given().spec(request).contentType(ContentType.JSON)
.body("{\"patchOperations\":[{\"op\":\"replace\",\"path\":\"/schema\",\"value\":"
+ mapper.writeValueAsString(urlSchema) + "}]}")
.patch("/restapis/" + apiId + "/models/Request").then().statusCode(200);
given().spec(request).contentType(ContentType.JSON).body("{\"stageName\":\"test\"}")
.post("/restapis/" + apiId + "/deployments").then().statusCode(201);
given().spec(request).contentType(ContentType.JSON).body(valid.replace("RUNNING", "DONE"))
.post(endpoint).then().statusCode(200);
given().spec(request).contentType(ContentType.JSON)
.body("{\"workflowCategory\":\"DMS\",\"status\":\"DONE\",\"child\":"
+ valid.replace("RUNNING", "DONE") + "}")
.post(endpoint).then().statusCode(200);
assertRejectedWithoutIntegration(request, endpoint, valid, integrationCalls);
assertRejectedWithoutIntegration(request, endpoint,
"{\"workflowCategory\":\"DMS\",\"status\":\"DONE\",\"child\":" + valid + "}", integrationCalls);
given().spec(request).contentType(ContentType.JSON).body(patch.replace("DONE", "RUNNING"))
.patch("/restapis/" + apiId + "/models/Status").then().statusCode(200);
given().spec(request).delete("/restapis/" + apiId + "/models/Category").then().statusCode(202);
given().spec(request).contentType(ContentType.JSON).body(valid.replace("RUNNING", "DONE"))
.post(endpoint).then().statusCode(200);
assertRejectedWithoutIntegration(request, endpoint, valid, integrationCalls);
given().spec(request).contentType(ContentType.JSON).body(patch)
.patch("/restapis/" + apiId + "/models/Status").then().statusCode(200);
given().spec(request).contentType(ContentType.JSON)
.body("{\"name\":\"Category\",\"schema\":\"{\\\"type\\\":\\\"string\\\",\\\"enum\\\":[\\\"DMS\\\"]}\"}")
.post("/restapis/" + apiId + "/models").then().statusCode(201);
given().spec(request).contentType(ContentType.JSON)
.body("{\"name\":\"UnrelatedInvalid\",\"schema\":\"{invalid\"}")
.post("/restapis/" + apiId + "/models").then().statusCode(201);
ObjectNode annotated = (ObjectNode) mapper.readTree(storedSchema);
annotated.putObject("default").put("$ref", "#/components/schemas/NotAModel");
given().spec(request).contentType(ContentType.JSON)
.body("{\"patchOperations\":[{\"op\":\"replace\",\"path\":\"/schema\",\"value\":"
+ mapper.writeValueAsString(annotated.toString()) + "}]}")
.patch("/restapis/" + apiId + "/models/Request").then().statusCode(200);
given().spec(request).contentType(ContentType.JSON).body("{\"stageName\":\"test\"}")
.post("/restapis/" + apiId + "/deployments").then().statusCode(201);
given().spec(request).contentType(ContentType.JSON).body(valid.replace("RUNNING", "DONE"))
.post(endpoint).then().statusCode(200);
String otherApi = given().spec(request).contentType(ContentType.JSON).body("{\"name\":\"OtherModelOwner\"}")
.post("/restapis").then().statusCode(201).extract().path("id");
try {
given().spec(request).contentType(ContentType.JSON)
.body("{\"name\":\"OtherOnly\",\"contentType\":\"application/json\",\"schema\":"
+ mapper.writeValueAsString("{\"type\":\"object\"}") + "}")
.post("/restapis/" + otherApi + "/models").then().statusCode(201);
String missing = "{\"$ref\":\"https://apigateway.amazonaws.com/restapis/"
+ otherApi + "/models/OtherOnly\"}";
given().spec(request).contentType(ContentType.JSON)
.body("{\"patchOperations\":[{\"op\":\"replace\",\"path\":\"/schema\",\"value\":"
+ mapper.writeValueAsString(missing) + "}]}")
.patch("/restapis/" + apiId + "/models/Request").then().statusCode(200);
given().spec(request).contentType(ContentType.JSON).body("{\"stageName\":\"test\"}")
.post("/restapis/" + apiId + "/deployments").then().statusCode(201);
assertRejectedWithoutIntegration(request, endpoint, valid, integrationCalls);
} finally {
given().spec(request).delete("/restapis/" + otherApi).then().statusCode(202);
}
AtomicInteger externalReads = new AtomicInteger();
HttpServer external = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
external.createContext("/schema", exchange -> {
externalReads.incrementAndGet();
byte[] bytes = "{}".getBytes(StandardCharsets.UTF_8);
exchange.sendResponseHeaders(200, bytes.length);
exchange.getResponseBody().write(bytes);
exchange.close();
});
external.start();
try {
String externalSchema = "{\"$ref\":\"http://127.0.0.1:" + external.getAddress().getPort() + "/schema\"}";
given().spec(request).contentType(ContentType.JSON)
.body("{\"patchOperations\":[{\"op\":\"replace\",\"path\":\"/schema\",\"value\":"
+ mapper.writeValueAsString(externalSchema) + "}]}")
.patch("/restapis/" + apiId + "/models/Request").then().statusCode(200);
given().spec(request).contentType(ContentType.JSON).body("{\"stageName\":\"test\"}")
.post("/restapis/" + apiId + "/deployments").then().statusCode(201);
assertRejectedWithoutIntegration(request, endpoint, valid, integrationCalls);
assertEquals(0, externalReads.get(), "Request validation must not fetch external schemas");
} finally {
external.stop(0);
}
} finally {
integration.stop(0);
given().spec(request).delete("/restapis/" + apiId).then().statusCode(202);
}
}
private void assertRejectedWithoutIntegration(RequestSpecification request, String endpoint, String body,
AtomicInteger integrationCalls) {
int calls = integrationCalls.get();
given().spec(request).contentType(ContentType.JSON).body(body).post(endpoint)
.then().statusCode(400).body("message", equalTo("Invalid request body"));
assertEquals(calls, integrationCalls.get(), "Rejected requests must not reach the integration");
}
@Test
@Order(96)
void validation_rejectsInvalidBody() throws Exception {
@@ -1171,7 +1364,7 @@ class ApiGatewayOpenApiImportTest {
.post("/execute-api/" + apiId + "/test/items")
.then()
.statusCode(400)
.body(containsString("price"));
.body("message", equalTo("Invalid request body"));
// Wrong type for "price" — should fail
given()
@@ -1295,7 +1488,7 @@ class ApiGatewayOpenApiImportTest {
// ──────────────────────────── Content-Type Tolerance ────────────────────────────
@Test
@Order(70)
@Order(73)
void importRestApi_octetStreamContentType() throws Exception {
String spec = """
{"openapi": "3.0.1", "info": {"title": "OctetImport", "version": "1.0"}, "paths": {}}
@@ -1369,7 +1562,7 @@ class ApiGatewayOpenApiImportTest {
// ──────────────────────────── Security scheme / authorizer import ────────────────────────────
@Test
@Order(90)
@Order(89)
void importRestApi_lambdaAuthorizerFromSecurityScheme() throws Exception {
String spec = """
{
@@ -1645,7 +1838,7 @@ class ApiGatewayOpenApiImportTest {
}
@Test
@Order(95)
@Order(101)
void importRestApi_requestAuthorizerWithoutIdentitySourceRejectsDefaultCaching() {
String title = "InvalidCachedRequestAuthorizer";
String spec = requestAuthorizerSpec(title, "", "/invalid");
@@ -1667,7 +1860,7 @@ class ApiGatewayOpenApiImportTest {
}
@Test
@Order(96)
@Order(102)
void importRestApi_requestAuthorizerWithoutIdentitySourceAllowsZeroTtl() throws Exception {
String spec = requestAuthorizerSpec(
"UncachedRequestAuthorizer", ", \"authorizerResultTtlInSeconds\": 0", "/uncached");
@@ -1691,7 +1884,7 @@ class ApiGatewayOpenApiImportTest {
}
@Test
@Order(97)
@Order(103)
void putRestApi_invalidCachedRequestAuthorizerDoesNotReplaceExistingApi() throws Exception {
String originalSpec = """
{