Skip to content

Commit

Permalink
Fix correct parsing of collections in AmazonLambdaRecorder with Colle…
Browse files Browse the repository at this point in the history
…ctionInputReader
  • Loading branch information
hamburml authored and gsmet committed May 13, 2024
1 parent 251439e commit 06d24d4
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ public void testSimpleLambdaSuccess() throws Exception {
// you test your lambdas by invoking on http://localhost:8081
// this works in dev mode too

Person in = new Person();
in.setName("Stu");
Person in = new Person("Stu");
given()
.contentType("application/json")
.accept("application/json")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

public class Person {

public Person() {
}

public Person(String name) {
this.name = name;
}

private String name;

public String getName() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.quarkus.amazon.lambda.deployment.testing;

import java.util.List;
import java.util.stream.Collectors;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;

public class PersonListLambda implements RequestHandler<List<Person>, String> {

@Override
public String handleRequest(List<Person> people, Context context) {
return people.stream().map(Person::getName).collect(Collectors.joining(" "));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.quarkus.amazon.lambda.deployment.testing;

import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.containsString;

import java.util.ArrayList;
import java.util.List;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;

public class PersonListLambdaTest {

@RegisterExtension
static final QuarkusUnitTest test = new QuarkusUnitTest().setArchiveProducer(() -> ShrinkWrap
.create(JavaArchive.class)
.addClasses(PersonListLambda.class, Person.class));

@Test
void testFruitsLambda() throws Exception {

List<Person> personList = new ArrayList<>();
personList.add(new Person("Chris"));
personList.add(new Person("Fred"));

given()
.body(personList)
.when()
.post()
.then()
.statusCode(200)
.body(containsString("Chris Fred"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand All @@ -16,6 +17,7 @@
import com.amazonaws.services.lambda.runtime.events.S3Event;
import com.fasterxml.jackson.databind.ObjectMapper;

import io.quarkus.amazon.lambda.runtime.handlers.CollectionInputReader;
import io.quarkus.amazon.lambda.runtime.handlers.S3EventInputReader;
import io.quarkus.arc.runtime.BeanContainer;
import io.quarkus.runtime.LaunchMode;
Expand Down Expand Up @@ -53,11 +55,15 @@ static void initializeHandlerClass(Class<? extends RequestHandler<?, ?>> handler
ObjectMapper objectMapper = AmazonLambdaMapperRecorder.objectMapper;
Method handlerMethod = discoverHandlerMethod(handlerClass);
Class<?> parameterType = handlerMethod.getParameterTypes()[0];

if (parameterType.equals(S3Event.class)) {
objectReader = new S3EventInputReader(objectMapper);
} else if (Collection.class.isAssignableFrom(parameterType)) {
objectReader = new CollectionInputReader<>(objectMapper, handlerMethod);
} else {
objectReader = new JacksonInputReader(objectMapper.readerFor(parameterType));
}

objectWriter = new JacksonOutputWriter(objectMapper.writerFor(handlerMethod.getReturnType()));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.quarkus.amazon.lambda.runtime.handlers;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Collection;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;

import io.quarkus.amazon.lambda.runtime.LambdaInputReader;

public class CollectionInputReader<T> implements LambdaInputReader<Collection<T>> {
final ObjectReader reader;

public CollectionInputReader(ObjectMapper mapper, Method handler) {
Type genericParameterType = handler.getGenericParameterTypes()[0];
JavaType constructParameterType = mapper.getTypeFactory().constructType(genericParameterType);
this.reader = mapper.readerFor(constructParameterType);
}

@Override
public Collection<T> readValue(InputStream is) throws IOException {
return this.reader.readValue(is);
}
}

0 comments on commit 06d24d4

Please sign in to comment.