Skip to content

Commit

Permalink
GH-1077 Update AWSLambdaUtils.java with null check
Browse files Browse the repository at this point in the history
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
  • Loading branch information
digithead1011 authored and olegz committed Oct 10, 2023
1 parent f33b4e4 commit 1f188e8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ static boolean isSupportedAWSType(Type inputType) {
if (FunctionTypeUtils.isMessage(inputType) || FunctionTypeUtils.isPublisher(inputType)) {
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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ protected boolean canConvertFrom(Message<?> message, @Nullable Class<?> targetCl
return ((boolean) message.getHeaders().get(AWSLambdaUtils.AWS_EVENT));
}
//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 false;
Expand All @@ -75,7 +76,8 @@ protected Object convertFromInternal(Message<?> message, Class<?> targetClass, @
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());
Object event = serializer.fromJson(new ByteArrayInputStream((byte[]) message.getPayload()));
return event;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1407,6 +1407,21 @@ public void testWithDefaultRouting() throws Exception {
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
@Configuration
public static class AuthorizerConfiguration {
Expand Down Expand Up @@ -1811,6 +1826,17 @@ public Function<Map<String, Object>, String> inputApiEventAsMap() {
}
}

@EnableAutoConfiguration
@Configuration
public static class PrimitiveConfiguration {
@Bean
public Function<Message<byte[]>, byte[]> returnByteArrayAsMessage() {
return v -> {
return v.getPayload();
};
}
}

public static class Person {
private String name;

Expand Down

0 comments on commit 1f188e8

Please sign in to comment.