|
14 | 14 | package software.amazon.lambda.powertools.utilities;
|
15 | 15 |
|
16 | 16 | import com.amazonaws.services.lambda.runtime.events.*;
|
| 17 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 18 | +import com.fasterxml.jackson.databind.ObjectReader; |
17 | 19 | import org.slf4j.Logger;
|
18 | 20 | import org.slf4j.LoggerFactory;
|
19 | 21 |
|
@@ -201,20 +203,29 @@ public <T> T as(Class<T> clazz) {
|
201 | 203 | * @return a list of objects of type T (deserialized from the content)
|
202 | 204 | */
|
203 | 205 | 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) { |
206 | 208 | throw new EventDeserializationException("The content of this event is not a list, consider using 'as' instead");
|
207 | 209 | }
|
208 | 210 | // should not occur, except if the event is really malformed
|
209 | 211 | throw new IllegalStateException("Event content is null: the event may be malformed (missing fields)");
|
210 | 212 | }
|
211 |
| - return contentList.stream().map(s -> { |
| 213 | + if (content != null) { |
| 214 | + ObjectReader reader = JsonConfig.get().getObjectMapper().readerForListOf(clazz); |
212 | 215 | 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); |
216 | 219 | }
|
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 | + } |
218 | 229 | }
|
219 | 230 | }
|
220 | 231 | }
|
0 commit comments