fix(apigatewayv2): add missing JSON handler operations and stageName auto-deploy (#393)

* fix(apigatewayv2): add missing JSON handler operations and stageName auto-deploy

The JSON 1.1 handler was missing GetDeployment, DeleteDeployment, and
DeleteIntegration operations. Callers using the X-Amz-Target path
(non-SDK, Terraform, CLI --cli-input-json) would get UnknownOperation
errors for these actions.

Additionally, the handler passed PascalCase map keys from the JSON 1.1
payload directly to service methods that expect lowerCamelCase. Added
key normalization so both REST and JSON 1.1 paths produce consistent
input for the service layer.

createDeployment now respects the stageName parameter: when provided,
it auto-deploys by updating the named stage's deploymentId and
lastUpdatedDate, matching AWS behavior. Throws NotFoundException if
the stage doesn't exist.

- Add GetDeployment, DeleteDeployment, DeleteIntegration switch cases
- Add toLowerCamelCase() key normalizer for JSON 1.1 map inputs
- Implement stageName auto-deploy in createDeployment
- 9 new integration tests covering the full deployment lifecycle,
  stageName auto-deploy, missing stage 404, and delete operations

* fix: address review feedback on apigatewayv2 changes

- Validate stage exists before persisting deployment to prevent
  orphan deployments on invalid stageName (Codex finding)
- Add JSON 1.1 path tests using X-Amz-Target and PascalCase keys
  to directly exercise the handler's normalization and new switch
  cases (both reviewers)
- Handle List values in toLowerCamelCase normalization for nested
  object arrays (Gemini suggestion)
- Add orphan deployment assertion to missing-stage 404 test
This commit is contained in:
TiM authored and GitHub committed 2026-04-13 11:01:03 -05:00
1 parent f053db583a
commit cd62fda111
3 files changed
+292 -6

No files matched your search

@@ -11,6 +11,7 @@ import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.core.Response;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@@ -49,7 +50,10 @@ public class ApiGatewayV2JsonHandler {
case "GetStages" -> handleGetStages(request, region);
case "DeleteStage" -> handleDeleteStage(request, region);
case "CreateDeployment" -> handleCreateDeployment(request, region);
case "GetDeployment" -> handleGetDeployment(request, region);
case "GetDeployments" -> handleGetDeployments(request, region);
case "DeleteDeployment" -> handleDeleteDeployment(request, region);
case "DeleteIntegration" -> handleDeleteIntegration(request, region);
default -> JsonErrorResponseUtils.createUnknownOperationErrorResponse(action);
};
} catch (AwsException e) {
@@ -61,7 +65,7 @@ public class ApiGatewayV2JsonHandler {
private Response handleCreateApi(JsonNode request, String region) {
@SuppressWarnings("unchecked")
Map<String, Object> map = objectMapper.convertValue(request, Map.class);
Map<String, Object> map = toLowerCamelCase(objectMapper.convertValue(request, Map.class));
Api api = service.createApi(region, map);
return Response.status(201).entity(toApiNode(api).toString()).build();
}
@@ -90,7 +94,7 @@ public class ApiGatewayV2JsonHandler {
private Response handleCreateAuthorizer(JsonNode request, String region) {
String apiId = request.path("ApiId").asText();
@SuppressWarnings("unchecked")
Map<String, Object> map = objectMapper.convertValue(request, Map.class);
Map<String, Object> map = toLowerCamelCase(objectMapper.convertValue(request, Map.class));
Authorizer auth = service.createAuthorizer(region, apiId, map);
return Response.status(201).entity(toAuthorizerNode(auth).toString()).build();
}
@@ -122,7 +126,7 @@ public class ApiGatewayV2JsonHandler {
private Response handleCreateRoute(JsonNode request, String region) {
String apiId = request.path("ApiId").asText();
@SuppressWarnings("unchecked")
Map<String, Object> map = objectMapper.convertValue(request, Map.class);
Map<String, Object> map = toLowerCamelCase(objectMapper.convertValue(request, Map.class));
Route route = service.createRoute(region, apiId, map);
return Response.status(201).entity(toRouteNode(route).toString()).build();
}
@@ -154,7 +158,7 @@ public class ApiGatewayV2JsonHandler {
private Response handleCreateIntegration(JsonNode request, String region) {
String apiId = request.path("ApiId").asText();
@SuppressWarnings("unchecked")
Map<String, Object> map = objectMapper.convertValue(request, Map.class);
Map<String, Object> map = toLowerCamelCase(objectMapper.convertValue(request, Map.class));
Integration integration = service.createIntegration(region, apiId, map);
return Response.status(201).entity(toIntegrationNode(integration).toString()).build();
}
@@ -179,7 +183,7 @@ public class ApiGatewayV2JsonHandler {
private Response handleCreateStage(JsonNode request, String region) {
String apiId = request.path("ApiId").asText();
@SuppressWarnings("unchecked")
Map<String, Object> map = objectMapper.convertValue(request, Map.class);
Map<String, Object> map = toLowerCamelCase(objectMapper.convertValue(request, Map.class));
Stage stage = service.createStage(region, apiId, map);
return Response.status(201).entity(toStageNode(stage).toString()).build();
}
@@ -211,11 +215,17 @@ public class ApiGatewayV2JsonHandler {
private Response handleCreateDeployment(JsonNode request, String region) {
String apiId = request.path("ApiId").asText();
@SuppressWarnings("unchecked")
Map<String, Object> map = objectMapper.convertValue(request, Map.class);
Map<String, Object> map = toLowerCamelCase(objectMapper.convertValue(request, Map.class));
Deployment deployment = service.createDeployment(region, apiId, map);
return Response.status(201).entity(toDeploymentNode(deployment).toString()).build();
}
private Response handleGetDeployment(JsonNode request, String region) {
String apiId = request.path("ApiId").asText();
String deploymentId = request.path("DeploymentId").asText();
return Response.ok(toDeploymentNode(service.getDeployment(region, apiId, deploymentId)).toString()).build();
}
private Response handleGetDeployments(JsonNode request, String region) {
String apiId = request.path("ApiId").asText();
List<Deployment> deployments = service.getDeployments(region, apiId);
@@ -225,6 +235,20 @@ public class ApiGatewayV2JsonHandler {
return Response.ok(root.toString()).build();
}
private Response handleDeleteDeployment(JsonNode request, String region) {
String apiId = request.path("ApiId").asText();
String deploymentId = request.path("DeploymentId").asText();
service.deleteDeployment(region, apiId, deploymentId);
return Response.noContent().build();
}
private Response handleDeleteIntegration(JsonNode request, String region) {
String apiId = request.path("ApiId").asText();
String integrationId = request.path("IntegrationId").asText();
service.deleteIntegration(region, apiId, integrationId);
return Response.noContent().build();
}
// ──────────────────────────── Serializers ────────────────────────────
private ObjectNode toApiNode(Api api) {
@@ -297,4 +321,33 @@ public class ApiGatewayV2JsonHandler {
return node;
}
/**
* Converts PascalCase map keys to lowerCamelCase so the service layer's
* field lookups work regardless of whether the request arrived via the
* REST path (lowerCamelCase body) or JSON 1.1 path (PascalCase body).
*/
@SuppressWarnings("unchecked")
private Map<String, Object> toLowerCamelCase(Map<String, Object> map) {
Map<String, Object> result = new LinkedHashMap<>();
for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = entry.getKey();
if (!key.isEmpty() && Character.isUpperCase(key.charAt(0))) {
key = Character.toLowerCase(key.charAt(0)) + key.substring(1);
}
result.put(key, normalizeValue(entry.getValue()));
}
return result;
}
@SuppressWarnings("unchecked")
private Object normalizeValue(Object value) {
if (value instanceof Map) {
return toLowerCamelCase((Map<String, Object>) value);
}
if (value instanceof List<?> list) {
return list.stream().map(this::normalizeValue).toList();
}
return value;
}
}
@@ -264,6 +264,16 @@ public class ApiGatewayV2Service {
public Deployment createDeployment(String region, String apiId, Map<String, Object> request) {
getApi(region, apiId);
// Validate stage exists before creating deployment to avoid orphans
String stageName = (String) request.get("stageName");
Stage stage = null;
if (stageName != null && !stageName.isBlank()) {
stage = stageStore.get(stageKey(region, apiId, stageName))
.orElseThrow(() -> new AwsException("NotFoundException",
"Stage " + stageName + " not found", 404));
}
Deployment deployment = new Deployment();
deployment.setDeploymentId(shortId(8));
deployment.setDeploymentStatus("DEPLOYED");
@@ -271,6 +281,13 @@ public class ApiGatewayV2Service {
deployment.setCreatedDate(System.currentTimeMillis());
deploymentStore.put(deploymentKey(region, apiId, deployment.getDeploymentId()), deployment);
if (stage != null) {
stage.setDeploymentId(deployment.getDeploymentId());
stage.setLastUpdatedDate(System.currentTimeMillis());
stageStore.put(stageKey(region, apiId, stageName), stage);
}
return deployment;
}