Skip to content

Commit 1787df8

Browse files
committed
GH-1135 Fix AWS data conversion
Resolves #1135
1 parent c0f4cba commit 1787df8

File tree

3 files changed

+60
-4
lines changed

3 files changed

+60
-4
lines changed

spring-cloud-function-adapters/spring-cloud-function-adapter-aws/src/main/java/org/springframework/cloud/function/adapter/aws/AWSLambdaUtils.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public static Message<byte[]> generateMessage(byte[] payload, Type inputType, bo
126126

127127
MessageBuilder builder = MessageBuilder
128128
.withPayload(structMessage instanceof Map msg && msg.containsKey("payload")
129-
? ((String) msg.get("payload")).getBytes(StandardCharsets.UTF_8)
129+
? (msg.get("payload"))
130130
: payload);
131131
if (isApiGateway) {
132132
builder.setHeader(AWSLambdaUtils.AWS_API_GATEWAY, true);

spring-cloud-function-adapters/spring-cloud-function-adapter-aws/src/main/java/org/springframework/cloud/function/adapter/aws/AWSTypesMessageConverter.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ protected Object convertFromInternal(Message<?> message, Class<?> targetClass, @
8989
}
9090
else {
9191
Object body;
92-
if (message.getHeaders().containsKey("payload")) {
93-
body = message.getPayload();
92+
if (structMessage.containsKey("body")) {
93+
body = structMessage.get("body");
9494
}
9595
else {
96-
body = structMessage.get("body");
96+
body = message.getPayload();
9797
}
9898
Object convertedResult = this.jsonMapper.fromJson(body, targetClass);
9999
return convertedResult;

spring-cloud-function-adapters/spring-cloud-function-adapter-aws/src/test/java/org/springframework/cloud/function/adapter/aws/FunctionInvokerTests.java

+56
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,35 @@ public class FunctionInvokerTests {
7878

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

81+
String someEvent = "{\n"
82+
+ " \"payload\": {\n"
83+
+ " \"headers\": {\n"
84+
+ " \"businessUnit\": \"1\"\n"
85+
+ " }\n"
86+
+ " },\n"
87+
+ " \"headers\": {\n"
88+
+ " \"aws-context\": {\n"
89+
+ " \"memoryLimit\": 1024,\n"
90+
+ " \"awsRequestId\": \"87a211bf-540f-4f9f-a218-d096a0099999\",\n"
91+
+ " \"functionName\": \"myfunction\",\n"
92+
+ " \"functionVersion\": \"278\",\n"
93+
+ " \"invokedFunctionArn\": \"arn:aws:lambda:us-east-1:xxxxxxx:function:xxxxx:snapstart\",\n"
94+
+ " \"deadlineTimeInMs\": 1712717704761,\n"
95+
+ " \"logger\": {\n"
96+
+ " \"logFiltering\": {\n"
97+
+ " \"minimumLogLevel\": \"UNDEFINED\"\n"
98+
+ " },\n"
99+
+ " \"logFormatter\": {},\n"
100+
+ " \"logFormat\": \"TEXT\"\n"
101+
+ " }\n"
102+
+ " },\n"
103+
+ " \"businessUnit\": \"1\",\n"
104+
+ " \"id\": \"xxxx\",\n"
105+
+ " \"aws-event\": true,\n"
106+
+ " \"timestamp\": 1712716805129\n"
107+
+ " }\n"
108+
+ "}";
109+
81110
String dynamoDbEvent = "{\n"
82111
+ " \"Records\": [\n"
83112
+ " {\n"
@@ -706,6 +735,22 @@ public void before() throws Exception {
706735
//this.getEnvironment().clear();
707736
}
708737

738+
@SuppressWarnings({ "rawtypes", "unchecked" })
739+
@Test
740+
public void testConversionWhenPayloadExists() throws Exception {
741+
System.setProperty("MAIN_CLASS", BasicConfiguration.class.getName());
742+
System.setProperty("spring.cloud.function.definition", "uppercase");
743+
FunctionInvoker invoker = new FunctionInvoker();
744+
745+
InputStream targetStream = new ByteArrayInputStream(this.someEvent.getBytes());
746+
ByteArrayOutputStream output = new ByteArrayOutputStream();
747+
invoker.handleRequest(targetStream, output, null);
748+
749+
Map result = mapper.readValue(output.toByteArray(), Map.class);
750+
assertThat(result).containsKey("HEADERS");
751+
752+
}
753+
709754
@Test
710755
public void testAPIGatewayCustomAuthorizerEvent() throws Exception {
711756
System.setProperty("MAIN_CLASS", AuthorizerConfiguration.class.getName());
@@ -1441,6 +1486,17 @@ public void testPrimitiveMessage() throws Exception {
14411486
assertThat(result).isEqualTo(testString);
14421487
}
14431488

1489+
@EnableAutoConfiguration
1490+
@Configuration
1491+
public static class BasicConfiguration {
1492+
@Bean
1493+
public Function<Message<String>, Message<String>> uppercase() {
1494+
return v -> {
1495+
return MessageBuilder.withPayload(v.getPayload().toUpperCase()).build();
1496+
};
1497+
}
1498+
}
1499+
14441500
@EnableAutoConfiguration
14451501
@Configuration
14461502
public static class AuthorizerConfiguration {

0 commit comments

Comments
 (0)