fix(apigateway): support AWS type Lambda integration and fix duplicate Content-Type header (#2049)

* fix(apigateway): support AWS type Lambda integration and fix duplicate Content-Type header

Integration tests for AWS type lambda integration with apigateway and duplicate content-type headers

Addressing Greptile review the qualifier is now preserved end to end and covered by tests

* fix(apigateway): resolving review feedback, adding in comment to LambdaArnUtils re lastIndexOf fallback, and updating DOCs with Line re v1 execution plane integration type behaviour
This commit is contained in:
Tedd authored and GitHub committed 2026-08-24 17:24:09 -04:00
1 parent fcd662fb62
commit 72dfa634f7
5 files changed
+380 -11

No files matched your search

@@ -1046,15 +1046,38 @@ public class ApiGatewayExecuteController {
// Dispatch to service.
//
// A path-style integration URI (arn:...:{service}:path/...) carries no action: the
// rendered template body is the AWS query protocol (form-urlencoded,
// Lambda path-style integrations (arn:aws:apigateway:{region}:lambda:path/...) are
// handled specially: the function name is extracted from the URI and the rendered
// request template body is passed directly as the Lambda payload — just like
// AWS_PROXY, but with request/response VTL mapping applied.
//
// For other services: a path-style integration URI (arn:...:{service}:path/...) carries
// no action: the rendered template body is the AWS query protocol (form-urlencoded,
// "Action=SendMessage&..."). Action-style URIs (arn:...:{service}:action/{Action})
// carry the action in the URI and render a JSON body.
Response serviceResponse;
String errorType = null;
String errorMessage = null;
try {
if (target.action() == null) {
if ("lambda".equals(target.service())) {
String functionName = functionNameFromUri(integration.getUri());
if (functionName == null || functionName.isBlank()) {
throw new AwsException("InvalidParameterValueException",
"Cannot resolve Lambda function name from URI: " + integration.getUri(), 400);
}
byte[] payload = transformedBody != null ? transformedBody.getBytes(StandardCharsets.UTF_8) : new byte[0];
InvokeResult invokeResult = lambdaService.invoke(region, functionName, payload, InvocationType.RequestResponse);
String lambdaResponseBody = invokeResult.getPayload() != null
? new String(invokeResult.getPayload(), StandardCharsets.UTF_8) : "{}";
int lambdaStatus = invokeResult.getStatusCode() > 0 ? invokeResult.getStatusCode() : 200;
if (invokeResult.getFunctionError() != null) {
errorType = invokeResult.getFunctionError();
errorMessage = lambdaResponseBody;
}
serviceResponse = Response.status(lambdaStatus)
.entity(lambdaResponseBody)
.type(MediaType.APPLICATION_JSON).build();
} else if (target.action() == null) {
MultivaluedMap<String, String> formParams = parseFormUrlEncoded(transformedBody);
if ("sqs".equals(target.service()) && target.path() != null) {
@@ -1191,13 +1214,18 @@ public class ApiGatewayExecuteController {
}
Response.ResponseBuilder rb = Response.status(finalStatus)
.entity(finalBody)
.type(MediaType.APPLICATION_JSON);
.entity(finalBody);
String contentType = null;
// Apply $context.responseOverride header assignments.
if (templateResult != null && !templateResult.headerOverrides().isEmpty()) {
for (Map.Entry<String, String> hdr : templateResult.headerOverrides().entrySet()) {
rb.header(hdr.getKey(), hdr.getValue());
if ("Content-Type".equalsIgnoreCase(hdr.getKey())) {
contentType = hdr.getValue();
} else {
rb.header(hdr.getKey(), hdr.getValue());
}
}
}
@@ -1216,11 +1244,16 @@ public class ApiGatewayExecuteController {
String headerName = dest.substring("method.response.header.".length());
String headerValue = resolveResponseParameter(source, serviceResponseHeaders, responseBodyStr);
if (headerValue != null) {
rb.header(headerName, headerValue);
if ("Content-Type".equalsIgnoreCase(headerName)) {
contentType = headerValue;
} else {
rb.header(headerName, headerValue);
}
}
}
}
rb.type(contentType != null ? contentType : MediaType.APPLICATION_JSON);
return rb.build();
}
@@ -163,16 +163,23 @@ public final class LambdaArnUtils {
}
/**
* Extracts the Lambda function name from an API Gateway integration URI.
* Extracts the Lambda function reference from an API Gateway integration URI.
* Handles formats like:
* <ul>
* <li>{@code arn:aws:lambda:us-east-1:000000000000:function:myFn/invocations}</li>
* <li>{@code arn:aws:lambda:us-east-1:000000000000:function:myFn}</li>
* <li>{@code arn:aws:lambda:us-east-1:000000000000:function:myFn:PROD/invocations} (alias/version)</li>
* <li>{@code myFn} (bare function name)</li>
* </ul>
*
* <p>Any trailing {@code :qualifier} (alias or version) is preserved in the returned
* value (e.g. {@code myFn:PROD}) so downstream {@code invoke} calls, which resolve the
* reference via {@link #resolve(String)}, target the configured alias/version rather
* than reducing to the bare function name and invoking {@code $LATEST}.
*
* @param uri the integration URI (may be null)
* @return the extracted function name, or null if the URI is null or unparseable
* @return the extracted function reference (name, optionally with {@code :qualifier}),
* or null if the URI is null or unparseable
*/
public static String extractFunctionNameFromUri(String uri) {
if (uri == null) {
@@ -189,8 +196,16 @@ public final class LambdaArnUtils {
String embeddedArn = String.join("/", java.util.Arrays.copyOfRange(parts, 3, parts.length));
return extractFunctionNameFromUri(embeddedArn);
}
// Standard Lambda ARN: parts[0] is "function:myFn", strip the "function:" prefix
// Standard Lambda ARN: parts[0] is "function:myFn" or "function:myFn:qualifier".
// Strip only the leading "function:" resource-type prefix, preserving any
// trailing :qualifier (alias/version) so the invoke target is not lost.
String functionPart = parts[0];
String functionPrefix = "function:";
if (functionPart.startsWith(functionPrefix)) {
return functionPart.substring(functionPrefix.length());
}
// Fallback for malformed/non-standard resources: every real Lambda ARN resource
// starts with "function:" and is handled above, so this only fires on unexpected input.
int colon = functionPart.lastIndexOf(':');
return colon >= 0 ? functionPart.substring(colon + 1) : functionPart;
} catch (IllegalArgumentException e) {