GH-1077 Update AWSLambdaUtils.java with null check

Resolves #1077 by checking for a null package, which can happen when the inputType is a Message that encloses a primitive generic type

Update AWSTypesMessageConverter.java

Added additional fixes for #1077 in AWTypesMessageConverter

Update FunctionInvokerTests.java

Added unit tests to verify fix for #1077

Checkstyle fix

Resolves #1078
This commit is contained in:
Rob Cash
2023-10-04 19:20:19 -04:00
committed by Oleg Zhurakousky
parent f33b4e4919
commit 1f188e8e36
3 changed files with 33 additions and 3 deletions

View File

@@ -74,7 +74,9 @@ public final class AWSLambdaUtils {
if (FunctionTypeUtils.isMessage(inputType) || FunctionTypeUtils.isPublisher(inputType)) { if (FunctionTypeUtils.isMessage(inputType) || FunctionTypeUtils.isPublisher(inputType)) {
inputType = FunctionTypeUtils.getImmediateGenericType(inputType, 0); inputType = FunctionTypeUtils.getImmediateGenericType(inputType, 0);
} }
return FunctionTypeUtils.getRawType(inputType).getPackage().getName().startsWith("com.amazonaws.services.lambda.runtime.events"); return FunctionTypeUtils.getRawType(inputType).getPackage() != null &&
FunctionTypeUtils.getRawType(inputType).getPackage().getName().startsWith(
"com.amazonaws.services.lambda.runtime.events");
} }
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")

View File

@@ -63,7 +63,8 @@ class AWSTypesMessageConverter extends JsonMessageConverter {
return ((boolean) message.getHeaders().get(AWSLambdaUtils.AWS_EVENT)); return ((boolean) message.getHeaders().get(AWSLambdaUtils.AWS_EVENT));
} }
//TODO Do we really need the ^^ above? It seems like the line below dows the trick //TODO Do we really need the ^^ above? It seems like the line below dows the trick
else if (targetClass.getPackage().getName().startsWith("com.amazonaws.services.lambda.runtime.events")) { else if (targetClass.getPackage() != null &&
targetClass.getPackage().getName().startsWith("com.amazonaws.services.lambda.runtime.events")) {
return true; return true;
} }
return false; return false;
@@ -75,7 +76,8 @@ class AWSTypesMessageConverter extends JsonMessageConverter {
return message.getPayload(); return message.getPayload();
} }
if (targetClass.getPackage().getName().startsWith("com.amazonaws.services.lambda.runtime.events")) { if (targetClass.getPackage() != null &&
targetClass.getPackage().getName().startsWith("com.amazonaws.services.lambda.runtime.events")) {
PojoSerializer<?> serializer = LambdaEventSerializers.serializerFor(targetClass, Thread.currentThread().getContextClassLoader()); PojoSerializer<?> serializer = LambdaEventSerializers.serializerFor(targetClass, Thread.currentThread().getContextClassLoader());
Object event = serializer.fromJson(new ByteArrayInputStream((byte[]) message.getPayload())); Object event = serializer.fromJson(new ByteArrayInputStream((byte[]) message.getPayload()));
return event; return event;

View File

@@ -1407,6 +1407,21 @@ public class FunctionInvokerTests {
assertThat(result.get("body")).isEqualTo("\"olleh\""); assertThat(result.get("body")).isEqualTo("\"olleh\"");
} }
@Test
public void testPrimitiveMessage() throws Exception {
System.setProperty("MAIN_CLASS", PrimitiveConfiguration.class.getName());
System.setProperty("spring.cloud.function.definition", "returnByteArrayAsMessage");
FunctionInvoker invoker = new FunctionInvoker();
String testString = "{ \"message\": \"Hello, world!\" }";
InputStream targetStream = new ByteArrayInputStream(testString.getBytes());
ByteArrayOutputStream output = new ByteArrayOutputStream();
invoker.handleRequest(targetStream, output, null);
String result = output.toString();
assertThat(result).isEqualTo(testString);
}
@EnableAutoConfiguration @EnableAutoConfiguration
@Configuration @Configuration
public static class AuthorizerConfiguration { public static class AuthorizerConfiguration {
@@ -1811,6 +1826,17 @@ public class FunctionInvokerTests {
} }
} }
@EnableAutoConfiguration
@Configuration
public static class PrimitiveConfiguration {
@Bean
public Function<Message<byte[]>, byte[]> returnByteArrayAsMessage() {
return v -> {
return v.getPayload();
};
}
}
public static class Person { public static class Person {
private String name; private String name;