Skip to content

Commit

Permalink
GH-1135 Fix AWS data conversion
Browse files Browse the repository at this point in the history
Resolves #1135
  • Loading branch information
olegz committed May 1, 2024
1 parent c0f4cba commit 1787df8
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public static Message<byte[]> generateMessage(byte[] payload, Type inputType, bo

MessageBuilder builder = MessageBuilder
.withPayload(structMessage instanceof Map msg && msg.containsKey("payload")
? ((String) msg.get("payload")).getBytes(StandardCharsets.UTF_8)
? (msg.get("payload"))
: payload);
if (isApiGateway) {
builder.setHeader(AWSLambdaUtils.AWS_API_GATEWAY, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ protected Object convertFromInternal(Message<?> message, Class<?> targetClass, @
}
else {
Object body;
if (message.getHeaders().containsKey("payload")) {
body = message.getPayload();
if (structMessage.containsKey("body")) {
body = structMessage.get("body");
}
else {
body = structMessage.get("body");
body = message.getPayload();
}
Object convertedResult = this.jsonMapper.fromJson(body, targetClass);
return convertedResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,35 @@ public class FunctionInvokerTests {

String jsonPojoCollection = "[{\"name\":\"Ricky\"},{\"name\":\"Julien\"},{\"name\":\"Julien\"}]";

String someEvent = "{\n"
+ " \"payload\": {\n"
+ " \"headers\": {\n"
+ " \"businessUnit\": \"1\"\n"
+ " }\n"
+ " },\n"
+ " \"headers\": {\n"
+ " \"aws-context\": {\n"
+ " \"memoryLimit\": 1024,\n"
+ " \"awsRequestId\": \"87a211bf-540f-4f9f-a218-d096a0099999\",\n"
+ " \"functionName\": \"myfunction\",\n"
+ " \"functionVersion\": \"278\",\n"
+ " \"invokedFunctionArn\": \"arn:aws:lambda:us-east-1:xxxxxxx:function:xxxxx:snapstart\",\n"
+ " \"deadlineTimeInMs\": 1712717704761,\n"
+ " \"logger\": {\n"
+ " \"logFiltering\": {\n"
+ " \"minimumLogLevel\": \"UNDEFINED\"\n"
+ " },\n"
+ " \"logFormatter\": {},\n"
+ " \"logFormat\": \"TEXT\"\n"
+ " }\n"
+ " },\n"
+ " \"businessUnit\": \"1\",\n"
+ " \"id\": \"xxxx\",\n"
+ " \"aws-event\": true,\n"
+ " \"timestamp\": 1712716805129\n"
+ " }\n"
+ "}";

String dynamoDbEvent = "{\n"
+ " \"Records\": [\n"
+ " {\n"
Expand Down Expand Up @@ -706,6 +735,22 @@ public void before() throws Exception {
//this.getEnvironment().clear();
}

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testConversionWhenPayloadExists() throws Exception {
System.setProperty("MAIN_CLASS", BasicConfiguration.class.getName());
System.setProperty("spring.cloud.function.definition", "uppercase");
FunctionInvoker invoker = new FunctionInvoker();

InputStream targetStream = new ByteArrayInputStream(this.someEvent.getBytes());
ByteArrayOutputStream output = new ByteArrayOutputStream();
invoker.handleRequest(targetStream, output, null);

Map result = mapper.readValue(output.toByteArray(), Map.class);
assertThat(result).containsKey("HEADERS");

}

@Test
public void testAPIGatewayCustomAuthorizerEvent() throws Exception {
System.setProperty("MAIN_CLASS", AuthorizerConfiguration.class.getName());
Expand Down Expand Up @@ -1441,6 +1486,17 @@ public void testPrimitiveMessage() throws Exception {
assertThat(result).isEqualTo(testString);
}

@EnableAutoConfiguration
@Configuration
public static class BasicConfiguration {
@Bean
public Function<Message<String>, Message<String>> uppercase() {
return v -> {
return MessageBuilder.withPayload(v.getPayload().toUpperCase()).build();
};
}
}

@EnableAutoConfiguration
@Configuration
public static class AuthorizerConfiguration {
Expand Down

0 comments on commit 1787df8

Please sign in to comment.