fix(apigateway): match AWS integration responses by status (#5443)

* fix(apigateway): match AWS integration responses by status

* fix(apigateway): ignore synthetic status during response selection
This commit is contained in:
XuJian authored and GitHub committed 2026-10-13 10:24:21 -04:00
1 parent e023ae9ff4
commit 7b8377fa3c
4 files changed
+312 -12

No files matched your search

@@ -846,8 +846,8 @@ public class ApiGatewayExecuteController {
* entirely from mapping templates and explicit {@code integration.request.*} parameter
* mappings — inbound headers and query parameters that were <em>not</em> mapped are not
* forwarded. The backend's response then runs through the method's integration responses, where
* {@code selectionPattern} is matched against the backend's HTTP status code (for
* {@code AWS}/Lambda integrations it is matched against the error message instead).
* {@code selectionPattern} is matched against the backend's HTTP status code.
* Lambda integrations use error text instead.
*/
private Response invokeHttpIntegration(GatewayResponseScope scope, String region, String apiId,
String httpMethod, String path,
@@ -1743,6 +1743,8 @@ public class ApiGatewayExecuteController {
// "Action=SendMessage&..."). Action-style URIs (arn:...:{service}:action/{Action})
// carry the action in the URI and render a JSON body.
Response serviceResponse;
int serviceStatus = 500;
String backendStatusMatchString = null;
String errorType = null;
String errorMessage = null;
try {
@@ -1784,6 +1786,8 @@ public class ApiGatewayExecuteController {
serviceResponse = serviceRouter.invoke(target.service(), target.action(), requestJson, region);
}
} catch (AwsException e) {
serviceStatus = e.getHttpStatus();
backendStatusMatchString = Integer.toString(serviceStatus);
errorType = e.getErrorCode();
errorMessage = e.getMessage();
serviceResponse = null;
@@ -1795,9 +1799,9 @@ public class ApiGatewayExecuteController {
// Build response body string
String responseBodyStr;
int serviceStatus;
if (serviceResponse != null) {
serviceStatus = serviceResponse.getStatus();
backendStatusMatchString = Integer.toString(serviceStatus);
Object entity = serviceResponse.getEntity();
if (entity instanceof JsonNode jsonNode) {
try {
@@ -1826,19 +1830,16 @@ public class ApiGatewayExecuteController {
}
}
} else {
serviceStatus = 500;
responseBodyStr = String.format("{\"errorMessage\":\"%s\",\"errorType\":\"%s\"}",
errorMessage != null ? errorMessage.replace("\"", "\\\"") : "Unknown error",
errorType != null ? errorType : "UnknownError");
}
// AWS matches selectionPattern against the error response body/message for AWS/Lambda
// integrations. We match against both errorType and errorMessage to catch patterns like
// ".*ResourceNotFoundException.*". (HTTP integrations match the status code instead —
// see invokeHttpIntegration.)
String errorMatchString = errorType != null
? errorType + (errorMessage != null ? ": " + errorMessage : "")
: errorMessage;
String selectionMatchString = "lambda".equals(target.service())
? errorMatchString : backendStatusMatchString;
// Case-insensitive: an integration.response.header.X-Foo mapping must resolve regardless of
// the casing the backend or client library used for the header name.
@@ -1855,7 +1856,7 @@ public class ApiGatewayExecuteController {
vtlAuthorizerContext, httpRequestMappingContext(scope, requestId, authorizerResult));
int fallbackStatus = errorType != null ? 500 : (serviceStatus >= 400 ? serviceStatus : 200);
return mapIntegrationResponse(integration, errorMatchString, responseBodyStr, fallbackStatus,
return mapIntegrationResponse(integration, selectionMatchString, responseBodyStr, fallbackStatus,
serviceResponseHeaders, responseMappingCtx, MediaType.APPLICATION_JSON);
}
@@ -1919,8 +1920,8 @@ public class ApiGatewayExecuteController {
* {@code $context.responseOverride} assignments and {@code responseParameters} header mappings.
*
* @param selectionMatchString what {@code selectionPattern} regexes are tested against — the
* error message for {@code AWS}/Lambda integrations, the backend's
* HTTP status code for {@code HTTP} integrations
* error text for Lambda integrations, the backend's HTTP status
* code for other {@code AWS} and {@code HTTP} integrations
* @param fallbackStatus status used when no integration response is configured or matched
*/
private Response mapIntegrationResponse(Integration integration, String selectionMatchString,