fix(apigateway): preserve and update method response parameters (#5030)

* fix(apigateway): preserve and update method response parameters

* fix(apigateway): serialize method response patches and decode header paths

* fix(apigateway): return created for method response updates
This commit is contained in:
Aniket Shukla authored and GitHub committed 2026-10-05 09:38:09 -04:00
1 parent 1d44b5fc7c
commit b988a061b4
4 files changed
+196 -2

No files matched your search

@@ -173,6 +173,22 @@ public class ApiGatewayController {
}
}
@PATCH
@Path("/restapis/{apiId}/resources/{resourceId}/methods/{httpMethod}/responses/{statusCode}")
public Response updateMethodResponse(@Context HttpHeaders headers,
@PathParam("apiId") String apiId,
@PathParam("resourceId") String resourceId,
@PathParam("httpMethod") String httpMethod,
@PathParam("statusCode") String statusCode,
String body) {
String region = regionResolver.resolveRegion(headers);
List<Map<String, String>> patchOperations = parsePatchOperations(body);
MethodResponse response = service.updateMethodResponse(
region, apiId, resourceId, httpMethod, statusCode, patchOperations);
return Response.status(201).entity(toMethodResponseNode(response).toString())
.type(MediaType.APPLICATION_JSON).build();
}
@DELETE
@Path("/restapis/{apiId}/resources/{resourceId}/methods/{httpMethod}/responses/{statusCode}")
public Response deleteMethodResponse(@Context HttpHeaders headers,
@@ -2235,6 +2251,10 @@ public class ApiGatewayController {
private ObjectNode toMethodResponseNode(MethodResponse r) {
ObjectNode node = objectMapper.createObjectNode();
node.put("statusCode", r.statusCode());
if (r.responseParameters() != null && !r.responseParameters().isEmpty()) {
ObjectNode parameters = node.putObject("responseParameters");
r.responseParameters().forEach(parameters::put);
}
return node;
}
@@ -541,7 +541,7 @@ public class ApiGatewayService implements ResourceProvider {
resourceStore.put(resourceKey(region, apiId, resourceId), resource);
}
public MethodResponse putMethodResponse(String region, String apiId, String resourceId,
public synchronized MethodResponse putMethodResponse(String region, String apiId, String resourceId,
String httpMethod, String statusCode,
Map<String, Object> request) {
MethodConfig method = getMethod(region, apiId, resourceId, httpMethod);
@@ -564,7 +564,41 @@ public class ApiGatewayService implements ResourceProvider {
return mr;
}
public void deleteMethodResponse(String region, String apiId, String resourceId,
public synchronized MethodResponse updateMethodResponse(String region, String apiId, String resourceId,
String httpMethod, String statusCode,
List<Map<String, String>> patchOperations) {
MethodResponse response = getMethodResponse(region, apiId, resourceId, httpMethod, statusCode);
Map<String, Boolean> parameters = new HashMap<>(
response.responseParameters() != null ? response.responseParameters() : Map.of());
if (patchOperations != null) {
for (Map<String, String> patch : patchOperations) {
String path = patch.get("path");
if (path == null || !path.startsWith("/responseParameters/method.response.header.")
|| path.length() == "/responseParameters/method.response.header.".length()) {
throw new AwsException("BadRequestException", "Invalid patch operation", 400);
}
String name = unescapeJsonPointer(path.substring("/responseParameters/".length()));
String op = patch.get("op");
if ("remove".equals(op)) {
parameters.remove(name);
} else if ("add".equals(op) || "replace".equals(op)) {
String value = patch.get("value");
if (!"true".equals(value) && !"false".equals(value)) {
throw new AwsException("BadRequestException", "Invalid patch operation", 400);
}
parameters.put(name, Boolean.parseBoolean(value));
} else {
throw new AwsException("BadRequestException", "Invalid patch operation", 400);
}
}
}
MethodResponse updated = new MethodResponse(statusCode, parameters);
getMethod(region, apiId, resourceId, httpMethod).getMethodResponses().put(statusCode, updated);
resourceStore.put(resourceKey(region, apiId, resourceId), getResource(region, apiId, resourceId));
return updated;
}
public synchronized void deleteMethodResponse(String region, String apiId, String resourceId,
String httpMethod, String statusCode) {
MethodConfig method = getMethod(region, apiId, resourceId, httpMethod);
if (method.getMethodResponses().remove(statusCode) == null) {