-
Notifications
You must be signed in to change notification settings - Fork 637
Description
Describe the bug
Considering the following Kotlin Spring Cloud Function definition (APIGatewayProxyRequestEvent) -> APIGatewayProxyResponseEvent when deployed as part of an API GW Lambda Integration configuration, the lambda integration response is not mapped into an HTTP Response according to the aws docu (https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-output-format).
Instead it is transparently passed as the HTTP Response body therefore also generating CORS error (because of course no mapping is done).
Changing the function definition to (APIGatewayProxyRequestEvent) -> Message<String> is correctly mapped by the API GW to an HTTP Response, where all the headers are set on the Message and the body is just the serialised object set as the Message payload.
Investigation
I wanted to see what are the differences so I quickly wrote(copied&changed the function def) this test in org.springframework.cloud.function.adapter.aws.FunctionInvokerTests:
@Test
public void testApiGatewayInEventAndOutMessage() throws Exception {
System.setProperty("MAIN_CLASS", ApiGatewayConfiguration.class.getName());
System.setProperty("spring.cloud.function.definition", "inputApiEventOutputMessage");
FunctionInvoker invoker = new FunctionInvoker();
InputStream targetStream = new ByteArrayInputStream(this.apiGatewayEvent.getBytes());
ByteArrayOutputStream output = new ByteArrayOutputStream();
invoker.handleRequest(targetStream, output, null);
Map result = mapper.readValue(output.toByteArray(), Map.class);
assertThat(result.get("body")).isEqualTo("hello");
Map headers = (Map) result.get("headers");
assertThat(headers.get("foo")).isEqualTo("bar");
}
@Bean
public Function<APIGatewayProxyRequestEvent, Message<String>> inputApiEventOutputMessage() {
return v -> MessageBuilder.withPayload(v.getBody())
.setHeader("statusCode", 200)
.setHeader("foo", "bar")
.build();
}
Outcome
I have noticed some differences in the output between the function from the test that I copied over testApiGatewayInEventAndOutMessage() and testApiGatewayInAndOut() like the body is wrapped in an extra double-quotes and there are some extra headers added to the Message but I don't know what this means exactly to the API GW.