Skip to content

Commit 1c23738

Browse files
committed
deserialize a string as list
1 parent 379075f commit 1c23738

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

powertools-serialization/src/main/java/software/amazon/lambda/powertools/utilities/EventDeserializer.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
package software.amazon.lambda.powertools.utilities;
1515

1616
import com.amazonaws.services.lambda.runtime.events.*;
17+
import com.fasterxml.jackson.core.JsonProcessingException;
18+
import com.fasterxml.jackson.databind.ObjectReader;
1719
import org.slf4j.Logger;
1820
import org.slf4j.LoggerFactory;
1921

@@ -201,20 +203,29 @@ public <T> T as(Class<T> clazz) {
201203
* @return a list of objects of type T (deserialized from the content)
202204
*/
203205
public <T> List<T> asListOf(Class<T> clazz) {
204-
if (contentList == null) {
205-
if (content != null || contentMap != null || contentObject != null) {
206+
if (contentList == null && content == null) {
207+
if (contentMap != null || contentObject != null) {
206208
throw new EventDeserializationException("The content of this event is not a list, consider using 'as' instead");
207209
}
208210
// should not occur, except if the event is really malformed
209211
throw new IllegalStateException("Event content is null: the event may be malformed (missing fields)");
210212
}
211-
return contentList.stream().map(s -> {
213+
if (content != null) {
214+
ObjectReader reader = JsonConfig.get().getObjectMapper().readerForListOf(clazz);
212215
try {
213-
return s == null ? null : JsonConfig.get().getObjectMapper().reader().readValue(s, clazz);
214-
} catch (IOException e) {
215-
throw new EventDeserializationException("Cannot load the event as " + clazz.getSimpleName(), e);
216+
return reader.readValue(content);
217+
} catch (JsonProcessingException e) {
218+
throw new EventDeserializationException("Cannot load the event as a list of " + clazz.getSimpleName() + ", consider using 'as' instead", e);
216219
}
217-
}).collect(Collectors.toList());
220+
} else {
221+
return contentList.stream().map(s -> {
222+
try {
223+
return s == null ? null : JsonConfig.get().getObjectMapper().reader().readValue(s, clazz);
224+
} catch (IOException e) {
225+
throw new EventDeserializationException("Cannot load the event as a list of " + clazz.getSimpleName(), e);
226+
}
227+
}).collect(Collectors.toList());
228+
}
218229
}
219230
}
220231
}

powertools-serialization/src/test/java/software/amazon/lambda/powertools/utilities/EventDeserializerTest.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,22 @@ public void testDeserializeStringAsObject_shouldReturnObject() {
4444
assertProduct(product);
4545
}
4646

47+
@Test
48+
public void testDeserializeStringArrayAsList_shouldReturnList() {
49+
String productStr = "[{\"id\":1234, \"name\":\"product\", \"price\":42}, {\"id\":2345, \"name\":\"product2\", \"price\":43}]";
50+
List<Product> products = extractDataFrom(productStr).asListOf(Product.class);
51+
assertThat(products).hasSize(2);
52+
assertProduct(products.get(0));
53+
}
54+
55+
@Test
56+
public void testDeserializeStringAsList_shouldThrowException() {
57+
String productStr = "{\"id\":1234, \"name\":\"product\", \"price\":42}";
58+
assertThatThrownBy(() -> extractDataFrom(productStr).asListOf(Product.class))
59+
.isInstanceOf(EventDeserializationException.class)
60+
.hasMessage("Cannot load the event as a list of Product, consider using 'as' instead");
61+
}
62+
4763
@Test
4864
public void testDeserializeMapAsObject_shouldReturnObject() {
4965
Map<String, Object> map = new HashMap<>();
@@ -121,7 +137,7 @@ public void testDeserializeAPIGatewayEventAsList_shouldThrowException(APIGateway
121137
public void testDeserializeSQSEventBodyAsWrongObjectType_shouldThrowException(SQSEvent event) {
122138
assertThatThrownBy(() -> extractDataFrom(event).asListOf(Basket.class))
123139
.isInstanceOf(EventDeserializationException.class)
124-
.hasMessage("Cannot load the event as Basket");
140+
.hasMessage("Cannot load the event as a list of Basket");
125141
}
126142

127143
@ParameterizedTest

0 commit comments

Comments
 (0)