feat(apigateway): generate REST and WebSocket model templates (#9089)

GetModelTemplate now answers for REST and WebSocket models with sample VTL:
the $inputRoot line, two-space indented typed placeholders ("foo", 3.1415),
and an editable ##TODO / #foreach skeleton for arrays, generated from the
latest stored schema. A top-level scalar model is refused with 400
"Unsupported data type for mapping template", a missing WebSocket model with
404 "Invalid model identifier specified: <id>", and HTTP APIs refuse models
at CreateModel.

Co-authored-by: Andre Kurait <AndreKurait@gmail.com>
Signed-off-by: Jackson Price <jxnprice@amazon.com>
This commit is contained in:
Jackson PriceandAndre Kurait authored and GitHub committed 2026-11-24 16:42:19 -04:00
1 parent bc6a2279d4
commit 33d873b705
16 files changed
+1007 -17

No files matched your search

@@ -0,0 +1,182 @@
package io.floci.aws.core.common;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.TextNode;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
/** Bounded JSON Schema traversal for API Gateway's sample mapping templates. */
public final class ModelTemplateGenerator {
private static final ObjectMapper JSON = new ObjectMapper()
.enable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS);
private static final int MAX_SCHEMA_CHARACTERS = 1_048_576;
private static final int MAX_NODES = 4_096;
private static final int MAX_DEPTH = 32;
private static final int MAX_OUTPUT_CHARACTERS = 262_144;
private static final Set<String> TYPES = Set.of("object", "array", "string", "integer", "number", "boolean", "null");
private ModelTemplateGenerator() {}
public static String generate(String schema, String contentType) {
if (contentType != null && !contentType.split(";", 2)[0].trim().equalsIgnoreCase("application/json")) {
throw unsupported("Only application/json model templates are supported.");
}
if (schema == null || schema.isBlank()) {
throw new AwsException("BadRequestException", "The model does not contain a JSON schema.", 400);
}
if (schema.length() > MAX_SCHEMA_CHARACTERS) {
throw unsupported("The model schema exceeds the local template generation limit.");
}
JsonNode document;
try {
document = JSON.readTree(schema);
} catch (JsonProcessingException error) {
throw new AwsException("BadRequestException", "The model schema is not valid JSON.", 400);
}
Compiler compiler = new Compiler(document);
Shape shape = compiler.read(document, 0, new HashSet<>());
if (!"object".equals(shape.type()) && !"array".equals(shape.type())) {
throw new AwsException("BadRequestException",
"Unsupported data type for mapping template: " + shape.type(), 400);
}
Output output = new Output();
output.append("#set($inputRoot = $input.path('$'))\n");
render(shape, 0, output);
return output.text.toString();
}
private static void render(Shape shape, int depth, Output output) {
String indent = " ".repeat(depth);
String childIndent = indent + " ";
switch (shape.type()) {
case "object" -> {
output.append("{");
boolean first = true;
for (Map.Entry<String, Shape> property : shape.properties().entrySet()) {
if (!first) {
output.append(",");
}
first = false;
output.append("\n" + childIndent + TextNode.valueOf(property.getKey()) + " : ");
render(property.getValue(), depth + 1, output);
}
if (!first) {
output.append("\n" + indent);
}
output.append("}");
}
case "array" -> {
output.append("[\n" + indent
+ "##TODO: Update this foreach loop to reference array from input json\n"
+ indent + "#foreach($elem in $inputRoot.TODO)\n" + indent + " ");
render(shape.items(), depth + 1, output);
output.append(" \n" + indent + "#if($foreach.hasNext),#end\n"
+ indent + "#end\n" + indent + "]");
}
case "string" -> output.append("\"foo\"");
case "integer" -> output.append("1");
case "number" -> output.append("3.1415");
case "boolean" -> output.append("true");
case "null" -> output.append("null");
default -> throw new IllegalStateException("Uncompiled schema type");
}
}
private static AwsException unsupported(String message) {
return new AwsException("UnsupportedOperation", message, 501);
}
private record Shape(String type, Map<String, Shape> properties, Shape items) {}
private static final class Compiler {
private final JsonNode root;
private int nodes;
private Compiler(JsonNode root) {
this.root = root;
}
private Shape read(JsonNode schema, int depth, Set<String> references) {
nodes++;
if (nodes > MAX_NODES || depth > MAX_DEPTH) {
throw unsupported("The model schema exceeds the local template traversal limit.");
}
if (schema == null || !schema.isObject()) {
throw unsupported("Model templates require JSON Schema objects.");
}
for (String keyword : Set.of("allOf", "anyOf", "oneOf", "not", "enum", "const")) {
if (schema.has(keyword)) {
throw unsupported("Model template generation does not support " + keyword + ".");
}
}
if (schema.has("$ref")) {
String reference = schema.path("$ref").asText();
if (!reference.startsWith("#/")) {
throw unsupported("Only finite local JSON Pointer references are supported.");
}
if (!references.add(reference)) {
throw unsupported("Cyclic model references are not supported.");
}
try {
JsonNode target = root.at(reference.substring(1));
if (target.isMissingNode()) {
throw unsupported("The local model reference could not be resolved.");
}
return read(target, depth + 1, references);
} catch (IllegalArgumentException error) {
throw unsupported("The local model reference is not a valid JSON Pointer.");
} finally {
references.remove(reference);
}
}
JsonNode declaredType = schema.get("type");
if (declaredType != null && !declaredType.isTextual()) {
throw unsupported("Model templates require a single supported JSON type at each value.");
}
String type = declaredType == null ? "" : declaredType.textValue();
if (declaredType == null && schema.has("properties")) {
type = "object";
} else if (declaredType == null && schema.has("items")) {
type = "array";
}
if (!TYPES.contains(type)) {
throw unsupported("Model templates require a single supported JSON type at each value.");
}
Map<String, Shape> properties = new LinkedHashMap<>();
Shape items = null;
if ("object".equals(type) && schema.has("properties")) {
JsonNode declared = schema.get("properties");
if (!declared.isObject()) {
throw unsupported("Model properties must be a JSON Schema object.");
}
for (Map.Entry<String, JsonNode> property : declared.properties()) {
String name = property.getKey();
if (name.chars().anyMatch(character -> character < 32 || "'\"\\$#".indexOf(character) >= 0)) {
throw unsupported("Property names containing VTL delimiters or control characters are not supported.");
}
properties.put(name, read(property.getValue(), depth + 1, references));
}
} else if ("array".equals(type)) {
items = read(schema.get("items"), depth + 1, references);
}
return new Shape(type, properties, items);
}
}
private static final class Output {
private final StringBuilder text = new StringBuilder();
private void append(String value) {
if (text.length() + value.length() > MAX_OUTPUT_CHARACTERS) {
throw unsupported("The generated template exceeds the local output limit.");
}
text.append(value);
}
}
}
@@ -1213,6 +1213,16 @@ public class ApiGatewayController {
return Response.ok(toModelNode(service.getModel(region, apiId, modelName)).toString()).type(MediaType.APPLICATION_JSON).build();
}
@GET
@Path("/restapis/{apiId}/models/{modelName}/default_template")
public Response getModelTemplate(@Context HttpHeaders headers, @PathParam("apiId") String apiId,
@PathParam("modelName") String modelName) {
String region = regionResolver.resolveRegion(headers);
String value = service.getModelTemplate(region, apiId, modelName);
return Response.ok(objectMapper.createObjectNode().put("value", value).toString())
.type(MediaType.APPLICATION_JSON).build();
}
@PATCH
@Path("/restapis/{apiId}/models/{modelName}")
public Response updateModel(@Context HttpHeaders headers, @PathParam("apiId") String apiId, @PathParam("modelName") String modelName, String body) {
@@ -2239,6 +2249,16 @@ public class ApiGatewayController {
return Response.ok(toV2ModelNode(v2Service.getModel(region, apiId, modelId)).toString()).type(MediaType.APPLICATION_JSON).build();
}
@GET
@Path("/v2/apis/{apiId}/models/{modelId}/template")
public Response getV2ModelTemplate(@Context HttpHeaders headers, @PathParam("apiId") String apiId,
@PathParam("modelId") String modelId) {
String region = regionResolver.resolveRegion(headers);
String value = v2Service.getModelTemplate(region, apiId, modelId);
return Response.ok(objectMapper.createObjectNode().put("value", value).toString())
.type(MediaType.APPLICATION_JSON).build();
}
@PATCH
@Path("/v2/apis/{apiId}/models/{modelId}")
public Response updateV2Model(@Context HttpHeaders headers,
@@ -15,6 +15,7 @@ import io.floci.aws.core.common.AwsPartitions;
import io.floci.aws.core.common.AwsRegionFacts;
import io.floci.aws.core.common.AwsRegions;
import io.floci.aws.core.common.CloudFrontEdgeDomain;
import io.floci.aws.core.common.ModelTemplateGenerator;
import io.floci.aws.core.common.PaginatedResult;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.core.common.ReservedTags;
@@ -2504,6 +2505,12 @@ public class ApiGatewayService implements ResourceProvider, Resettable {
return model;
}
public String getModelTemplate(String region, String apiId, String modelName) {
getRestApi(region, apiId);
Model model = getModel(region, apiId, modelName);
return ModelTemplateGenerator.generate(model.getSchema(), model.getContentType());
}
public List<Model> getModels(String region, String apiId) {
getRestApi(region, apiId);
String prefix = region + "::" + apiId + "::";
@@ -83,6 +83,7 @@ public class ApiGatewayV2JsonHandler {
case "DeleteIntegrationResponse" -> handleDeleteIntegrationResponse(request, region);
case "CreateModel" -> handleCreateModel(request, region);
case "GetModel" -> handleGetModel(request, region);
case "GetModelTemplate" -> handleGetModelTemplate(request, region);
case "GetModels" -> handleGetModels(request, region);
case "UpdateModel" -> handleUpdateModel(request, region);
case "DeleteModel" -> handleDeleteModel(request, region);
@@ -492,6 +493,14 @@ public class ApiGatewayV2JsonHandler {
return Response.ok(root.toString()).build();
}
private Response handleGetModelTemplate(JsonNode request, String region) {
String apiId = request.path("ApiId").asText();
String modelId = request.path("ModelId").asText();
String template = service.getModelTemplate(region, apiId, modelId);
ObjectNode response = objectMapper.createObjectNode().put("Value", template);
return Response.ok(response.toString()).build();
}
private Response handleUpdateModel(JsonNode request, String region) {
String apiId = request.path("ApiId").asText();
String modelId = request.path("ModelId").asText();
@@ -5,6 +5,7 @@ import io.floci.aws.config.EmulatorConfig;
import io.floci.aws.core.common.AwsArnUtils;
import io.floci.aws.core.common.AwsEndpoints;
import io.floci.aws.core.common.AwsException;
import io.floci.aws.core.common.ModelTemplateGenerator;
import io.floci.aws.core.common.PaginatedResult;
import io.floci.aws.core.common.Pagination;
import io.floci.aws.core.common.RegionResolver;
@@ -1457,7 +1458,11 @@ public class ApiGatewayV2Service {
// ──────────────────────────── Model CRUD ────────────────────────────
public Model createModel(String region, String apiId, Map<String, Object> request) {
getApi(region, apiId);
Api api = getApi(region, apiId);
if (!"WEBSOCKET".equals(api.getProtocolType())) {
throw new AwsException("BadRequestException",
"Currently, Models are not permitted for APIs with a protocol type of HTTP", 400);
}
Model model = new Model();
model.setModelId(shortId(10));
model.setName((String) request.get("name"));
@@ -1473,6 +1478,14 @@ public class ApiGatewayV2Service {
.orElseThrow(() -> new AwsException("NotFoundException", "Model not found", 404));
}
public String getModelTemplate(String region, String apiId, String modelId) {
getApi(region, apiId);
Model model = modelStore.get(modelKey(region, apiId, modelId))
.orElseThrow(() -> new AwsException("NotFoundException",
"Invalid model identifier specified: " + modelId, 404));
return ModelTemplateGenerator.generate(model.getSchema(), model.getContentType());
}
public List<Model> getModels(String region, String apiId) {
getApi(region, apiId);
String prefix = region + "::" + apiId + "::";