Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that Amazon Lambda uses the ObjectMapper bean when available #7508

Merged
merged 1 commit into from
Mar 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.ObjectWriter;

import io.quarkus.arc.Arc;
import io.quarkus.arc.InstanceHandle;
import io.quarkus.arc.runtime.BeanContainer;
import io.quarkus.runtime.Application;
import io.quarkus.runtime.ShutdownContext;
Expand All @@ -45,14 +47,22 @@ public class AmazonLambdaRecorder {
public void setHandlerClass(Class<? extends RequestHandler<?, ?>> handler, BeanContainer container) {
handlerClass = handler;
beanContainer = container;
objectMapper = new ObjectMapper()
AmazonLambdaRecorder.objectMapper = getObjectMapper()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
Method handlerMethod = discoverHandlerMethod(handlerClass);
objectReader = objectMapper.readerFor(handlerMethod.getParameterTypes()[0]);
objectWriter = objectMapper.writerFor(handlerMethod.getReturnType());
}

private ObjectMapper getObjectMapper() {
InstanceHandle<ObjectMapper> instance = Arc.container().instance(ObjectMapper.class);
if (instance.isAvailable()) {
return instance.get().copy();
geoand marked this conversation as resolved.
Show resolved Hide resolved
}
return new ObjectMapper();
}

/**
* Called by JVM handler wrapper
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

import com.fasterxml.jackson.databind.ObjectMapper;

import io.quarkus.arc.Arc;
import io.quarkus.arc.ArcContainer;
import io.quarkus.arc.InstanceHandle;

public class LambdaClient {

private static final AtomicInteger REQUEST_ID_GENERATOR = new AtomicInteger();
Expand Down Expand Up @@ -46,7 +50,7 @@ public static <T> T invoke(Class<T> returnType, Object input) {
throw new RuntimeException(problem);
}
try {
final ObjectMapper mapper = new ObjectMapper();
final ObjectMapper mapper = getObjectMapper();
String id = "aws-request-" + REQUEST_ID_GENERATOR.incrementAndGet();
CompletableFuture<String> result = new CompletableFuture<>();
REQUESTS.put(id, result);
Expand Down Expand Up @@ -82,4 +86,16 @@ public String setValue(String value) {
}
}

private static ObjectMapper getObjectMapper() {
ArcContainer container = Arc.container();
if (container == null) {
return new ObjectMapper();
}
InstanceHandle<ObjectMapper> instance = container.instance(ObjectMapper.class);
if (instance.isAvailable()) {
return instance.get();
}
return new ObjectMapper();
}

}